audio_linux.go (1170B)
1 //go:build linux && cgo 2 3 package nativeaudio 4 5 import ( 6 "git.sr.ht/~jackmordaunt/nativeaudio/internal/libav" 7 ) 8 9 func start() error { 10 return libav.Startup() 11 } 12 13 func end() error { 14 return libav.Shutdown() 15 } 16 17 // openStream decodes incrementally through libav. 18 // 19 // Unlike the subprocess path this needs no temporary file: a custom 20 // AVIOContext gives the demuxer a seekable view over the bytes, which 21 // is what containers like mp4 want and what a pipe cannot offer. 22 func openStream(compressed []byte) (*Stream, error) { 23 ls, err := libav.Open(compressed) 24 if err != nil { 25 return nil, err 26 } 27 return adapt(ls), nil 28 } 29 30 // openStreamFile decodes the file incrementally, letting libav read it 31 // rather than buffering the compressed bytes first. 32 func openStreamFile(path string) (*Stream, error) { 33 ls, err := libav.OpenFile(path) 34 if err != nil { 35 return nil, err 36 } 37 return adapt(ls), nil 38 } 39 40 // adapt wraps a libav stream in the package's own Stream type. 41 func adapt(ls *libav.Stream) *Stream { 42 f := ls.Format() 43 return &Stream{ 44 r: ls, 45 format: Format{ 46 SampleRate: f.SampleRate, 47 Channels: f.Channels, 48 BytesPerSample: f.BytesPerSample, 49 }, 50 } 51 }