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
3 // found in the LICENSE file.
5 #include "media/base/audio_pull_fifo.h"
9 #include "base/logging.h"
10 #include "media/base/audio_bus.h"
14 AudioPullFifo::AudioPullFifo(int channels, int frames, const ReadCB& read_cb)
16 fifo_(AudioBus::Create(channels, frames)),
17 fifo_index_(frames) {}
19 AudioPullFifo::~AudioPullFifo() {}
21 void AudioPullFifo::Consume(AudioBus* destination, int frames_to_consume) {
22 DCHECK_LE(frames_to_consume, destination->frames());
24 int remaining_frames_to_provide = frames_to_consume;
26 // Try to fulfill the request using what's available in the FIFO.
27 int frames_read = ReadFromFifo(destination, remaining_frames_to_provide, 0);
28 int write_pos = frames_read;
29 remaining_frames_to_provide -= frames_read;
31 // Get the remaining audio frames from the producer using the callback.
32 while (remaining_frames_to_provide > 0) {
33 DCHECK_EQ(fifo_index_, fifo_->frames());
36 // Fill up the FIFO by acquiring audio data from the producer.
37 read_cb_.Run(write_pos, fifo_.get());
39 // Try to fulfill the request using what's available in the FIFO.
41 ReadFromFifo(destination, remaining_frames_to_provide, write_pos);
42 write_pos += frames_read;
43 remaining_frames_to_provide -= frames_read;
47 void AudioPullFifo::Clear() { fifo_index_ = fifo_->frames(); }
49 int AudioPullFifo::ReadFromFifo(AudioBus* destination,
50 int frames_to_provide,
52 int frames = std::min(frames_to_provide, fifo_->frames() - fifo_index_);
56 for (int ch = 0; ch < fifo_->channels(); ++ch) {
57 const float* src = fifo_->channel(ch) + fifo_index_;
58 float* dest = destination->channel(ch) + write_pos;
59 memcpy(dest, src, frames * sizeof(*src));
62 fifo_index_ += frames;