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()); | |
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(); | |
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(!stop_cb_.is_null()); | |
236 | |
237 ResetTimestampState(); | |
238 queued_audio_.clear(); | |
239 ReleaseFFmpegResources(); | |
240 base::ResetAndReturn(&stop_cb_).Run(); | |
241 } | |
242 | |
243 void FFmpegAudioDecoder::DoReset() { | |
244 DCHECK(!reset_cb_.is_null()); | |
245 | |
246 avcodec_flush_buffers(codec_context_.get()); | |
247 ResetTimestampState(); | |
248 queued_audio_.clear(); | |
249 base::ResetAndReturn(&reset_cb_).Run(); | |
250 } | |
251 | |
229 void FFmpegAudioDecoder::ReadFromDemuxerStream() { | 252 void FFmpegAudioDecoder::ReadFromDemuxerStream() { |
230 DCHECK(!read_cb_.is_null()); | 253 DCHECK(!read_cb_.is_null()); |
231 demuxer_stream_->Read(base::Bind( | 254 demuxer_stream_->Read(base::Bind( |
232 &FFmpegAudioDecoder::BufferReady, weak_this_)); | 255 &FFmpegAudioDecoder::BufferReady, weak_this_)); |
233 } | 256 } |
234 | 257 |
235 void FFmpegAudioDecoder::BufferReady( | 258 void FFmpegAudioDecoder::BufferReady( |
236 DemuxerStream::Status status, | 259 DemuxerStream::Status status, |
237 const scoped_refptr<DecoderBuffer>& input) { | 260 const scoped_refptr<DecoderBuffer>& input) { |
238 DCHECK(task_runner_->BelongsToCurrentThread()); | 261 DCHECK(task_runner_->BelongsToCurrentThread()); |
239 DCHECK(!read_cb_.is_null()); | 262 DCHECK(!read_cb_.is_null()); |
240 DCHECK(queued_audio_.empty()); | 263 DCHECK(queued_audio_.empty()); |
241 DCHECK_EQ(status != DemuxerStream::kOk, !input.get()) << status; | 264 DCHECK_EQ(status != DemuxerStream::kOk, !input.get()) << status; |
242 | 265 |
266 // Stop() is pending, we'll ignore the buffer we just got, fire read_cb_, and | |
267 // complete the Stop. | |
268 if (!stop_cb_.is_null()) { | |
269 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
270 DoStop(); | |
271 return; | |
272 } | |
273 | |
274 // Reset() is pending, we'll ignore the buffer we just got, fire read_cb_, and | |
DaleCurtis
2014/01/09 23:04:21
Not sure this is necessary, but should be fine.
rileya (GONE FROM CHROMIUM)
2014/01/10 00:04:05
Yeah, I wasn't quite sure. DecryptingAudioDecoder
| |
275 // complete the Reset. | |
276 if (!reset_cb_.is_null()) { | |
277 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
278 DoReset(); | |
279 return; | |
280 } | |
281 | |
243 if (status == DemuxerStream::kAborted) { | 282 if (status == DemuxerStream::kAborted) { |
244 DCHECK(!input.get()); | 283 DCHECK(!input.get()); |
245 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 284 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); |
246 return; | 285 return; |
247 } | 286 } |
248 | 287 |
249 if (status == DemuxerStream::kConfigChanged) { | 288 if (status == DemuxerStream::kConfigChanged) { |
250 DCHECK(!input.get()); | 289 DCHECK(!input.get()); |
251 | 290 |
252 // Send a "end of stream" buffer to the decode loop | 291 // 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. | 571 // Decoding finished successfully, update statistics. |
533 if (result > 0) { | 572 if (result > 0) { |
534 PipelineStatistics statistics; | 573 PipelineStatistics statistics; |
535 statistics.audio_bytes_decoded = result; | 574 statistics.audio_bytes_decoded = result; |
536 statistics_cb_.Run(statistics); | 575 statistics_cb_.Run(statistics); |
537 } | 576 } |
538 } while (packet.size > 0); | 577 } while (packet.size > 0); |
539 } | 578 } |
540 | 579 |
541 } // namespace media | 580 } // namespace media |
OLD | NEW |