From f8f8f0e7ee6c75642e0c6fe91c224dbaaab80b6b Mon Sep 17 00:00:00 2001 From: oldma3095 <43288861+oldma3095@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:26:30 +0800 Subject: [PATCH] OpenIOContextWithDictionary (#116) * OpenIOContextWithDictionary * OpenIOContext * OpenIOContext * IOInterrupterCB * OpenIOContext(filename string, flags IOContextFlags, ii *IOInterrupter, d *Dictionary) --- class_test.go | 2 +- examples/remuxing/main.go | 2 +- examples/transcoding/main.go | 2 +- format_context_test.go | 4 ++-- io_context.go | 14 +++++++++++--- io_context_test.go | 2 +- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/class_test.go b/class_test.go index d462407..3b3f320 100644 --- a/class_test.go +++ b/class_test.go @@ -48,7 +48,7 @@ func TestClassers(t *testing.T) { fmc2 := AllocFormatContext() require.NoError(t, fmc2.OpenInput("testdata/video.mp4", nil, nil)) path := filepath.Join(t.TempDir(), "iocontext.txt") - ic1, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite)) + ic1, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), nil, nil) require.NoError(t, err) defer os.RemoveAll(path) ic2, err := AllocIOContext(1, true, nil, nil, nil) diff --git a/examples/remuxing/main.go b/examples/remuxing/main.go index 9ea9713..eaed6bf 100644 --- a/examples/remuxing/main.go +++ b/examples/remuxing/main.go @@ -103,7 +103,7 @@ func main() { // If this is a file, we need to use an io context if !outputFormatContext.OutputFormat().Flags().Has(astiav.IOFormatFlagNofile) { // Open io context - ioContext, err := astiav.OpenIOContext(*output, astiav.NewIOContextFlags(astiav.IOContextFlagWrite)) + ioContext, err := astiav.OpenIOContext(*output, astiav.NewIOContextFlags(astiav.IOContextFlagWrite), nil, nil) if err != nil { log.Fatal(fmt.Errorf("main: opening io context failed: %w", err)) } diff --git a/examples/transcoding/main.go b/examples/transcoding/main.go index 3bf897d..aa805cb 100644 --- a/examples/transcoding/main.go +++ b/examples/transcoding/main.go @@ -334,7 +334,7 @@ func openOutputFile() (err error) { if !outputFormatContext.OutputFormat().Flags().Has(astiav.IOFormatFlagNofile) { // Open io context var ioContext *astiav.IOContext - if ioContext, err = astiav.OpenIOContext(*output, astiav.NewIOContextFlags(astiav.IOContextFlagWrite)); err != nil { + if ioContext, err = astiav.OpenIOContext(*output, astiav.NewIOContextFlags(astiav.IOContextFlagWrite), nil, nil); err != nil { err = fmt.Errorf("main: opening io context failed: %w", err) return } diff --git a/format_context_test.go b/format_context_test.go index 2effac9..445d742 100644 --- a/format_context_test.go +++ b/format_context_test.go @@ -61,7 +61,7 @@ func TestFormatContext(t *testing.T) { fc3 := AllocFormatContext() require.NotNil(t, fc3) defer fc3.Free() - io, err := OpenIOContext("testdata/video.mp4", NewIOContextFlags(IOContextFlagRead)) + io, err := OpenIOContext("testdata/video.mp4", NewIOContextFlags(IOContextFlagRead), nil, nil) require.NoError(t, err) defer io.Close() //nolint:errcheck fc3.SetPb(io) @@ -140,7 +140,7 @@ func TestFormatContext(t *testing.T) { require.NotNil(t, os) require.NoError(t, is.CodecParameters().Copy(os.CodecParameters())) } - ic, err := OpenIOContext(outputPath, NewIOContextFlags(IOContextFlagWrite)) + ic, err := OpenIOContext(outputPath, NewIOContextFlags(IOContextFlagWrite), nil, nil) require.NoError(t, err) fc7.SetPb(ic) require.NoError(t, fc7.WriteHeader(nil)) diff --git a/io_context.go b/io_context.go index 710d932..063e95d 100644 --- a/io_context.go +++ b/io_context.go @@ -108,12 +108,20 @@ func AllocIOContext(bufferSize int, writable bool, readFunc IOContextReadFunc, s return } -// https://ffmpeg.org/doxygen/7.0/avio_8c.html#ab1b99c5b70aa59f15ab7cd4cbb40381e -func OpenIOContext(filename string, flags IOContextFlags) (*IOContext, error) { +// https://ffmpeg.org/doxygen/7.0/avio_8c.html#ae8589aae955d16ca228b6b9d66ced33d +func OpenIOContext(filename string, flags IOContextFlags, ii *IOInterrupter, d *Dictionary) (*IOContext, error) { cfi := C.CString(filename) defer C.free(unsafe.Pointer(cfi)) + var dc **C.AVDictionary + if d != nil { + dc = &d.c + } + var cii *C.AVIOInterruptCB = nil + if ii != nil { + cii = &ii.c + } var c *C.AVIOContext - if err := newError(C.avio_open(&c, cfi, C.int(flags))); err != nil { + if err := newError(C.avio_open2(&c, cfi, C.int(flags), cii, dc)); err != nil { return nil, err } return newIOContextFromC(c), nil diff --git a/io_context_test.go b/io_context_test.go index 09d4576..3a8b269 100644 --- a/io_context_test.go +++ b/io_context_test.go @@ -56,7 +56,7 @@ func TestIOContext(t *testing.T) { func TestOpenIOContext(t *testing.T) { path := filepath.Join(t.TempDir(), "iocontext.txt") - c, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite)) + c, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), nil, nil) require.NoError(t, err) cl := c.Class() require.NotNil(t, cl)