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/ffmpeg_audio_decoder.h" | 5 #include "media/filters/ffmpeg_audio_decoder.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/location.h" |
| 10 #include "base/message_loop_proxy.h" |
9 #include "media/base/audio_decoder_config.h" | 11 #include "media/base/audio_decoder_config.h" |
10 #include "media/base/data_buffer.h" | 12 #include "media/base/data_buffer.h" |
11 #include "media/base/decoder_buffer.h" | 13 #include "media/base/decoder_buffer.h" |
12 #include "media/base/demuxer.h" | 14 #include "media/base/demuxer.h" |
13 #include "media/base/pipeline.h" | 15 #include "media/base/pipeline.h" |
14 #include "media/ffmpeg/ffmpeg_common.h" | 16 #include "media/ffmpeg/ffmpeg_common.h" |
15 #include "media/filters/ffmpeg_glue.h" | 17 #include "media/filters/ffmpeg_glue.h" |
16 | 18 |
17 namespace media { | 19 namespace media { |
18 | 20 |
19 // Returns true if the decode result was end of stream. | 21 // Returns true if the decode result was end of stream. |
20 static inline bool IsEndOfStream(int result, int decoded_size, Buffer* input) { | 22 static inline bool IsEndOfStream(int result, int decoded_size, Buffer* input) { |
21 // Three conditions to meet to declare end of stream for this decoder: | 23 // Three conditions to meet to declare end of stream for this decoder: |
22 // 1. FFmpeg didn't read anything. | 24 // 1. FFmpeg didn't read anything. |
23 // 2. FFmpeg didn't output anything. | 25 // 2. FFmpeg didn't output anything. |
24 // 3. An end of stream buffer is received. | 26 // 3. An end of stream buffer is received. |
25 return result == 0 && decoded_size == 0 && input->IsEndOfStream(); | 27 return result == 0 && decoded_size == 0 && input->IsEndOfStream(); |
26 } | 28 } |
27 | 29 |
28 FFmpegAudioDecoder::FFmpegAudioDecoder( | 30 FFmpegAudioDecoder::FFmpegAudioDecoder( |
29 const base::Callback<MessageLoop*()>& message_loop_cb) | 31 const MessageLoopFactoryCB& message_loop_factory_cb) |
30 : message_loop_factory_cb_(message_loop_cb), | 32 : message_loop_factory_cb_(message_loop_factory_cb), |
31 message_loop_(NULL), | 33 message_loop_(NULL), |
32 codec_context_(NULL), | 34 codec_context_(NULL), |
33 bits_per_channel_(0), | 35 bits_per_channel_(0), |
34 channel_layout_(CHANNEL_LAYOUT_NONE), | 36 channel_layout_(CHANNEL_LAYOUT_NONE), |
35 samples_per_second_(0), | 37 samples_per_second_(0), |
36 bytes_per_frame_(0), | 38 bytes_per_frame_(0), |
37 output_timestamp_base_(kNoTimestamp()), | 39 output_timestamp_base_(kNoTimestamp()), |
38 total_frames_decoded_(0), | 40 total_frames_decoded_(0), |
39 last_input_timestamp_(kNoTimestamp()), | 41 last_input_timestamp_(kNoTimestamp()), |
40 output_bytes_to_drop_(0), | 42 output_bytes_to_drop_(0), |
41 av_frame_(NULL) { | 43 av_frame_(NULL) { |
42 } | 44 } |
43 | 45 |
44 void FFmpegAudioDecoder::Initialize( | 46 void FFmpegAudioDecoder::Initialize( |
45 const scoped_refptr<DemuxerStream>& stream, | 47 const scoped_refptr<DemuxerStream>& stream, |
46 const PipelineStatusCB& status_cb, | 48 const PipelineStatusCB& status_cb, |
47 const StatisticsCB& statistics_cb) { | 49 const StatisticsCB& statistics_cb) { |
48 // Ensure FFmpeg has been initialized | 50 // Ensure FFmpeg has been initialized |
49 FFmpegGlue::GetInstance(); | 51 FFmpegGlue::GetInstance(); |
50 | 52 |
51 if (!message_loop_) { | 53 if (!message_loop_) { |
52 message_loop_ = message_loop_factory_cb_.Run(); | 54 message_loop_ = base::ResetAndReturn(&message_loop_factory_cb_).Run(); |
53 message_loop_factory_cb_.Reset(); | |
54 } else { | 55 } else { |
55 // TODO(scherkus): initialization currently happens more than once in | 56 // TODO(scherkus): initialization currently happens more than once in |
56 // PipelineIntegrationTest.BasicPlayback. | 57 // PipelineIntegrationTest.BasicPlayback. |
57 LOG(ERROR) << "Initialize has already been called."; | 58 LOG(ERROR) << "Initialize has already been called."; |
58 } | 59 } |
59 message_loop_->PostTask( | 60 message_loop_->PostTask( |
60 FROM_HERE, | 61 FROM_HERE, |
61 base::Bind(&FFmpegAudioDecoder::DoInitialize, this, | 62 base::Bind(&FFmpegAudioDecoder::DoInitialize, this, |
62 stream, status_cb, statistics_cb)); | 63 stream, status_cb, statistics_cb)); |
63 } | 64 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 void FFmpegAudioDecoder::DoReset(const base::Closure& closure) { | 148 void FFmpegAudioDecoder::DoReset(const base::Closure& closure) { |
148 avcodec_flush_buffers(codec_context_); | 149 avcodec_flush_buffers(codec_context_); |
149 output_timestamp_base_ = kNoTimestamp(); | 150 output_timestamp_base_ = kNoTimestamp(); |
150 total_frames_decoded_ = 0; | 151 total_frames_decoded_ = 0; |
151 last_input_timestamp_ = kNoTimestamp(); | 152 last_input_timestamp_ = kNoTimestamp(); |
152 output_bytes_to_drop_ = 0; | 153 output_bytes_to_drop_ = 0; |
153 closure.Run(); | 154 closure.Run(); |
154 } | 155 } |
155 | 156 |
156 void FFmpegAudioDecoder::DoRead(const ReadCB& read_cb) { | 157 void FFmpegAudioDecoder::DoRead(const ReadCB& read_cb) { |
157 DCHECK_EQ(MessageLoop::current(), message_loop_); | 158 DCHECK(message_loop_->BelongsToCurrentThread()); |
158 DCHECK(!read_cb.is_null()); | 159 DCHECK(!read_cb.is_null()); |
159 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | 160 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
160 | 161 |
161 read_cb_ = read_cb; | 162 read_cb_ = read_cb; |
162 ReadFromDemuxerStream(); | 163 ReadFromDemuxerStream(); |
163 } | 164 } |
164 | 165 |
165 void FFmpegAudioDecoder::DoDecodeBuffer( | 166 void FFmpegAudioDecoder::DoDecodeBuffer( |
166 DemuxerStream::Status status, | 167 DemuxerStream::Status status, |
167 const scoped_refptr<DecoderBuffer>& input) { | 168 const scoped_refptr<DecoderBuffer>& input) { |
168 DCHECK_EQ(MessageLoop::current(), message_loop_); | 169 DCHECK(message_loop_->BelongsToCurrentThread()); |
169 DCHECK(!read_cb_.is_null()); | 170 DCHECK(!read_cb_.is_null()); |
170 | 171 |
171 if (status != DemuxerStream::kOk) { | 172 if (status != DemuxerStream::kOk) { |
172 DCHECK(!input); | 173 DCHECK(!input); |
173 // TODO(acolwell): Add support for reinitializing the decoder when | 174 // TODO(acolwell): Add support for reinitializing the decoder when |
174 // |status| == kConfigChanged. For now we just trigger a decode error. | 175 // |status| == kConfigChanged. For now we just trigger a decode error. |
175 AudioDecoder::Status decoder_status = | 176 AudioDecoder::Status decoder_status = |
176 (status == DemuxerStream::kAborted) ? kAborted : kDecodeError; | 177 (status == DemuxerStream::kAborted) ? kAborted : kDecodeError; |
177 base::ResetAndReturn(&read_cb_).Run(decoder_status, NULL); | 178 base::ResetAndReturn(&read_cb_).Run(decoder_status, NULL); |
178 return; | 179 return; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 &FFmpegAudioDecoder::DoDecodeBuffer, this, status, buffer)); | 328 &FFmpegAudioDecoder::DoDecodeBuffer, this, status, buffer)); |
328 } | 329 } |
329 | 330 |
330 base::TimeDelta FFmpegAudioDecoder::GetNextOutputTimestamp() const { | 331 base::TimeDelta FFmpegAudioDecoder::GetNextOutputTimestamp() const { |
331 DCHECK(output_timestamp_base_ != kNoTimestamp()); | 332 DCHECK(output_timestamp_base_ != kNoTimestamp()); |
332 double decoded_us = (total_frames_decoded_ / samples_per_second_) * | 333 double decoded_us = (total_frames_decoded_ / samples_per_second_) * |
333 base::Time::kMicrosecondsPerSecond; | 334 base::Time::kMicrosecondsPerSecond; |
334 return output_timestamp_base_ + base::TimeDelta::FromMicroseconds(decoded_us); | 335 return output_timestamp_base_ + base::TimeDelta::FromMicroseconds(decoded_us); |
335 } | 336 } |
336 } // namespace media | 337 } // namespace media |
OLD | NEW |