Chromium Code Reviews| Index: media/base/audio_push_fifo.cc |
| diff --git a/media/base/audio_push_fifo.cc b/media/base/audio_push_fifo.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bf72fed73ebb31400fd8e36670cd472e9b2423e7 |
| --- /dev/null |
| +++ b/media/base/audio_push_fifo.cc |
| @@ -0,0 +1,74 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/base/audio_push_fifo.h" |
| + |
| +#include <algorithm> |
| + |
| +namespace media { |
| + |
| +AudioPushFifo::AudioPushFifo(const OutputCallback& callback) |
| + : callback_(callback), output_frames_(0) { |
| + DCHECK(!callback_.is_null()); |
| +} |
| + |
| +AudioPushFifo::~AudioPushFifo() {} |
| + |
| +void AudioPushFifo::Reset(int frames_per_buffer) { |
| + DCHECK_GT(frames_per_buffer, 0); |
| + |
| + audio_queue_.reset(); |
|
xjz
2016/02/23 22:03:02
Is it possible that there are still pending packet
miu
2016/02/23 23:32:33
Yes. As Dale suggested, I've added a Flush() meth
|
| + queued_frames_ = 0; |
| + |
| + output_frames_ = frames_per_buffer; |
| +} |
| + |
| +void AudioPushFifo::Push(const AudioBus& input_bus) { |
| + DCHECK_GT(output_frames_, 0); |
| + |
| + // Fast path: No buffering required. |
| + if ((queued_frames_ == 0) && (input_bus.frames() == output_frames_)) { |
| + callback_.Run(input_bus, 0); |
| + return; |
| + } |
| + |
| + // Lazy-create the |audio_queue_| if needed. |
| + if (!audio_queue_ || audio_queue_->channels() != input_bus.channels()) |
|
DaleCurtis
2016/02/23 20:34:26
Do you want to document that channel changes will
miu
2016/02/23 23:32:33
Done. In header comments for Push() method.
|
| + audio_queue_ = AudioBus::Create(input_bus.channels(), output_frames_); |
| + |
| + // Start with a frame offset that refers to the position of the first sample |
| + // in |audio_queue_| relative to the first sample in |input_bus|. |
| + int frame_offset = -queued_frames_; |
| + |
| + // Repeatedly fill up |audio_queue_| with more sample frames from |input_bus| |
| + // and deliver batches until all sample frames in |input_bus| have been |
| + // consumed. |
| + int input_offset = 0; |
| + do { |
| + // Attempt to fill |audio_queue_| completely. |
| + const int frames_to_enqueue = |
| + std::min(static_cast<int>(input_bus.frames() - input_offset), |
| + output_frames_ - queued_frames_); |
| + if (frames_to_enqueue > 0) { |
| + DVLOG(2) << "Enqueuing " << frames_to_enqueue << " frames."; |
| + input_bus.CopyPartialFramesTo(input_offset, frames_to_enqueue, |
| + queued_frames_, audio_queue_.get()); |
| + queued_frames_ += frames_to_enqueue; |
| + input_offset += frames_to_enqueue; |
| + } |
| + |
| + // If |audio_queue_| has been filled completely, deliver the re-buffered |
| + // audio to the consumer. |
| + if (queued_frames_ == output_frames_) { |
| + DVLOG(2) << "Delivering another " << queued_frames_ << " frames."; |
| + callback_.Run(*audio_queue_, frame_offset); |
| + frame_offset += output_frames_; |
| + queued_frames_ = 0; |
| + } else { |
| + // Not enough frames queued-up yet to deliver more frames. |
| + } |
| + } while (input_offset < input_bus.frames()); |
| +} |
| + |
| +} // namespace media |