This source file includes following definitions.
- GetSizes
- UpdatePos
- write_pos_
- frames
- Push
- Consume
- Clear
#include "media/base/audio_fifo.h"
#include "base/logging.h"
using base::subtle::Atomic32;
using base::subtle::NoBarrier_Store;
namespace media {
static void GetSizes(
int pos, int max_size, int in_size, int* size, int* wrap_size) {
if (pos + in_size > max_size) {
*size = max_size - pos;
*wrap_size = in_size - *size;
} else {
*size = in_size;
*wrap_size = 0;
}
}
static int UpdatePos(int pos, int step, int max_size) {
return ((pos + step) % max_size);
}
AudioFifo::AudioFifo(int channels, int frames)
: audio_bus_(AudioBus::Create(channels, frames)),
max_frames_(frames),
frames_pushed_(0),
frames_consumed_(0),
read_pos_(0),
write_pos_(0) {}
AudioFifo::~AudioFifo() {}
int AudioFifo::frames() const {
int delta = frames_pushed_ - frames_consumed_;
base::subtle::MemoryBarrier();
return delta;
}
void AudioFifo::Push(const AudioBus* source) {
DCHECK(source);
DCHECK_EQ(source->channels(), audio_bus_->channels());
const int source_size = source->frames();
CHECK_LE(source_size + frames(), max_frames_);
int append_size = 0;
int wrap_size = 0;
GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size);
for (int ch = 0; ch < source->channels(); ++ch) {
float* dest = audio_bus_->channel(ch);
const float* src = source->channel(ch);
memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0]));
if (wrap_size > 0) {
memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0]));
}
}
base::subtle::MemoryBarrier();
Atomic32 new_frames_pushed = frames_pushed_ + source_size;
NoBarrier_Store(&frames_pushed_, new_frames_pushed);
DCHECK_LE(frames(), max_frames());
write_pos_ = UpdatePos(write_pos_, source_size, max_frames());
}
void AudioFifo::Consume(AudioBus* destination,
int start_frame,
int frames_to_consume) {
DCHECK(destination);
DCHECK_EQ(destination->channels(), audio_bus_->channels());
CHECK_LE(frames_to_consume, frames());
CHECK_LE(frames_to_consume + start_frame, destination->frames());
int consume_size = 0;
int wrap_size = 0;
GetSizes(read_pos_, max_frames(), frames_to_consume,
&consume_size, &wrap_size);
for (int ch = 0; ch < destination->channels(); ++ch) {
float* dest = destination->channel(ch);
const float* src = audio_bus_->channel(ch);
memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0]));
if (wrap_size > 0) {
memcpy(&dest[consume_size + start_frame], &src[0],
wrap_size * sizeof(src[0]));
}
}
Atomic32 new_frames_consumed = frames_consumed_ + frames_to_consume;
NoBarrier_Store(&frames_consumed_, new_frames_consumed);
read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames());
}
void AudioFifo::Clear() {
frames_pushed_ = 0;
frames_consumed_ = 0;
read_pos_ = 0;
write_pos_ = 0;
}
}