| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "media/filters/decrypting_audio_decoder.h" | 5 #include "media/filters/decrypting_audio_decoder.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 return std::abs(timestamp_1.InMilliseconds() - timestamp_2.InMilliseconds()) > | 33 return std::abs(timestamp_1.InMilliseconds() - timestamp_2.InMilliseconds()) > |
| 34 kOutOfSyncThresholdInMilliseconds; | 34 kOutOfSyncThresholdInMilliseconds; |
| 35 } | 35 } |
| 36 | 36 |
| 37 DecryptingAudioDecoder::DecryptingAudioDecoder( | 37 DecryptingAudioDecoder::DecryptingAudioDecoder( |
| 38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 39 const SetDecryptorReadyCB& set_decryptor_ready_cb) | 39 const SetDecryptorReadyCB& set_decryptor_ready_cb) |
| 40 : task_runner_(task_runner), | 40 : task_runner_(task_runner), |
| 41 weak_factory_(this), | 41 weak_factory_(this), |
| 42 state_(kUninitialized), | 42 state_(kUninitialized), |
| 43 demuxer_stream_(NULL), | |
| 44 set_decryptor_ready_cb_(set_decryptor_ready_cb), | 43 set_decryptor_ready_cb_(set_decryptor_ready_cb), |
| 45 decryptor_(NULL), | 44 decryptor_(NULL), |
| 46 key_added_while_decode_pending_(false), | 45 key_added_while_decode_pending_(false), |
| 47 bits_per_channel_(0), | 46 bits_per_channel_(0), |
| 48 channel_layout_(CHANNEL_LAYOUT_NONE), | 47 channel_layout_(CHANNEL_LAYOUT_NONE), |
| 49 samples_per_second_(0) { | 48 samples_per_second_(0) { |
| 50 } | 49 } |
| 51 | 50 |
| 52 void DecryptingAudioDecoder::Initialize( | 51 void DecryptingAudioDecoder::Initialize(const AudioDecoderConfig& config, |
| 53 DemuxerStream* stream, | 52 const PipelineStatusCB& status_cb) { |
| 54 const PipelineStatusCB& status_cb, | |
| 55 const StatisticsCB& statistics_cb) { | |
| 56 DVLOG(2) << "Initialize()"; | 53 DVLOG(2) << "Initialize()"; |
| 57 DCHECK(task_runner_->BelongsToCurrentThread()); | 54 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 58 DCHECK_EQ(state_, kUninitialized) << state_; | 55 DCHECK_EQ(state_, kUninitialized) << state_; |
| 59 DCHECK(stream); | |
| 60 | 56 |
| 61 weak_this_ = weak_factory_.GetWeakPtr(); | 57 weak_this_ = weak_factory_.GetWeakPtr(); |
| 62 init_cb_ = BindToCurrentLoop(status_cb); | 58 init_cb_ = BindToCurrentLoop(status_cb); |
| 63 | 59 |
| 64 const AudioDecoderConfig& config = stream->audio_decoder_config(); | |
| 65 if (!config.IsValidConfig()) { | 60 if (!config.IsValidConfig()) { |
| 66 DLOG(ERROR) << "Invalid audio stream config."; | 61 DLOG(ERROR) << "Invalid audio stream config."; |
| 67 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_DECODE); | 62 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_DECODE); |
| 68 return; | 63 return; |
| 69 } | 64 } |
| 70 | 65 |
| 71 // DecryptingAudioDecoder only accepts potentially encrypted stream. | 66 // DecryptingAudioDecoder only accepts potentially encrypted stream. |
| 72 if (!config.is_encrypted()) { | 67 if (!config.is_encrypted()) { |
| 73 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 68 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
| 74 return; | 69 return; |
| 75 } | 70 } |
| 76 | 71 |
| 77 DCHECK(!demuxer_stream_); | 72 config_ = config; |
| 78 demuxer_stream_ = stream; | |
| 79 statistics_cb_ = statistics_cb; | |
| 80 | 73 |
| 81 state_ = kDecryptorRequested; | 74 state_ = kDecryptorRequested; |
| 82 set_decryptor_ready_cb_.Run(BindToCurrentLoop( | 75 set_decryptor_ready_cb_.Run(BindToCurrentLoop( |
| 83 base::Bind(&DecryptingAudioDecoder::SetDecryptor, weak_this_))); | 76 base::Bind(&DecryptingAudioDecoder::SetDecryptor, weak_this_))); |
| 84 } | 77 } |
| 85 | 78 |
| 86 void DecryptingAudioDecoder::Read(const ReadCB& read_cb) { | 79 void DecryptingAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
| 87 DVLOG(3) << "Read()"; | 80 const DecodeCB& decode_cb) { |
| 81 DVLOG(3) << "Decode()"; |
| 88 DCHECK(task_runner_->BelongsToCurrentThread()); | 82 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 89 DCHECK(state_ == kIdle || state_ == kDecodeFinished) << state_; | 83 DCHECK(state_ == kIdle || state_ == kDecodeFinished) << state_; |
| 90 DCHECK(!read_cb.is_null()); | 84 DCHECK(!decode_cb.is_null()); |
| 91 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | 85 CHECK(decode_cb_.is_null()) << "Overlapping decodes are not supported."; |
| 92 | 86 |
| 93 read_cb_ = BindToCurrentLoop(read_cb); | 87 decode_cb_ = BindToCurrentLoop(decode_cb); |
| 94 | 88 |
| 95 // Return empty (end-of-stream) frames if decoding has finished. | 89 // Return empty (end-of-stream) frames if decoding has finished. |
| 96 if (state_ == kDecodeFinished) { | 90 if (state_ == kDecodeFinished) { |
| 97 base::ResetAndReturn(&read_cb_).Run(kOk, AudioBuffer::CreateEOSBuffer()); | 91 base::ResetAndReturn(&decode_cb_).Run(kOk, AudioBuffer::CreateEOSBuffer()); |
| 98 return; | 92 return; |
| 99 } | 93 } |
| 100 | 94 |
| 101 if (!queued_audio_frames_.empty()) { | 95 if (!queued_audio_frames_.empty()) { |
| 102 base::ResetAndReturn(&read_cb_).Run(kOk, queued_audio_frames_.front()); | 96 DCHECK(!buffer); |
| 97 base::ResetAndReturn(&decode_cb_).Run(kOk, queued_audio_frames_.front()); |
| 103 queued_audio_frames_.pop_front(); | 98 queued_audio_frames_.pop_front(); |
| 104 return; | 99 return; |
| 105 } | 100 } |
| 106 | 101 |
| 107 state_ = kPendingDemuxerRead; | 102 pending_buffer_to_decode_ = buffer; |
| 108 ReadFromDemuxerStream(); | 103 state_ = kPendingDecode; |
| 104 DecodePendingBuffer(); |
| 105 } |
| 106 |
| 107 scoped_refptr<AudioBuffer> DecryptingAudioDecoder::GetAudioBuffer( |
| 108 AudioDecoder::Status* status) { |
| 109 if (queued_audio_frames_.empty()) { |
| 110 return NULL; |
| 111 } |
| 112 scoped_refptr<AudioBuffer> out = queued_audio_frames_.front(); |
| 113 queued_audio_frames_.pop_front(); |
| 114 *status = kOk; // We only enqueue buffers if we were kOk. |
| 115 return out; |
| 109 } | 116 } |
| 110 | 117 |
| 111 void DecryptingAudioDecoder::Reset(const base::Closure& closure) { | 118 void DecryptingAudioDecoder::Reset(const base::Closure& closure) { |
| 112 DVLOG(2) << "Reset() - state: " << state_; | 119 DVLOG(2) << "Reset() - state: " << state_; |
| 113 DCHECK(task_runner_->BelongsToCurrentThread()); | 120 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 114 DCHECK(state_ == kIdle || | 121 DCHECK(state_ == kIdle || |
| 115 state_ == kPendingConfigChange || | 122 state_ == kPendingConfigChange || |
| 116 state_ == kPendingDemuxerRead || | 123 state_ == kPendingDemuxerRead || |
| 117 state_ == kPendingDecode || | 124 state_ == kPendingDecode || |
| 118 state_ == kWaitingForKey || | 125 state_ == kWaitingForKey || |
| 119 state_ == kDecodeFinished) << state_; | 126 state_ == kDecodeFinished) << state_; |
| 120 DCHECK(init_cb_.is_null()); // No Reset() during pending initialization. | 127 DCHECK(init_cb_.is_null()); // No Reset() during pending initialization. |
| 121 DCHECK(reset_cb_.is_null()); | 128 DCHECK(reset_cb_.is_null()); |
| 122 | 129 |
| 123 reset_cb_ = BindToCurrentLoop(closure); | 130 reset_cb_ = BindToCurrentLoop(closure); |
| 124 | 131 |
| 125 decryptor_->ResetDecoder(Decryptor::kAudio); | 132 decryptor_->ResetDecoder(Decryptor::kAudio); |
| 126 | 133 |
| 127 // Reset() cannot complete if the read callback is still pending. | 134 // Reset() cannot complete if the read callback is still pending. |
| 128 // Defer the resetting process in this case. The |reset_cb_| will be fired | 135 // Defer the resetting process in this case. The |reset_cb_| will be fired |
| 129 // after the read callback is fired - see DecryptAndDecodeBuffer() and | 136 // after the read callback is fired - see DecryptAndDecodeBuffer() and |
| 130 // DeliverFrame(). | 137 // DeliverFrame(). |
| 131 if (state_ == kPendingConfigChange || | 138 if (state_ == kPendingConfigChange || state_ == kPendingDecode) { |
| 132 state_ == kPendingDemuxerRead || | 139 DCHECK(!decode_cb_.is_null()); |
| 133 state_ == kPendingDecode) { | |
| 134 DCHECK(!read_cb_.is_null()); | |
| 135 return; | 140 return; |
| 136 } | 141 } |
| 137 | 142 |
| 138 if (state_ == kWaitingForKey) { | 143 if (state_ == kWaitingForKey) { |
| 139 DCHECK(!read_cb_.is_null()); | 144 DCHECK(!decode_cb_.is_null()); |
| 140 pending_buffer_to_decode_ = NULL; | 145 pending_buffer_to_decode_ = NULL; |
| 141 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 146 base::ResetAndReturn(&decode_cb_).Run(kAborted, NULL); |
| 142 } | 147 } |
| 143 | 148 |
| 144 DCHECK(read_cb_.is_null()); | 149 DCHECK(decode_cb_.is_null()); |
| 145 DoReset(); | 150 DoReset(); |
| 146 } | 151 } |
| 147 | 152 |
| 148 void DecryptingAudioDecoder::Stop(const base::Closure& closure) { | 153 void DecryptingAudioDecoder::Stop(const base::Closure& closure) { |
| 149 DVLOG(2) << "Stop() - state: " << state_; | 154 DVLOG(2) << "Stop() - state: " << state_; |
| 150 DCHECK(task_runner_->BelongsToCurrentThread()); | 155 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 151 | 156 |
| 152 if (decryptor_) { | 157 if (decryptor_) { |
| 153 decryptor_->RegisterNewKeyCB(Decryptor::kAudio, Decryptor::NewKeyCB()); | 158 decryptor_->RegisterNewKeyCB(Decryptor::kAudio, Decryptor::NewKeyCB()); |
| 154 decryptor_->DeinitializeDecoder(Decryptor::kAudio); | 159 decryptor_->DeinitializeDecoder(Decryptor::kAudio); |
| 155 decryptor_ = NULL; | 160 decryptor_ = NULL; |
| 156 } | 161 } |
| 157 if (!set_decryptor_ready_cb_.is_null()) | 162 if (!set_decryptor_ready_cb_.is_null()) |
| 158 base::ResetAndReturn(&set_decryptor_ready_cb_).Run(DecryptorReadyCB()); | 163 base::ResetAndReturn(&set_decryptor_ready_cb_).Run(DecryptorReadyCB()); |
| 159 pending_buffer_to_decode_ = NULL; | 164 pending_buffer_to_decode_ = NULL; |
| 160 if (!init_cb_.is_null()) | 165 if (!init_cb_.is_null()) |
| 161 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 166 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
| 162 if (!read_cb_.is_null()) | 167 if (!decode_cb_.is_null()) |
| 163 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 168 base::ResetAndReturn(&decode_cb_).Run(kAborted, NULL); |
| 164 if (!reset_cb_.is_null()) | 169 if (!reset_cb_.is_null()) |
| 165 base::ResetAndReturn(&reset_cb_).Run(); | 170 base::ResetAndReturn(&reset_cb_).Run(); |
| 166 state_ = kStopped; | 171 state_ = kStopped; |
| 167 task_runner_->PostTask(FROM_HERE, closure); | 172 task_runner_->PostTask(FROM_HERE, closure); |
| 168 } | 173 } |
| 169 | 174 |
| 170 int DecryptingAudioDecoder::bits_per_channel() { | 175 int DecryptingAudioDecoder::bits_per_channel() { |
| 171 DCHECK(task_runner_->BelongsToCurrentThread()); | 176 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 172 return bits_per_channel_; | 177 return bits_per_channel_; |
| 173 } | 178 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 201 | 206 |
| 202 if (!decryptor) { | 207 if (!decryptor) { |
| 203 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 208 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
| 204 // TODO(xhwang): Add kError state. See http://crbug.com/251503 | 209 // TODO(xhwang): Add kError state. See http://crbug.com/251503 |
| 205 state_ = kStopped; | 210 state_ = kStopped; |
| 206 return; | 211 return; |
| 207 } | 212 } |
| 208 | 213 |
| 209 decryptor_ = decryptor; | 214 decryptor_ = decryptor; |
| 210 | 215 |
| 211 const AudioDecoderConfig& input_config = | |
| 212 demuxer_stream_->audio_decoder_config(); | |
| 213 AudioDecoderConfig config; | |
| 214 config.Initialize(input_config.codec(), | |
| 215 kSampleFormatS16, | |
| 216 input_config.channel_layout(), | |
| 217 input_config.samples_per_second(), | |
| 218 input_config.extra_data(), | |
| 219 input_config.extra_data_size(), | |
| 220 input_config.is_encrypted(), | |
| 221 false, | |
| 222 base::TimeDelta(), | |
| 223 base::TimeDelta()); | |
| 224 | |
| 225 state_ = kPendingDecoderInit; | 216 state_ = kPendingDecoderInit; |
| 226 decryptor_->InitializeAudioDecoder( | 217 decryptor_->InitializeAudioDecoder( |
| 227 config, | 218 config_, |
| 228 BindToCurrentLoop(base::Bind( | 219 BindToCurrentLoop(base::Bind( |
| 229 &DecryptingAudioDecoder::FinishInitialization, weak_this_))); | 220 &DecryptingAudioDecoder::FinishInitialization, weak_this_))); |
| 230 } | 221 } |
| 231 | 222 |
| 232 void DecryptingAudioDecoder::FinishInitialization(bool success) { | 223 void DecryptingAudioDecoder::FinishInitialization(bool success) { |
| 233 DVLOG(2) << "FinishInitialization()"; | 224 DVLOG(2) << "FinishInitialization()"; |
| 234 DCHECK(task_runner_->BelongsToCurrentThread()); | 225 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 235 | 226 |
| 236 if (state_ == kStopped) | 227 if (state_ == kStopped) |
| 237 return; | 228 return; |
| 238 | 229 |
| 239 DCHECK_EQ(state_, kPendingDecoderInit) << state_; | 230 DCHECK_EQ(state_, kPendingDecoderInit) << state_; |
| 240 DCHECK(!init_cb_.is_null()); | 231 DCHECK(!init_cb_.is_null()); |
| 241 DCHECK(reset_cb_.is_null()); // No Reset() before initialization finished. | 232 DCHECK(reset_cb_.is_null()); // No Reset() before initialization finished. |
| 242 DCHECK(read_cb_.is_null()); // No Read() before initialization finished. | 233 DCHECK(decode_cb_.is_null()); // No Decode() before initialization finished. |
| 243 | 234 |
| 244 if (!success) { | 235 if (!success) { |
| 245 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 236 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
| 246 state_ = kStopped; | 237 state_ = kStopped; |
| 247 return; | 238 return; |
| 248 } | 239 } |
| 249 | 240 |
| 250 // Success! | 241 // Success! |
| 251 UpdateDecoderConfig(); | 242 UpdateDecoderConfig(); |
| 252 | 243 |
| 253 decryptor_->RegisterNewKeyCB( | 244 decryptor_->RegisterNewKeyCB( |
| 254 Decryptor::kAudio, BindToCurrentLoop(base::Bind( | 245 Decryptor::kAudio, BindToCurrentLoop(base::Bind( |
| 255 &DecryptingAudioDecoder::OnKeyAdded, weak_this_))); | 246 &DecryptingAudioDecoder::OnKeyAdded, weak_this_))); |
| 256 | 247 |
| 257 state_ = kIdle; | 248 state_ = kIdle; |
| 258 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); | 249 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); |
| 259 } | 250 } |
| 260 | 251 |
| 261 void DecryptingAudioDecoder::FinishConfigChange(bool success) { | |
| 262 DVLOG(2) << "FinishConfigChange()"; | |
| 263 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 264 DCHECK_EQ(state_, kPendingConfigChange) << state_; | |
| 265 DCHECK(!read_cb_.is_null()); | |
| 266 | |
| 267 if (!success) { | |
| 268 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
| 269 state_ = kDecodeFinished; | |
| 270 if (!reset_cb_.is_null()) | |
| 271 base::ResetAndReturn(&reset_cb_).Run(); | |
| 272 return; | |
| 273 } | |
| 274 | |
| 275 // Config change succeeded. | |
| 276 UpdateDecoderConfig(); | |
| 277 | |
| 278 if (!reset_cb_.is_null()) { | |
| 279 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
| 280 DoReset(); | |
| 281 return; | |
| 282 } | |
| 283 | |
| 284 state_ = kPendingDemuxerRead; | |
| 285 ReadFromDemuxerStream(); | |
| 286 } | |
| 287 | |
| 288 void DecryptingAudioDecoder::ReadFromDemuxerStream() { | |
| 289 DCHECK_EQ(state_, kPendingDemuxerRead) << state_; | |
| 290 DCHECK(!read_cb_.is_null()); | |
| 291 | |
| 292 demuxer_stream_->Read( | |
| 293 base::Bind(&DecryptingAudioDecoder::DecryptAndDecodeBuffer, weak_this_)); | |
| 294 } | |
| 295 | |
| 296 void DecryptingAudioDecoder::DecryptAndDecodeBuffer( | |
| 297 DemuxerStream::Status status, | |
| 298 const scoped_refptr<DecoderBuffer>& buffer) { | |
| 299 DVLOG(3) << "DecryptAndDecodeBuffer()"; | |
| 300 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 301 DCHECK_EQ(state_, kPendingDemuxerRead) << state_; | |
| 302 DCHECK(!read_cb_.is_null()); | |
| 303 DCHECK_EQ(buffer.get() != NULL, status == DemuxerStream::kOk) << status; | |
| 304 | |
| 305 if (status == DemuxerStream::kConfigChanged) { | |
| 306 DVLOG(2) << "DecryptAndDecodeBuffer() - kConfigChanged"; | |
| 307 | |
| 308 const AudioDecoderConfig& input_config = | |
| 309 demuxer_stream_->audio_decoder_config(); | |
| 310 AudioDecoderConfig config; | |
| 311 config.Initialize(input_config.codec(), | |
| 312 kSampleFormatS16, | |
| 313 input_config.channel_layout(), | |
| 314 input_config.samples_per_second(), | |
| 315 input_config.extra_data(), | |
| 316 input_config.extra_data_size(), | |
| 317 input_config.is_encrypted(), | |
| 318 false, | |
| 319 base::TimeDelta(), | |
| 320 base::TimeDelta()); | |
| 321 | |
| 322 state_ = kPendingConfigChange; | |
| 323 decryptor_->DeinitializeDecoder(Decryptor::kAudio); | |
| 324 decryptor_->InitializeAudioDecoder( | |
| 325 config, BindToCurrentLoop(base::Bind( | |
| 326 &DecryptingAudioDecoder::FinishConfigChange, weak_this_))); | |
| 327 return; | |
| 328 } | |
| 329 | |
| 330 if (!reset_cb_.is_null()) { | |
| 331 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
| 332 DoReset(); | |
| 333 return; | |
| 334 } | |
| 335 | |
| 336 if (status == DemuxerStream::kAborted) { | |
| 337 DVLOG(2) << "DecryptAndDecodeBuffer() - kAborted"; | |
| 338 state_ = kIdle; | |
| 339 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
| 340 return; | |
| 341 } | |
| 342 | |
| 343 DCHECK_EQ(status, DemuxerStream::kOk); | |
| 344 | |
| 345 // Initialize the |next_output_timestamp_| to be the timestamp of the first | |
| 346 // non-EOS buffer. | |
| 347 if (timestamp_helper_->base_timestamp() == kNoTimestamp() && | |
| 348 !buffer->end_of_stream()) { | |
| 349 timestamp_helper_->SetBaseTimestamp(buffer->timestamp()); | |
| 350 } | |
| 351 | |
| 352 pending_buffer_to_decode_ = buffer; | |
| 353 state_ = kPendingDecode; | |
| 354 DecodePendingBuffer(); | |
| 355 } | |
| 356 | |
| 357 void DecryptingAudioDecoder::DecodePendingBuffer() { | 252 void DecryptingAudioDecoder::DecodePendingBuffer() { |
| 358 DCHECK(task_runner_->BelongsToCurrentThread()); | 253 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 359 DCHECK_EQ(state_, kPendingDecode) << state_; | 254 DCHECK_EQ(state_, kPendingDecode) << state_; |
| 360 | 255 |
| 361 int buffer_size = 0; | 256 int buffer_size = 0; |
| 362 if (!pending_buffer_to_decode_->end_of_stream()) { | 257 if (!pending_buffer_to_decode_->end_of_stream()) { |
| 363 buffer_size = pending_buffer_to_decode_->data_size(); | 258 buffer_size = pending_buffer_to_decode_->data_size(); |
| 364 } | 259 } |
| 365 | 260 |
| 366 decryptor_->DecryptAndDecodeAudio( | 261 decryptor_->DecryptAndDecodeAudio( |
| 367 pending_buffer_to_decode_, | 262 pending_buffer_to_decode_, |
| 368 BindToCurrentLoop(base::Bind( | 263 BindToCurrentLoop(base::Bind( |
| 369 &DecryptingAudioDecoder::DeliverFrame, weak_this_, buffer_size))); | 264 &DecryptingAudioDecoder::DeliverFrame, weak_this_, buffer_size))); |
| 370 } | 265 } |
| 371 | 266 |
| 372 void DecryptingAudioDecoder::DeliverFrame( | 267 void DecryptingAudioDecoder::DeliverFrame( |
| 373 int buffer_size, | 268 int buffer_size, |
| 374 Decryptor::Status status, | 269 Decryptor::Status status, |
| 375 const Decryptor::AudioBuffers& frames) { | 270 const Decryptor::AudioBuffers& frames) { |
| 376 DVLOG(3) << "DeliverFrame() - status: " << status; | 271 DVLOG(3) << "DeliverFrame() - status: " << status; |
| 377 DCHECK(task_runner_->BelongsToCurrentThread()); | 272 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 378 | 273 |
| 379 if (state_ == kStopped) | 274 if (state_ == kStopped) |
| 380 return; | 275 return; |
| 381 | 276 |
| 382 DCHECK_EQ(state_, kPendingDecode) << state_; | 277 DCHECK_EQ(state_, kPendingDecode) << state_; |
| 383 DCHECK(!read_cb_.is_null()); | 278 DCHECK(!decode_cb_.is_null()); |
| 384 DCHECK(pending_buffer_to_decode_.get()); | 279 DCHECK(pending_buffer_to_decode_.get()); |
| 385 DCHECK(queued_audio_frames_.empty()); | 280 DCHECK(queued_audio_frames_.empty()); |
| 386 | 281 |
| 387 bool need_to_try_again_if_nokey_is_returned = key_added_while_decode_pending_; | 282 bool need_to_try_again_if_nokey_is_returned = key_added_while_decode_pending_; |
| 388 key_added_while_decode_pending_ = false; | 283 key_added_while_decode_pending_ = false; |
| 389 | 284 |
| 390 scoped_refptr<DecoderBuffer> scoped_pending_buffer_to_decode = | 285 scoped_refptr<DecoderBuffer> scoped_pending_buffer_to_decode = |
| 391 pending_buffer_to_decode_; | 286 pending_buffer_to_decode_; |
| 392 pending_buffer_to_decode_ = NULL; | 287 pending_buffer_to_decode_ = NULL; |
| 393 | 288 |
| 394 if (!reset_cb_.is_null()) { | 289 if (!reset_cb_.is_null()) { |
| 395 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 290 base::ResetAndReturn(&decode_cb_).Run(kAborted, NULL); |
| 396 DoReset(); | 291 DoReset(); |
| 397 return; | 292 return; |
| 398 } | 293 } |
| 399 | 294 |
| 400 DCHECK_EQ(status == Decryptor::kSuccess, !frames.empty()); | 295 DCHECK_EQ(status == Decryptor::kSuccess, !frames.empty()); |
| 401 | 296 |
| 402 if (status == Decryptor::kError) { | 297 if (status == Decryptor::kError) { |
| 403 DVLOG(2) << "DeliverFrame() - kError"; | 298 DVLOG(2) << "DeliverFrame() - kError"; |
| 404 state_ = kDecodeFinished; | 299 state_ = kDecodeFinished; // TODO add kError state |
| 405 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | 300 base::ResetAndReturn(&decode_cb_).Run(kDecodeError, NULL); |
| 406 return; | 301 return; |
| 407 } | 302 } |
| 408 | 303 |
| 409 if (status == Decryptor::kNoKey) { | 304 if (status == Decryptor::kNoKey) { |
| 410 DVLOG(2) << "DeliverFrame() - kNoKey"; | 305 DVLOG(2) << "DeliverFrame() - kNoKey"; |
| 411 // Set |pending_buffer_to_decode_| back as we need to try decoding the | 306 // Set |pending_buffer_to_decode_| back as we need to try decoding the |
| 412 // pending buffer again when new key is added to the decryptor. | 307 // pending buffer again when new key is added to the decryptor. |
| 413 pending_buffer_to_decode_ = scoped_pending_buffer_to_decode; | 308 pending_buffer_to_decode_ = scoped_pending_buffer_to_decode; |
| 414 | 309 |
| 415 if (need_to_try_again_if_nokey_is_returned) { | 310 if (need_to_try_again_if_nokey_is_returned) { |
| 416 // The |state_| is still kPendingDecode. | 311 // The |state_| is still kPendingDecode. |
| 417 DecodePendingBuffer(); | 312 DecodePendingBuffer(); |
| 418 return; | 313 return; |
| 419 } | 314 } |
| 420 | 315 |
| 421 state_ = kWaitingForKey; | 316 state_ = kWaitingForKey; |
| 422 return; | 317 return; |
| 423 } | 318 } |
| 424 | 319 |
| 425 // The buffer has been accepted by the decoder, let's report statistics. | |
| 426 if (buffer_size) { | |
| 427 PipelineStatistics statistics; | |
| 428 statistics.audio_bytes_decoded = buffer_size; | |
| 429 statistics_cb_.Run(statistics); | |
| 430 } | |
| 431 | |
| 432 if (status == Decryptor::kNeedMoreData) { | 320 if (status == Decryptor::kNeedMoreData) { |
| 433 DVLOG(2) << "DeliverFrame() - kNeedMoreData"; | 321 DVLOG(2) << "DeliverFrame() - kNeedMoreData"; |
| 434 if (scoped_pending_buffer_to_decode->end_of_stream()) { | 322 if (scoped_pending_buffer_to_decode->end_of_stream()) { |
| 435 state_ = kDecodeFinished; | 323 state_ = kDecodeFinished; |
| 436 base::ResetAndReturn(&read_cb_).Run(kOk, AudioBuffer::CreateEOSBuffer()); | 324 base::ResetAndReturn(&decode_cb_) |
| 325 .Run(kOk, AudioBuffer::CreateEOSBuffer()); |
| 437 return; | 326 return; |
| 438 } | 327 } |
| 439 | 328 |
| 440 state_ = kPendingDemuxerRead; | 329 state_ = kIdle; |
| 441 ReadFromDemuxerStream(); | 330 base::ResetAndReturn(&decode_cb_).Run(kNotEnoughData, NULL); |
| 442 return; | 331 return; |
| 443 } | 332 } |
| 444 | 333 |
| 445 DCHECK_EQ(status, Decryptor::kSuccess); | 334 DCHECK_EQ(status, Decryptor::kSuccess); |
| 446 DCHECK(!frames.empty()); | 335 DCHECK(!frames.empty()); |
| 447 EnqueueFrames(frames); | 336 EnqueueFrames(frames); |
| 448 | 337 |
| 449 state_ = kIdle; | 338 state_ = kIdle; |
| 450 base::ResetAndReturn(&read_cb_).Run(kOk, queued_audio_frames_.front()); | 339 base::ResetAndReturn(&decode_cb_).Run(kOk, queued_audio_frames_.front()); |
| 451 queued_audio_frames_.pop_front(); | 340 queued_audio_frames_.pop_front(); |
| 452 } | 341 } |
| 453 | 342 |
| 454 void DecryptingAudioDecoder::OnKeyAdded() { | 343 void DecryptingAudioDecoder::OnKeyAdded() { |
| 455 DCHECK(task_runner_->BelongsToCurrentThread()); | 344 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 456 | 345 |
| 457 if (state_ == kPendingDecode) { | 346 if (state_ == kPendingDecode) { |
| 458 key_added_while_decode_pending_ = true; | 347 key_added_while_decode_pending_ = true; |
| 459 return; | 348 return; |
| 460 } | 349 } |
| 461 | 350 |
| 462 if (state_ == kWaitingForKey) { | 351 if (state_ == kWaitingForKey) { |
| 463 state_ = kPendingDecode; | 352 state_ = kPendingDecode; |
| 464 DecodePendingBuffer(); | 353 DecodePendingBuffer(); |
| 465 } | 354 } |
| 466 } | 355 } |
| 467 | 356 |
| 468 void DecryptingAudioDecoder::DoReset() { | 357 void DecryptingAudioDecoder::DoReset() { |
| 469 DCHECK(init_cb_.is_null()); | 358 DCHECK(init_cb_.is_null()); |
| 470 DCHECK(read_cb_.is_null()); | 359 DCHECK(decode_cb_.is_null()); |
| 471 timestamp_helper_->SetBaseTimestamp(kNoTimestamp()); | 360 timestamp_helper_->SetBaseTimestamp(kNoTimestamp()); |
| 472 state_ = kIdle; | 361 state_ = kIdle; |
| 473 base::ResetAndReturn(&reset_cb_).Run(); | 362 base::ResetAndReturn(&reset_cb_).Run(); |
| 474 } | 363 } |
| 475 | 364 |
| 476 void DecryptingAudioDecoder::UpdateDecoderConfig() { | 365 void DecryptingAudioDecoder::UpdateDecoderConfig() { |
| 477 const AudioDecoderConfig& config = demuxer_stream_->audio_decoder_config(); | |
| 478 bits_per_channel_ = kSupportedBitsPerChannel; | 366 bits_per_channel_ = kSupportedBitsPerChannel; |
| 479 channel_layout_ = config.channel_layout(); | 367 channel_layout_ = config_.channel_layout(); |
| 480 samples_per_second_ = config.samples_per_second(); | 368 samples_per_second_ = config_.samples_per_second(); |
| 481 timestamp_helper_.reset(new AudioTimestampHelper(samples_per_second_)); | 369 timestamp_helper_.reset(new AudioTimestampHelper(samples_per_second_)); |
| 482 } | 370 } |
| 483 | 371 |
| 484 void DecryptingAudioDecoder::EnqueueFrames( | 372 void DecryptingAudioDecoder::EnqueueFrames( |
| 485 const Decryptor::AudioBuffers& frames) { | 373 const Decryptor::AudioBuffers& frames) { |
| 486 queued_audio_frames_ = frames; | 374 queued_audio_frames_ = frames; |
| 487 | 375 |
| 488 for (Decryptor::AudioBuffers::iterator iter = queued_audio_frames_.begin(); | 376 for (Decryptor::AudioBuffers::iterator iter = queued_audio_frames_.begin(); |
| 489 iter != queued_audio_frames_.end(); | 377 iter != queued_audio_frames_.end(); |
| 490 ++iter) { | 378 ++iter) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 502 } | 390 } |
| 503 | 391 |
| 504 frame->set_timestamp(current_time); | 392 frame->set_timestamp(current_time); |
| 505 frame->set_duration( | 393 frame->set_duration( |
| 506 timestamp_helper_->GetFrameDuration(frame->frame_count())); | 394 timestamp_helper_->GetFrameDuration(frame->frame_count())); |
| 507 timestamp_helper_->AddFrames(frame->frame_count()); | 395 timestamp_helper_->AddFrames(frame->frame_count()); |
| 508 } | 396 } |
| 509 } | 397 } |
| 510 | 398 |
| 511 } // namespace media | 399 } // namespace media |
| OLD | NEW |