OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/filters/audio_decoder_selector.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "media/base/audio_decoder_config.h" |
| 12 #include "media/base/bind_to_loop.h" |
| 13 #include "media/base/demuxer_stream.h" |
| 14 #include "media/base/pipeline.h" |
| 15 #include "media/filters/decrypting_audio_decoder.h" |
| 16 #include "media/filters/decrypting_demuxer_stream.h" |
| 17 |
| 18 namespace media { |
| 19 |
| 20 AudioDecoderSelector::AudioDecoderSelector( |
| 21 const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 22 const AudioDecoderList& decoders, |
| 23 const SetDecryptorReadyCB& set_decryptor_ready_cb) |
| 24 : message_loop_(message_loop), |
| 25 decoders_(decoders), |
| 26 set_decryptor_ready_cb_(set_decryptor_ready_cb), |
| 27 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)) { |
| 28 } |
| 29 |
| 30 AudioDecoderSelector::~AudioDecoderSelector() {} |
| 31 |
| 32 void AudioDecoderSelector::SelectAudioDecoder( |
| 33 const scoped_refptr<DemuxerStream>& stream, |
| 34 const StatisticsCB& statistics_cb, |
| 35 const SelectDecoderCB& select_decoder_cb) { |
| 36 DVLOG(2) << "SelectAudioDecoder()"; |
| 37 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 38 DCHECK(stream); |
| 39 |
| 40 // Make sure the |selected_decoder_cb| on a different execution stack. |
| 41 select_decoder_cb_ = BindToCurrentLoop(select_decoder_cb); |
| 42 |
| 43 const AudioDecoderConfig& config = stream->audio_decoder_config(); |
| 44 if (!config.IsValidConfig()) { |
| 45 DLOG(ERROR) << "Invalid audio stream config."; |
| 46 base::ResetAndReturn(&select_decoder_cb_).Run(NULL, NULL); |
| 47 return; |
| 48 } |
| 49 |
| 50 input_stream_ = stream; |
| 51 statistics_cb_ = statistics_cb; |
| 52 |
| 53 if (!config.is_encrypted()) { |
| 54 if (decoders_.empty()) { |
| 55 DLOG(ERROR) << "No audio decoder can be used to decode the input stream."; |
| 56 base::ResetAndReturn(&select_decoder_cb_).Run(NULL, NULL); |
| 57 return; |
| 58 } |
| 59 |
| 60 InitializeNextDecoder(); |
| 61 return; |
| 62 } |
| 63 |
| 64 // This could happen if Encrypted Media Extension (EME) is not enabled. |
| 65 if (set_decryptor_ready_cb_.is_null()) { |
| 66 base::ResetAndReturn(&select_decoder_cb_).Run(NULL, NULL); |
| 67 return; |
| 68 } |
| 69 |
| 70 audio_decoder_ = new DecryptingAudioDecoder(message_loop_, |
| 71 set_decryptor_ready_cb_); |
| 72 |
| 73 audio_decoder_->Initialize( |
| 74 input_stream_, |
| 75 BindToCurrentLoop(base::Bind( |
| 76 &AudioDecoderSelector::DecryptingAudioDecoderInitDone, |
| 77 weak_this_.GetWeakPtr())), |
| 78 statistics_cb_); |
| 79 } |
| 80 |
| 81 void AudioDecoderSelector::DecryptingAudioDecoderInitDone( |
| 82 PipelineStatus status) { |
| 83 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 84 |
| 85 if (status == PIPELINE_OK) { |
| 86 decoders_.clear(); |
| 87 base::ResetAndReturn(&select_decoder_cb_).Run(audio_decoder_, NULL); |
| 88 return; |
| 89 } |
| 90 |
| 91 audio_decoder_ = NULL; |
| 92 |
| 93 if (decoders_.empty()) { |
| 94 DLOG(ERROR) << "No audio decoder can be used to decode the input stream."; |
| 95 base::ResetAndReturn(&select_decoder_cb_).Run(NULL, NULL); |
| 96 return; |
| 97 } |
| 98 |
| 99 decrypted_stream_ = new DecryptingDemuxerStream( |
| 100 message_loop_, set_decryptor_ready_cb_); |
| 101 |
| 102 decrypted_stream_->Initialize( |
| 103 input_stream_, |
| 104 BindToCurrentLoop(base::Bind( |
| 105 &AudioDecoderSelector::DecryptingDemuxerStreamInitDone, |
| 106 weak_this_.GetWeakPtr()))); |
| 107 } |
| 108 |
| 109 void AudioDecoderSelector::DecryptingDemuxerStreamInitDone( |
| 110 PipelineStatus status) { |
| 111 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 112 |
| 113 if (status != PIPELINE_OK) { |
| 114 decrypted_stream_ = NULL; |
| 115 base::ResetAndReturn(&select_decoder_cb_).Run(NULL, NULL); |
| 116 return; |
| 117 } |
| 118 |
| 119 DCHECK(!decrypted_stream_->audio_decoder_config().is_encrypted()); |
| 120 input_stream_ = decrypted_stream_; |
| 121 InitializeNextDecoder(); |
| 122 } |
| 123 |
| 124 void AudioDecoderSelector::InitializeNextDecoder() { |
| 125 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 126 DCHECK(!decoders_.empty()); |
| 127 |
| 128 audio_decoder_ = decoders_.front(); |
| 129 decoders_.pop_front(); |
| 130 DCHECK(audio_decoder_); |
| 131 audio_decoder_->Initialize( |
| 132 input_stream_, |
| 133 BindToCurrentLoop(base::Bind( |
| 134 &AudioDecoderSelector::DecoderInitDone, weak_this_.GetWeakPtr())), |
| 135 statistics_cb_); |
| 136 } |
| 137 |
| 138 void AudioDecoderSelector::DecoderInitDone(PipelineStatus status) { |
| 139 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 140 |
| 141 if (status != PIPELINE_OK) { |
| 142 if (!decoders_.empty()) |
| 143 InitializeNextDecoder(); |
| 144 else |
| 145 base::ResetAndReturn(&select_decoder_cb_).Run(NULL, NULL); |
| 146 return; |
| 147 } |
| 148 |
| 149 decoders_.clear(); |
| 150 base::ResetAndReturn(&select_decoder_cb_).Run(audio_decoder_, |
| 151 decrypted_stream_); |
| 152 } |
| 153 |
| 154 } // namespace media |
OLD | NEW |