Skip to content

Commit

Permalink
Added io interrupter
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Mar 1, 2024
1 parent b5db8fb commit 6648662
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
19 changes: 4 additions & 15 deletions format_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ package astiav
//#cgo pkg-config: libavcodec libavformat
//#include <libavcodec/avcodec.h>
//#include <libavformat/avformat.h>
/*
int astiavInterruptCallback(void *ret)
{
return *((int*)ret);
}
AVIOInterruptCB astiavNewInterruptCallback(int *ret)
{
AVIOInterruptCB c = { astiavInterruptCallback, ret };
return c;
}
*/
import "C"
import (
"math"
Expand Down Expand Up @@ -95,10 +84,10 @@ func (fc *FormatContext) Flags() FormatContextFlags {
return FormatContextFlags(fc.c.flags)
}

func (fc *FormatContext) SetInterruptCallback() *int {
ret := 0
fc.c.interrupt_callback = C.astiavNewInterruptCallback((*C.int)(unsafe.Pointer(&ret)))
return &ret
func (fc *FormatContext) SetInterruptCallback() IOInterrupter {
i := newIOInterrupter()
fc.c.interrupt_callback = i.c
return i
}

func (fc *FormatContext) InputFormat() *InputFormat {
Expand Down
7 changes: 6 additions & 1 deletion format_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ func TestFormatContext(t *testing.T) {
require.NotNil(t, s3)
require.Equal(t, 1, s3.Index())

// TODO Test SetInterruptCallback
fc4 := AllocFormatContext()
require.NotNil(t, fc4)
defer fc4.Free()
fc4.SetInterruptCallback().Interrupt()
require.ErrorIs(t, fc4.OpenInput("testdata/video.mp4", nil, nil), ErrExit)

// TODO Test ReadFrame
// TODO Test SeekFrame
// TODO Test Flush
Expand Down
37 changes: 37 additions & 0 deletions io_interrupter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package astiav

//#cgo pkg-config: libavformat
//#include <libavformat/avio.h>
/*
int astiavInterruptCallback(void *ret)
{
return *((int*)ret);
}
AVIOInterruptCB astiavNewInterruptCallback(int *ret)
{
AVIOInterruptCB c = { astiavInterruptCallback, ret };
return c;
}
*/
import "C"

type IOInterrupter interface {
Interrupt()
}

var _ IOInterrupter = (*ioInterrupter)(nil)

type ioInterrupter struct {
c C.struct_AVIOInterruptCB
i C.int
}

func newIOInterrupter() *ioInterrupter {
cb := &ioInterrupter{}
cb.c = C.astiavNewInterruptCallback(&cb.i)
return cb
}

func (cb *ioInterrupter) Interrupt() {
cb.i = 1
}

0 comments on commit 6648662

Please sign in to comment.