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_factory.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 AudioDecoderFactory::AudioDecoderFactory( |
| 21 const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 22 const AudioDecoderList& decoders, |
| 23 const RequestDecryptorNotificationCB& request_decryptor_notification_cb) |
| 24 : message_loop_(message_loop), |
| 25 precreated_decoders_(decoders), |
| 26 request_decryptor_notification_cb_(request_decryptor_notification_cb), |
| 27 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| 28 weak_this_(weak_ptr_factory_.GetWeakPtr()) { |
| 29 } |
| 30 |
| 31 void AudioDecoderFactory::InitAudioDecoder( |
| 32 const scoped_refptr<DemuxerStream>& stream, |
| 33 const StatisticsCB& statistics_cb, |
| 34 const InitDoneCB& init_done_cb) { |
| 35 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 36 DVLOG(2) << "InitAudioDecoder()"; |
| 37 DCHECK(stream); |
| 38 |
| 39 init_done_cb_ = BindToCurrentLoop(init_done_cb); |
| 40 |
| 41 const AudioDecoderConfig& config = stream->audio_decoder_config(); |
| 42 if (!config.IsValidConfig()) { |
| 43 DLOG(ERROR) << "Invalid audio stream config."; |
| 44 base::ResetAndReturn(&init_done_cb_).Run(NULL, NULL); |
| 45 return; |
| 46 } |
| 47 |
| 48 input_stream_ = stream; |
| 49 statistics_cb_ = statistics_cb; |
| 50 |
| 51 if (!config.is_encrypted()) { |
| 52 if (precreated_decoders_.empty()) { |
| 53 DLOG(ERROR) << "No audio decoder can be used to decode the input stream."; |
| 54 base::ResetAndReturn(&init_done_cb_).Run(NULL, NULL); |
| 55 return; |
| 56 } |
| 57 |
| 58 InitializeNextPrecreatedDecoder(); |
| 59 return; |
| 60 } |
| 61 |
| 62 // This could happen if Encrypted Media Extension (EME) is not enabled. |
| 63 if (request_decryptor_notification_cb_.is_null()) { |
| 64 base::ResetAndReturn(&init_done_cb_).Run(NULL, NULL); |
| 65 return; |
| 66 } |
| 67 |
| 68 audio_decoder_ = new DecryptingAudioDecoder( |
| 69 message_loop_, request_decryptor_notification_cb_); |
| 70 |
| 71 audio_decoder_->Initialize( |
| 72 input_stream_, |
| 73 BindToCurrentLoop(base::Bind( |
| 74 &AudioDecoderFactory::DecryptingAudioDecoderInitDone, weak_this_)), |
| 75 statistics_cb_); |
| 76 } |
| 77 |
| 78 void AudioDecoderFactory::DecryptingAudioDecoderInitDone( |
| 79 PipelineStatus status) { |
| 80 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 81 |
| 82 if (status == PIPELINE_OK) { |
| 83 precreated_decoders_.clear(); |
| 84 base::ResetAndReturn(&init_done_cb_).Run(audio_decoder_, NULL); |
| 85 return; |
| 86 } |
| 87 |
| 88 audio_decoder_ = NULL; |
| 89 |
| 90 if (precreated_decoders_.empty()) { |
| 91 DLOG(ERROR) << "No audio decoder can be used to decode the input stream."; |
| 92 base::ResetAndReturn(&init_done_cb_).Run(NULL, NULL); |
| 93 return; |
| 94 } |
| 95 |
| 96 decrypted_stream_ = new DecryptingDemuxerStream( |
| 97 message_loop_, request_decryptor_notification_cb_); |
| 98 |
| 99 decrypted_stream_->Initialize( |
| 100 input_stream_, |
| 101 BindToCurrentLoop(base::Bind( |
| 102 &AudioDecoderFactory::DecryptingDemuxerStreamInitDone, weak_this_))); |
| 103 } |
| 104 |
| 105 void AudioDecoderFactory::DecryptingDemuxerStreamInitDone( |
| 106 PipelineStatus status) { |
| 107 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 108 |
| 109 if (status != PIPELINE_OK) { |
| 110 decrypted_stream_ = NULL; |
| 111 base::ResetAndReturn(&init_done_cb_).Run(NULL, NULL); |
| 112 return; |
| 113 } |
| 114 |
| 115 DCHECK(!decrypted_stream_->audio_decoder_config().is_encrypted()); |
| 116 input_stream_ = decrypted_stream_; |
| 117 InitializeNextPrecreatedDecoder(); |
| 118 } |
| 119 |
| 120 void AudioDecoderFactory::InitializeNextPrecreatedDecoder() { |
| 121 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 122 DCHECK(!precreated_decoders_.empty()); |
| 123 |
| 124 audio_decoder_ = precreated_decoders_.front(); |
| 125 precreated_decoders_.pop_front(); |
| 126 DCHECK(audio_decoder_); |
| 127 audio_decoder_->Initialize( |
| 128 input_stream_, |
| 129 BindToCurrentLoop(base::Bind( |
| 130 &AudioDecoderFactory::PrecreatedDecoderInitDone, weak_this_)), |
| 131 statistics_cb_); |
| 132 } |
| 133 |
| 134 void AudioDecoderFactory::PrecreatedDecoderInitDone(PipelineStatus status) { |
| 135 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 136 |
| 137 if (status != PIPELINE_OK) { |
| 138 if (!precreated_decoders_.empty()) |
| 139 InitializeNextPrecreatedDecoder(); |
| 140 else |
| 141 base::ResetAndReturn(&init_done_cb_).Run(NULL, NULL); |
| 142 return; |
| 143 } |
| 144 |
| 145 precreated_decoders_.clear(); |
| 146 base::ResetAndReturn(&init_done_cb_).Run(audio_decoder_, decrypted_stream_); |
| 147 } |
| 148 |
| 149 AudioDecoderFactory::~AudioDecoderFactory() {} |
| 150 |
| 151 } // namespace media |
OLD | NEW |