Skip to content

Commit

Permalink
IOInterrupterCB
Browse files Browse the repository at this point in the history
  • Loading branch information
oldma3095 committed Dec 12, 2024
1 parent 27d143e commit e4f6abc
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), nil)
ic1, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), nil, nil)
require.NoError(t, err)
defer os.RemoveAll(path)
ic2, err := AllocIOContext(1, true, nil, nil, nil)
Expand Down
2 changes: 1 addition & 1 deletion examples/remuxing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), nil)
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))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/transcoding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), nil); 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
}
Expand Down
4 changes: 2 additions & 2 deletions format_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), nil)
io, err := OpenIOContext("testdata/video.mp4", NewIOContextFlags(IOContextFlagRead), nil, nil)
require.NoError(t, err)
defer io.Close() //nolint:errcheck
fc3.SetPb(io)
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestFormatContext(t *testing.T) {
require.NotNil(t, os)
require.NoError(t, is.CodecParameters().Copy(os.CodecParameters()))
}
ic, err := OpenIOContext(outputPath, NewIOContextFlags(IOContextFlagWrite), nil)
ic, err := OpenIOContext(outputPath, NewIOContextFlags(IOContextFlagWrite), nil, nil)
require.NoError(t, err)
fc7.SetPb(ic)
require.NoError(t, fc7.WriteHeader(nil))
Expand Down
8 changes: 6 additions & 2 deletions io_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,19 @@ func AllocIOContext(bufferSize int, writable bool, readFunc IOContextReadFunc, s
}

// https://ffmpeg.org/doxygen/7.0/avio_8c.html#ae8589aae955d16ca228b6b9d66ced33d
func OpenIOContext(filename string, flags IOContextFlags, d *Dictionary) (*IOContext, error) {
func OpenIOContext(filename string, flags IOContextFlags, cb *IOInterrupterCB, 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 cb != nil {
cii = &cb.c
}
var c *C.AVIOContext
if err := newError(C.avio_open2(&c, cfi, C.int(flags), nil, dc)); err != nil {
if err := newError(C.avio_open2(&c, cfi, C.int(flags), cii, dc)); err != nil {
return nil, err
}
return newIOContextFromC(c), nil
Expand Down
4 changes: 1 addition & 3 deletions io_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func TestIOContext(t *testing.T) {

func TestOpenIOContext(t *testing.T) {
path := filepath.Join(t.TempDir(), "iocontext.txt")
d := NewDictionary()
_ = d.Set("k", "v", 0)
c, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), d)
c, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), nil, nil)
require.NoError(t, err)
cl := c.Class()
require.NotNil(t, cl)
Expand Down
9 changes: 9 additions & 0 deletions io_interrupter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import "C"
type IOInterrupter interface {
Interrupt()
Resume()
CB() *IOInterrupterCB
}

type IOInterrupterCB struct {
c C.AVIOInterruptCB
}

type defaultIOInterrupter struct {
Expand All @@ -26,3 +31,7 @@ func (i *defaultIOInterrupter) Interrupt() {
func (i *defaultIOInterrupter) Resume() {
i.i = 0
}

func (i *defaultIOInterrupter) CB() *IOInterrupterCB {
return &IOInterrupterCB{c: i.c}
}

0 comments on commit e4f6abc

Please sign in to comment.