36 lines
1.1 KiB
C++
36 lines
1.1 KiB
C++
#include "ffcpp/FifoQueue.h"
|
|
#include "ffcpp/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(FramePtr frame) {
|
|
addSamples((void**)frame->nativePtr()->data, frame->samplesCount());
|
|
}
|
|
|
|
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(FramePtr frame) {
|
|
int res = av_audio_fifo_read(_fifo, (void**)frame->nativePtr()->data, _frameSize);
|
|
throwIfError(res, "cannot read data from fifo queue");
|
|
}
|
|
|
|
}
|