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/opus_audio_decoder.h" | 5 #include "media/filters/opus_audio_decoder.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 return true; | 242 return true; |
243 } | 243 } |
244 | 244 |
245 OpusAudioDecoder::OpusAudioDecoder( | 245 OpusAudioDecoder::OpusAudioDecoder( |
246 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 246 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
247 : task_runner_(task_runner), | 247 : task_runner_(task_runner), |
248 opus_decoder_(NULL), | 248 opus_decoder_(NULL), |
249 start_input_timestamp_(kNoTimestamp()) {} | 249 start_input_timestamp_(kNoTimestamp()) {} |
250 | 250 |
251 void OpusAudioDecoder::Initialize(const AudioDecoderConfig& config, | 251 void OpusAudioDecoder::Initialize(const AudioDecoderConfig& config, |
252 const PipelineStatusCB& status_cb) { | 252 const PipelineStatusCB& status_cb, |
| 253 const OutputCB& output_cb) { |
253 DCHECK(task_runner_->BelongsToCurrentThread()); | 254 DCHECK(task_runner_->BelongsToCurrentThread()); |
254 PipelineStatusCB initialize_cb = BindToCurrentLoop(status_cb); | 255 PipelineStatusCB initialize_cb = BindToCurrentLoop(status_cb); |
255 | 256 |
256 config_ = config; | 257 config_ = config; |
| 258 output_cb_ = BindToCurrentLoop(output_cb); |
257 | 259 |
258 if (!ConfigureDecoder()) { | 260 if (!ConfigureDecoder()) { |
259 initialize_cb.Run(DECODER_ERROR_NOT_SUPPORTED); | 261 initialize_cb.Run(DECODER_ERROR_NOT_SUPPORTED); |
260 return; | 262 return; |
261 } | 263 } |
262 | 264 |
263 initialize_cb.Run(PIPELINE_OK); | 265 initialize_cb.Run(PIPELINE_OK); |
264 } | 266 } |
265 | 267 |
266 void OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 268 void OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
267 const DecodeCB& decode_cb) { | 269 const DecodeCB& decode_cb) { |
268 DCHECK(task_runner_->BelongsToCurrentThread()); | 270 DCHECK(task_runner_->BelongsToCurrentThread()); |
269 DCHECK(!decode_cb.is_null()); | 271 DCHECK(!decode_cb.is_null()); |
270 | 272 |
271 DecodeBuffer(buffer, BindToCurrentLoop(decode_cb)); | 273 DecodeBuffer(buffer, BindToCurrentLoop(decode_cb)); |
272 } | 274 } |
273 | 275 |
274 void OpusAudioDecoder::Reset(const base::Closure& closure) { | 276 void OpusAudioDecoder::Reset(const base::Closure& closure) { |
275 DCHECK(task_runner_->BelongsToCurrentThread()); | 277 DCHECK(task_runner_->BelongsToCurrentThread()); |
276 | 278 |
277 opus_multistream_decoder_ctl(opus_decoder_, OPUS_RESET_STATE); | 279 opus_multistream_decoder_ctl(opus_decoder_, OPUS_RESET_STATE); |
(...skipping 18 matching lines...) Expand all Loading... |
296 const scoped_refptr<DecoderBuffer>& input, | 298 const scoped_refptr<DecoderBuffer>& input, |
297 const DecodeCB& decode_cb) { | 299 const DecodeCB& decode_cb) { |
298 DCHECK(task_runner_->BelongsToCurrentThread()); | 300 DCHECK(task_runner_->BelongsToCurrentThread()); |
299 DCHECK(!decode_cb.is_null()); | 301 DCHECK(!decode_cb.is_null()); |
300 | 302 |
301 DCHECK(input.get()); | 303 DCHECK(input.get()); |
302 | 304 |
303 // Libopus does not buffer output. Decoding is complete when an end of stream | 305 // Libopus does not buffer output. Decoding is complete when an end of stream |
304 // input buffer is received. | 306 // input buffer is received. |
305 if (input->end_of_stream()) { | 307 if (input->end_of_stream()) { |
306 decode_cb.Run(kOk, AudioBuffer::CreateEOSBuffer()); | 308 output_cb_.Run(AudioBuffer::CreateEOSBuffer()); |
| 309 decode_cb.Run(kOk); |
307 return; | 310 return; |
308 } | 311 } |
309 | 312 |
310 // Make sure we are notified if http://crbug.com/49709 returns. Issue also | 313 // Make sure we are notified if http://crbug.com/49709 returns. Issue also |
311 // occurs with some damaged files. | 314 // occurs with some damaged files. |
312 if (input->timestamp() == kNoTimestamp()) { | 315 if (input->timestamp() == kNoTimestamp()) { |
313 DLOG(ERROR) << "Received a buffer without timestamps!"; | 316 DLOG(ERROR) << "Received a buffer without timestamps!"; |
314 decode_cb.Run(kDecodeError, NULL); | 317 decode_cb.Run(kDecodeError); |
315 return; | 318 return; |
316 } | 319 } |
317 | 320 |
318 // Apply the necessary codec delay. | 321 // Apply the necessary codec delay. |
319 if (start_input_timestamp_ == kNoTimestamp()) | 322 if (start_input_timestamp_ == kNoTimestamp()) |
320 start_input_timestamp_ = input->timestamp(); | 323 start_input_timestamp_ = input->timestamp(); |
321 if (!discard_helper_->initialized() && | 324 if (!discard_helper_->initialized() && |
322 input->timestamp() == start_input_timestamp_) { | 325 input->timestamp() == start_input_timestamp_) { |
323 discard_helper_->Reset(config_.codec_delay()); | 326 discard_helper_->Reset(config_.codec_delay()); |
324 } | 327 } |
325 | 328 |
326 scoped_refptr<AudioBuffer> output_buffer; | 329 scoped_refptr<AudioBuffer> output_buffer; |
327 | 330 |
328 if (!Decode(input, &output_buffer)) { | 331 if (!Decode(input, &output_buffer)) { |
329 decode_cb.Run(kDecodeError, NULL); | 332 decode_cb.Run(kDecodeError); |
330 return; | 333 return; |
331 } | 334 } |
332 | 335 |
333 if (output_buffer.get()) { | 336 if (output_buffer) { |
334 // Execute callback to return the decoded audio. | 337 output_cb_.Run(output_buffer); |
335 decode_cb.Run(kOk, output_buffer); | |
336 } else { | |
337 // We exhausted the input data, but it wasn't enough for a frame. | |
338 decode_cb.Run(kNotEnoughData, NULL); | |
339 } | 338 } |
| 339 |
| 340 decode_cb.Run(kOk); |
340 } | 341 } |
341 | 342 |
342 bool OpusAudioDecoder::ConfigureDecoder() { | 343 bool OpusAudioDecoder::ConfigureDecoder() { |
343 if (config_.codec() != kCodecOpus) { | 344 if (config_.codec() != kCodecOpus) { |
344 DVLOG(1) << "Codec must be kCodecOpus."; | 345 DVLOG(1) << "Codec must be kCodecOpus."; |
345 return false; | 346 return false; |
346 } | 347 } |
347 | 348 |
348 const int channel_count = | 349 const int channel_count = |
349 ChannelLayoutToChannelCount(config_.channel_layout()); | 350 ChannelLayoutToChannelCount(config_.channel_layout()); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 output_buffer->get()->TrimEnd(trim_frames); | 477 output_buffer->get()->TrimEnd(trim_frames); |
477 | 478 |
478 // Handles discards and timestamping. Discard the buffer if more data needed. | 479 // Handles discards and timestamping. Discard the buffer if more data needed. |
479 if (!discard_helper_->ProcessBuffers(input, *output_buffer)) | 480 if (!discard_helper_->ProcessBuffers(input, *output_buffer)) |
480 *output_buffer = NULL; | 481 *output_buffer = NULL; |
481 | 482 |
482 return true; | 483 return true; |
483 } | 484 } |
484 | 485 |
485 } // namespace media | 486 } // namespace media |
OLD | NEW |