Skip to content

Commit

Permalink
applied review
Browse files Browse the repository at this point in the history
  • Loading branch information
hsnks100 committed Sep 5, 2024
1 parent 09efdfd commit 65b7f33
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
35 changes: 21 additions & 14 deletions audio_fifo.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package astiav

//#cgo pkg-config: libavutil
//#include <libavutil/audio_fifo.h>
//#include <stdlib.h>
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 {
Expand All @@ -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)
}
18 changes: 13 additions & 5 deletions audio_fifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

0 comments on commit 65b7f33

Please sign in to comment.