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" | 9 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 } | 108 } |
109 | 109 |
110 statistics_cb_ = statistics_cb; | 110 statistics_cb_ = statistics_cb; |
111 initialize_cb.Run(PIPELINE_OK); | 111 initialize_cb.Run(PIPELINE_OK); |
112 } | 112 } |
113 | 113 |
114 void FFmpegAudioDecoder::Read(const ReadCB& read_cb) { | 114 void FFmpegAudioDecoder::Read(const ReadCB& read_cb) { |
115 DCHECK(task_runner_->BelongsToCurrentThread()); | 115 DCHECK(task_runner_->BelongsToCurrentThread()); |
116 DCHECK(!read_cb.is_null()); | 116 DCHECK(!read_cb.is_null()); |
117 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | 117 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; |
118 DCHECK(stop_cb_.is_null()); | |
xhwang
2014/01/10 18:58:51
nit: check reset_cb as well?
| |
118 | 119 |
119 read_cb_ = BindToCurrentLoop(read_cb); | 120 read_cb_ = BindToCurrentLoop(read_cb); |
120 | 121 |
121 // If we don't have any queued audio from the last packet we decoded, ask for | 122 // If we don't have any queued audio from the last packet we decoded, ask for |
122 // more data from the demuxer to satisfy this read. | 123 // more data from the demuxer to satisfy this read. |
123 if (queued_audio_.empty()) { | 124 if (queued_audio_.empty()) { |
124 ReadFromDemuxerStream(); | 125 ReadFromDemuxerStream(); |
125 return; | 126 return; |
126 } | 127 } |
127 | 128 |
(...skipping 12 matching lines...) Expand all Loading... | |
140 return channel_layout_; | 141 return channel_layout_; |
141 } | 142 } |
142 | 143 |
143 int FFmpegAudioDecoder::samples_per_second() { | 144 int FFmpegAudioDecoder::samples_per_second() { |
144 DCHECK(task_runner_->BelongsToCurrentThread()); | 145 DCHECK(task_runner_->BelongsToCurrentThread()); |
145 return samples_per_second_; | 146 return samples_per_second_; |
146 } | 147 } |
147 | 148 |
148 void FFmpegAudioDecoder::Reset(const base::Closure& closure) { | 149 void FFmpegAudioDecoder::Reset(const base::Closure& closure) { |
149 DCHECK(task_runner_->BelongsToCurrentThread()); | 150 DCHECK(task_runner_->BelongsToCurrentThread()); |
150 base::Closure reset_cb = BindToCurrentLoop(closure); | 151 reset_cb_ = BindToCurrentLoop(closure); |
151 | 152 |
152 avcodec_flush_buffers(codec_context_.get()); | 153 if (read_cb_.is_null()) |
153 ResetTimestampState(); | 154 DoReset(); |
154 queued_audio_.clear(); | 155 // Otherwise a demuxer read is pending, we'll wait until it finishes. |
155 reset_cb.Run(); | |
156 } | 156 } |
157 | 157 |
158 FFmpegAudioDecoder::~FFmpegAudioDecoder() { | 158 void FFmpegAudioDecoder::Stop(const base::Closure& closure) { |
159 // TODO(scherkus): should we require Stop() to be called? this might end up | 159 DCHECK(task_runner_->BelongsToCurrentThread()); |
160 // getting called on a random thread due to refcounting. | 160 stop_cb_ = BindToCurrentLoop(closure); |
161 ReleaseFFmpegResources(); | 161 |
162 if (read_cb_.is_null()) | |
163 DoStop(); | |
xhwang
2014/01/10 18:58:51
What if we are calling Stop() during a pending res
| |
164 // Otherwise a demuxer read is pending, we'll wait until it finishes. | |
162 } | 165 } |
163 | 166 |
167 FFmpegAudioDecoder::~FFmpegAudioDecoder() {} | |
168 | |
164 int FFmpegAudioDecoder::GetAudioBuffer(AVCodecContext* codec, | 169 int FFmpegAudioDecoder::GetAudioBuffer(AVCodecContext* codec, |
165 AVFrame* frame, | 170 AVFrame* frame, |
166 int flags) { | 171 int flags) { |
167 // Since this routine is called by FFmpeg when a buffer is required for audio | 172 // Since this routine is called by FFmpeg when a buffer is required for audio |
168 // data, use the values supplied by FFmpeg (ignoring the current settings). | 173 // data, use the values supplied by FFmpeg (ignoring the current settings). |
169 // RunDecodeLoop() gets to determine if the buffer is useable or not. | 174 // RunDecodeLoop() gets to determine if the buffer is useable or not. |
170 AVSampleFormat format = static_cast<AVSampleFormat>(frame->format); | 175 AVSampleFormat format = static_cast<AVSampleFormat>(frame->format); |
171 SampleFormat sample_format = AVSampleFormatToSampleFormat(format); | 176 SampleFormat sample_format = AVSampleFormatToSampleFormat(format); |
172 int channels = DetermineChannels(frame); | 177 int channels = DetermineChannels(frame); |
173 if ((channels <= 0) || (channels >= limits::kMaxChannels)) { | 178 if ((channels <= 0) || (channels >= limits::kMaxChannels)) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 | 224 |
220 // Now create an AVBufferRef for the data just allocated. It will own the | 225 // Now create an AVBufferRef for the data just allocated. It will own the |
221 // reference to the AudioBuffer object. | 226 // reference to the AudioBuffer object. |
222 void* opaque = NULL; | 227 void* opaque = NULL; |
223 buffer.swap(reinterpret_cast<AudioBuffer**>(&opaque)); | 228 buffer.swap(reinterpret_cast<AudioBuffer**>(&opaque)); |
224 frame->buf[0] = av_buffer_create( | 229 frame->buf[0] = av_buffer_create( |
225 frame->data[0], buffer_size_in_bytes, ReleaseAudioBufferImpl, opaque, 0); | 230 frame->data[0], buffer_size_in_bytes, ReleaseAudioBufferImpl, opaque, 0); |
226 return 0; | 231 return 0; |
227 } | 232 } |
228 | 233 |
234 void FFmpegAudioDecoder::DoStop() { | |
235 DCHECK(task_runner_->BelongsToCurrentThread()); | |
236 DCHECK(!stop_cb_.is_null()); | |
237 | |
xhwang
2014/01/10 18:58:51
check that no read and no reset is pending when we
| |
238 ResetTimestampState(); | |
239 queued_audio_.clear(); | |
240 ReleaseFFmpegResources(); | |
241 base::ResetAndReturn(&stop_cb_).Run(); | |
242 } | |
243 | |
244 void FFmpegAudioDecoder::DoReset() { | |
245 DCHECK(task_runner_->BelongsToCurrentThread()); | |
246 DCHECK(!reset_cb_.is_null()); | |
xhwang
2014/01/10 18:58:51
check that no read is pending when we DoReset.
| |
247 | |
248 avcodec_flush_buffers(codec_context_.get()); | |
249 ResetTimestampState(); | |
250 queued_audio_.clear(); | |
251 base::ResetAndReturn(&reset_cb_).Run(); | |
252 } | |
253 | |
229 void FFmpegAudioDecoder::ReadFromDemuxerStream() { | 254 void FFmpegAudioDecoder::ReadFromDemuxerStream() { |
230 DCHECK(!read_cb_.is_null()); | 255 DCHECK(!read_cb_.is_null()); |
231 demuxer_stream_->Read(base::Bind( | 256 demuxer_stream_->Read(base::Bind( |
232 &FFmpegAudioDecoder::BufferReady, weak_this_)); | 257 &FFmpegAudioDecoder::BufferReady, weak_this_)); |
233 } | 258 } |
234 | 259 |
235 void FFmpegAudioDecoder::BufferReady( | 260 void FFmpegAudioDecoder::BufferReady( |
236 DemuxerStream::Status status, | 261 DemuxerStream::Status status, |
237 const scoped_refptr<DecoderBuffer>& input) { | 262 const scoped_refptr<DecoderBuffer>& input) { |
238 DCHECK(task_runner_->BelongsToCurrentThread()); | 263 DCHECK(task_runner_->BelongsToCurrentThread()); |
239 DCHECK(!read_cb_.is_null()); | 264 DCHECK(!read_cb_.is_null()); |
240 DCHECK(queued_audio_.empty()); | 265 DCHECK(queued_audio_.empty()); |
241 DCHECK_EQ(status != DemuxerStream::kOk, !input.get()) << status; | 266 DCHECK_EQ(status != DemuxerStream::kOk, !input.get()) << status; |
242 | 267 |
268 // Stop() is pending, we'll ignore the buffer we just got, fire read_cb_, and | |
269 // complete the Stop. | |
270 if (!stop_cb_.is_null()) { | |
271 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
272 DoStop(); | |
273 return; | |
274 } | |
275 | |
276 // Reset() is pending, we'll ignore the buffer we just got, fire read_cb_, and | |
277 // complete the Reset. | |
278 if (!reset_cb_.is_null()) { | |
279 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
280 DoReset(); | |
281 return; | |
282 } | |
283 | |
xhwang
2014/01/10 18:58:51
If Stop() is called during pending reset, we shoul
| |
243 if (status == DemuxerStream::kAborted) { | 284 if (status == DemuxerStream::kAborted) { |
244 DCHECK(!input.get()); | 285 DCHECK(!input.get()); |
245 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 286 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); |
246 return; | 287 return; |
247 } | 288 } |
248 | 289 |
249 if (status == DemuxerStream::kConfigChanged) { | 290 if (status == DemuxerStream::kConfigChanged) { |
250 DCHECK(!input.get()); | 291 DCHECK(!input.get()); |
251 | 292 |
252 // Send a "end of stream" buffer to the decode loop | 293 // Send a "end of stream" buffer to the decode loop |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 // Decoding finished successfully, update statistics. | 573 // Decoding finished successfully, update statistics. |
533 if (result > 0) { | 574 if (result > 0) { |
534 PipelineStatistics statistics; | 575 PipelineStatistics statistics; |
535 statistics.audio_bytes_decoded = result; | 576 statistics.audio_bytes_decoded = result; |
536 statistics_cb_.Run(statistics); | 577 statistics_cb_.Run(statistics); |
537 } | 578 } |
538 } while (packet.size > 0); | 579 } while (packet.size > 0); |
539 } | 580 } |
540 | 581 |
541 } // namespace media | 582 } // namespace media |
OLD | NEW |