diff --git a/audio_fifo.go b/audio_fifo.go index 42068b0..c6cec4e 100644 --- a/audio_fifo.go +++ b/audio_fifo.go @@ -1,13 +1,13 @@ package astiav -//#cgo pkg-config: libavutil //#include //#include import "C" import "unsafe" +// https://github.com/FFmpeg/FFmpeg/blob/n7.0/libavutil/audio_fifo.c#L37 type AudioFifo struct { - c *C.struct_AVAudioFifo + c *C.AVAudioFifo } func newAudioFifoFromC(c *C.struct_AVAudioFifo) *AudioFifo { @@ -21,26 +21,33 @@ func AllocAudioFifo(sampleFmt SampleFormat, channels int, nbSamples int) *AudioF return newAudioFifoFromC(C.av_audio_fifo_alloc(C.enum_AVSampleFormat(sampleFmt), C.int(channels), C.int(nbSamples))) } -func (a *AudioFifo) AudioFifoRealloc(nbSamples int) int { - return int(C.av_audio_fifo_realloc((*C.struct_AVAudioFifo)(a.c), C.int(nbSamples))) +func (a *AudioFifo) Realloc(nbSamples int) error { + return newError(C.av_audio_fifo_realloc(a.c, C.int(nbSamples))) } -func (a *AudioFifo) AudioFifoSize() int { - return int(C.av_audio_fifo_size((*C.struct_AVAudioFifo)(a.c))) +func (a *AudioFifo) Size() int { + return int(C.av_audio_fifo_size(a.c)) } -func (a *AudioFifo) AudioFifoSpace() int { - return int(C.av_audio_fifo_space((*C.struct_AVAudioFifo)(a.c))) +func (a *AudioFifo) Space() int { + return int(C.av_audio_fifo_space(a.c)) } -func (a *AudioFifo) AudioFifoWrite(data **uint8, nbSamples int) int { - return int(C.av_audio_fifo_write((*C.struct_AVAudioFifo)(a.c), (*unsafe.Pointer)(unsafe.Pointer(data)), C.int(nbSamples))) +func (a *AudioFifo) Write(f *Frame) (int, error) { + //frameData := f.DataPtr() + frameRawData := (**uint8)(unsafe.Pointer(&f.c.data[0])) + ret := int(C.av_audio_fifo_write(a.c, (*unsafe.Pointer)(unsafe.Pointer(frameRawData)), C.int(f.NbSamples()))) + if ret < 0 { + return ret, newError(C.int(ret)) + } + return ret, nil } -func (a *AudioFifo) AudioFifoRead(data **uint8, nbSamples int) int { - return int(C.av_audio_fifo_read((*C.struct_AVAudioFifo)(a.c), (*unsafe.Pointer)(unsafe.Pointer(data)), C.int(nbSamples))) +func (a *AudioFifo) Read(f *Frame) int { + frameRawData := (**uint8)(unsafe.Pointer(&f.c.data[0])) + return int(C.av_audio_fifo_read(a.c, (*unsafe.Pointer)(unsafe.Pointer(frameRawData)), C.int(f.NbSamples()))) } -func (a *AudioFifo) AudioFifoFree() { - C.av_audio_fifo_free((*C.struct_AVAudioFifo)(a.c)) +func (a *AudioFifo) Free() { + C.av_audio_fifo_free(a.c) } diff --git a/audio_fifo_test.go b/audio_fifo_test.go index 6b7e29d..4ed1e1a 100644 --- a/audio_fifo_test.go +++ b/audio_fifo_test.go @@ -7,11 +7,11 @@ import ( ) func TestAudioFIFO(t *testing.T) { - audioFifo := AllocAudioFifo( + af := AllocAudioFifo( SampleFormatFltp, 2, 960) - + defer af.Free() writeSamples := 1024 readSamples := 120 writeFrame := AllocFrame() @@ -28,9 +28,17 @@ func TestAudioFIFO(t *testing.T) { readFrame.SetSampleRate(48000) readFrame.AllocBuffer(0) - written := audioFifo.AudioFifoWrite(writeFrame.DataPtr(), writeFrame.NbSamples()) + written, err := af.Write(writeFrame) + require.Equal(t, err, nil) require.Equal(t, writeSamples, written) - read := audioFifo.AudioFifoRead(readFrame.DataPtr(), readFrame.NbSamples()) + read := af.Read(readFrame) require.Equal(t, readSamples, read) - require.Equal(t, audioFifo.AudioFifoSize(), writeSamples-readSamples) + require.Equal(t, af.Size(), writeSamples-readSamples) + reallocSamples := 3000 + err = af.Realloc(reallocSamples) + require.Equal(t, err, nil) + expectedAfSize := writeSamples - readSamples + require.Equal(t, af.Space(), reallocSamples-expectedAfSize) + // It still has the same amount of data + require.Equal(t, af.Size(), expectedAfSize) }