Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: media/filters/ffmpeg_audio_decoder.cc

Issue 126793002: Add Stop() to AudioDecoder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Handle stop with pending demuxer read Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/filters/ffmpeg_audio_decoder.h ('k') | media/filters/opus_audio_decoder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 118
119 read_cb_ = BindToCurrentLoop(read_cb); 119 read_cb_ = BindToCurrentLoop(read_cb);
120 120
121 // Don't allow further reads after Stop().
DaleCurtis 2014/01/09 21:10:42 Since the renderer / AudioBufferStream will invoke
122 if (!stop_cb_.is_null()) {
123 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
124 return;
125 }
126
121 // If we don't have any queued audio from the last packet we decoded, ask for 127 // 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. 128 // more data from the demuxer to satisfy this read.
123 if (queued_audio_.empty()) { 129 if (queued_audio_.empty()) {
124 ReadFromDemuxerStream(); 130 ReadFromDemuxerStream();
125 return; 131 return;
126 } 132 }
127 133
128 base::ResetAndReturn(&read_cb_).Run( 134 base::ResetAndReturn(&read_cb_).Run(
129 queued_audio_.front().status, queued_audio_.front().buffer); 135 queued_audio_.front().status, queued_audio_.front().buffer);
130 queued_audio_.pop_front(); 136 queued_audio_.pop_front();
(...skipping 17 matching lines...) Expand all
148 void FFmpegAudioDecoder::Reset(const base::Closure& closure) { 154 void FFmpegAudioDecoder::Reset(const base::Closure& closure) {
149 DCHECK(task_runner_->BelongsToCurrentThread()); 155 DCHECK(task_runner_->BelongsToCurrentThread());
150 base::Closure reset_cb = BindToCurrentLoop(closure); 156 base::Closure reset_cb = BindToCurrentLoop(closure);
151 157
152 avcodec_flush_buffers(codec_context_.get()); 158 avcodec_flush_buffers(codec_context_.get());
153 ResetTimestampState(); 159 ResetTimestampState();
154 queued_audio_.clear(); 160 queued_audio_.clear();
155 reset_cb.Run(); 161 reset_cb.Run();
156 } 162 }
157 163
158 FFmpegAudioDecoder::~FFmpegAudioDecoder() { 164 void FFmpegAudioDecoder::Stop(const base::Closure& closure) {
159 // TODO(scherkus): should we require Stop() to be called? this might end up 165 DCHECK(task_runner_->BelongsToCurrentThread());
160 // getting called on a random thread due to refcounting. 166 stop_cb_ = BindToCurrentLoop(closure);
161 ReleaseFFmpegResources(); 167
168 if (read_cb_.is_null()) {
DaleCurtis 2014/01/09 21:10:42 {} is not necessary on single line conditionals.
169 DoStop();
170 }
171 // Otherwise a demuxer read is pending, we'll wait until it finishes.
162 } 172 }
163 173
174 FFmpegAudioDecoder::~FFmpegAudioDecoder() {}
175
164 int FFmpegAudioDecoder::GetAudioBuffer(AVCodecContext* codec, 176 int FFmpegAudioDecoder::GetAudioBuffer(AVCodecContext* codec,
165 AVFrame* frame, 177 AVFrame* frame,
166 int flags) { 178 int flags) {
167 // Since this routine is called by FFmpeg when a buffer is required for audio 179 // 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). 180 // data, use the values supplied by FFmpeg (ignoring the current settings).
169 // RunDecodeLoop() gets to determine if the buffer is useable or not. 181 // RunDecodeLoop() gets to determine if the buffer is useable or not.
170 AVSampleFormat format = static_cast<AVSampleFormat>(frame->format); 182 AVSampleFormat format = static_cast<AVSampleFormat>(frame->format);
171 SampleFormat sample_format = AVSampleFormatToSampleFormat(format); 183 SampleFormat sample_format = AVSampleFormatToSampleFormat(format);
172 int channels = DetermineChannels(frame); 184 int channels = DetermineChannels(frame);
173 if ((channels <= 0) || (channels >= limits::kMaxChannels)) { 185 if ((channels <= 0) || (channels >= limits::kMaxChannels)) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 231
220 // Now create an AVBufferRef for the data just allocated. It will own the 232 // Now create an AVBufferRef for the data just allocated. It will own the
221 // reference to the AudioBuffer object. 233 // reference to the AudioBuffer object.
222 void* opaque = NULL; 234 void* opaque = NULL;
223 buffer.swap(reinterpret_cast<AudioBuffer**>(&opaque)); 235 buffer.swap(reinterpret_cast<AudioBuffer**>(&opaque));
224 frame->buf[0] = av_buffer_create( 236 frame->buf[0] = av_buffer_create(
225 frame->data[0], buffer_size_in_bytes, ReleaseAudioBufferImpl, opaque, 0); 237 frame->data[0], buffer_size_in_bytes, ReleaseAudioBufferImpl, opaque, 0);
226 return 0; 238 return 0;
227 } 239 }
228 240
241 void FFmpegAudioDecoder::DoStop() {
242 DCHECK(!stop_cb_.is_null());
243
244 ResetTimestampState();
245 queued_audio_.clear();
246 ReleaseFFmpegResources();
247 base::ResetAndReturn(&stop_cb_).Run();
248 }
249
229 void FFmpegAudioDecoder::ReadFromDemuxerStream() { 250 void FFmpegAudioDecoder::ReadFromDemuxerStream() {
230 DCHECK(!read_cb_.is_null()); 251 DCHECK(!read_cb_.is_null());
231 demuxer_stream_->Read(base::Bind( 252 demuxer_stream_->Read(base::Bind(
232 &FFmpegAudioDecoder::BufferReady, weak_this_)); 253 &FFmpegAudioDecoder::BufferReady, weak_this_));
233 } 254 }
234 255
235 void FFmpegAudioDecoder::BufferReady( 256 void FFmpegAudioDecoder::BufferReady(
236 DemuxerStream::Status status, 257 DemuxerStream::Status status,
237 const scoped_refptr<DecoderBuffer>& input) { 258 const scoped_refptr<DecoderBuffer>& input) {
238 DCHECK(task_runner_->BelongsToCurrentThread()); 259 DCHECK(task_runner_->BelongsToCurrentThread());
239 DCHECK(!read_cb_.is_null()); 260 DCHECK(!read_cb_.is_null());
240 DCHECK(queued_audio_.empty()); 261 DCHECK(queued_audio_.empty());
241 DCHECK_EQ(status != DemuxerStream::kOk, !input.get()) << status; 262 DCHECK_EQ(status != DemuxerStream::kOk, !input.get()) << status;
242 263
264 // Stop() is pending, we'll ignore the buffer we just got, fire read_cb_, and
265 // complete the Stop.
266 if (!stop_cb_.is_null()) {
267 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
268 DoStop();
269 return;
270 }
271
243 if (status == DemuxerStream::kAborted) { 272 if (status == DemuxerStream::kAborted) {
244 DCHECK(!input.get()); 273 DCHECK(!input.get());
245 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); 274 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
246 return; 275 return;
247 } 276 }
248 277
249 if (status == DemuxerStream::kConfigChanged) { 278 if (status == DemuxerStream::kConfigChanged) {
250 DCHECK(!input.get()); 279 DCHECK(!input.get());
251 280
252 // Send a "end of stream" buffer to the decode loop 281 // Send a "end of stream" buffer to the decode loop
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 // Decoding finished successfully, update statistics. 561 // Decoding finished successfully, update statistics.
533 if (result > 0) { 562 if (result > 0) {
534 PipelineStatistics statistics; 563 PipelineStatistics statistics;
535 statistics.audio_bytes_decoded = result; 564 statistics.audio_bytes_decoded = result;
536 statistics_cb_.Run(statistics); 565 statistics_cb_.Run(statistics);
537 } 566 }
538 } while (packet.size > 0); 567 } while (packet.size > 0);
539 } 568 }
540 569
541 } // namespace media 570 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/ffmpeg_audio_decoder.h ('k') | media/filters/opus_audio_decoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698