ffconv/ffcpp/FifoQueue.cpp

35 lines
1.0 KiB
C++

#include "FifoQueue.h"
#include "ffcpp.h"
#include <stdexcept>
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) {
int res = av_audio_fifo_realloc(_fifo, av_audio_fifo_size(_fifo) + frame.samplesCount());
throwIfError(res, "cannot reallocate fifo queue");
const AVFrame* frameImpl = frame;
res = av_audio_fifo_write(_fifo, (void**)frameImpl->data, frame.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");
}
}