#include "FifoQueue.h" #include "ffcpp.h" #include namespace ffcpp { FifoQueue::FifoQueue(AVSampleFormat sampleFormat, int channels, int frameSize) { _frameSize = frameSize; _fifo = av_audio_fifo_alloc(sampleFormat, channels, 1); if(!_fifo) throw std::runtime_error("cannot create audio fifo queue"); } void FifoQueue::addSamples(const Frame &frame) { const AVFrame* frameImpl = frame; addSamples((void**)frameImpl->data, frameImpl->nb_samples); } void FifoQueue::addSamples(void **data, int samplesCount) { int res = av_audio_fifo_realloc(_fifo, av_audio_fifo_size(_fifo) + samplesCount); throwIfError(res, "cannot reallocate fifo queue"); res = av_audio_fifo_write(_fifo, data, samplesCount); throwIfError(res, "cannot add data from frame to fifo queue"); } bool FifoQueue::enoughSamples() const { return av_audio_fifo_size(_fifo) >= _frameSize; } void FifoQueue::readFrame(Frame& frame) { AVFrame* nativeFrame = frame; int res = av_audio_fifo_read(_fifo, (void**)nativeFrame->data, _frameSize); throwIfError(res, "cannot read data from fifo queue"); } }