Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	program.go
  • Loading branch information
oldma3095 committed Dec 18, 2024
2 parents dec5570 + ff05de2 commit 08409f4
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 35 deletions.
4 changes: 4 additions & 0 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.27.0

- make sure to call the `IOInterrupter`.`Free` method after using `NewIOInterrupter`

# v0.25.0

- `CodecParameters`.`CodecType` and `CodecParameters`.`SetCodecType` have been removed, use `CodecParameters`.`MediaType` and `CodecParameters`.`SetMediaType` instead
Expand Down
2 changes: 1 addition & 1 deletion astiav.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package astiav

//#cgo pkg-config: libavcodec libavdevice libavfilter libavformat libswscale libavutil
//#cgo pkg-config: libavcodec libavdevice libavfilter libavformat libswresample libswscale libavutil
import "C"
2 changes: 1 addition & 1 deletion format_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (fc *FormatContext) SetIOInterrupter(i *IOInterrupter) {
if i == nil {
fc.c.interrupt_callback = C.AVIOInterruptCB{}
} else {
fc.c.interrupt_callback = i.c
fc.c.interrupt_callback = *i.c
}
}

Expand Down
2 changes: 2 additions & 0 deletions format_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestFormatContext(t *testing.T) {
require.NotNil(t, fc4)
defer fc4.Free()
ii1 := NewIOInterrupter()
defer ii1.Free()
fc4.SetIOInterrupter(ii1)
ii1.Interrupt()
require.ErrorIs(t, fc4.OpenInput("testdata/video.mp4", nil, nil), ErrExit)
Expand All @@ -98,6 +99,7 @@ func TestFormatContext(t *testing.T) {
require.NotNil(t, fc9)
defer fc9.Free()
ii2 := NewIOInterrupter()
defer ii2.Free()
fc9.SetIOInterrupter(ii2)
fc9.SetIOInterrupter(nil)
require.NoError(t, fc4.OpenInput("testdata/video.mp4", nil, nil))
Expand Down
11 changes: 6 additions & 5 deletions io_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func OpenIOContext(filename string, flags IOContextFlags, ii *IOInterrupter, d *
}
var cii *C.AVIOInterruptCB = nil
if ii != nil {
cii = &ii.c
cii = ii.c
}
var c *C.AVIOContext
if err := newError(C.avio_open2(&c, cfi, C.int(flags), cii, dc)); err != nil {
Expand All @@ -137,14 +137,15 @@ func (ic *IOContext) Close() error {
// Make sure to clone the classer before freeing the object since
// the C free method may reset the pointer
c := newClonedClasser(ic)
if err := newError(C.avio_closep(&ic.c)); err != nil {
return err
}
// Error is returned when closing the url but pointer has been freed at this point
// therefore we must make sure classers are cleaned up properly even on error
err := newError(C.avio_closep(&ic.c))
// Make sure to remove from classers after freeing the object since
// the C free method may use methods needing the classer
if c != nil {
if c != nil && ic.c == nil {
classers.del(c)
}
return err
}
return nil
}
Expand Down
32 changes: 24 additions & 8 deletions io_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,33 @@ func TestIOContext(t *testing.T) {

func TestOpenIOContext(t *testing.T) {
path := filepath.Join(t.TempDir(), "iocontext.txt")
c, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), nil, nil)
c1, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), nil, nil)
require.NoError(t, err)
cl := c.Class()
defer os.RemoveAll(path)
cl := c1.Class()
require.NotNil(t, cl)
require.Equal(t, "AVIOContext", cl.Name())
c.Write(nil)
c.Write([]byte("test"))
require.NoError(t, c.Close())
b, err := os.ReadFile(path)
c1.Write(nil)
c1.Write([]byte("test"))
require.NoError(t, c1.Close())
b1, err := os.ReadFile(path)
require.NoError(t, err)
require.Equal(t, "test", string(b))
err = os.Remove(path)
require.Equal(t, "test", string(b1))

ii := NewIOInterrupter()
defer ii.Free()
c2, err := OpenIOContext(path, NewIOContextFlags(IOContextFlagRead), ii, nil)
require.NoError(t, err)
b2 := make([]byte, 10)
_, err = c2.Read(b2)
require.NoError(t, err)
ii.Interrupt()
_, err = c2.Read(b2)
require.ErrorIs(t, err, ErrExit)
require.ErrorIs(t, c2.Close(), ErrExit)

d := NewDictionary()
require.NoError(t, d.Set("protocol_whitelist", "rtp", NewDictionaryFlags()))
_, err = OpenIOContext(path, NewIOContextFlags(IOContextFlagWrite), nil, d)
require.Error(t, err)
}
7 changes: 5 additions & 2 deletions io_interrupter.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include <libavformat/avio.h>
#include <stdlib.h>

int astiavInterruptCallback(void *ret)
{
return *((int*)ret);
}

AVIOInterruptCB astiavNewInterruptCallback(int *ret)
AVIOInterruptCB* astiavNewInterruptCallback(int *ret)
{
AVIOInterruptCB c = { astiavInterruptCallback, ret };
AVIOInterruptCB* c = malloc(sizeof(AVIOInterruptCB));
c->callback = astiavInterruptCallback;
c->opaque = ret;
return c;
}
8 changes: 7 additions & 1 deletion io_interrupter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package astiav

//#include "io_interrupter.h"
//#include <stdlib.h>
import "C"
import "unsafe"

type IOInterrupter struct {
c C.AVIOInterruptCB
c *C.AVIOInterruptCB
i C.int
}

Expand All @@ -14,6 +16,10 @@ func NewIOInterrupter() *IOInterrupter {
return i
}

func (i *IOInterrupter) Free() {
C.free(unsafe.Pointer(i.c))
}

func (i *IOInterrupter) Interrupt() {
i.i = 1
}
Expand Down
2 changes: 1 addition & 1 deletion io_interrupter.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <libavformat/avio.h>

int astiavInterruptCallback(void *ret);
AVIOInterruptCB astiavNewInterruptCallback(int *ret);
AVIOInterruptCB* astiavNewInterruptCallback(int *ret);
1 change: 1 addition & 0 deletions io_interrupter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

func TestIOInterrupter(t *testing.T) {
ii := NewIOInterrupter()
defer ii.Free()
require.False(t, ii.Interrupted())
ii.Interrupt()
require.True(t, ii.Interrupted())
Expand Down
16 changes: 0 additions & 16 deletions program.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,6 @@ func (p *Program) SetDiscard(d Discard) {
p.c.discard = int32(d)
}

// https://ffmpeg.org/doxygen/7.0/structAVProgram.html#a7967d41af4812ed61a28762e988c7a02
func (p *Program) StreamIndex() *uint {
if p.c.stream_index == nil {
return nil
}
u := uint(C.uint(*p.c.stream_index))
return &u
}

// https://ffmpeg.org/doxygen/7.0/structAVProgram.html#a7967d41af4812ed61a28762e988c7a02
func (p *Program) SetStreamIndex(n uint) {
if p.c.stream_index != nil {
*p.c.stream_index = C.uint(n)
}
}

// https://ffmpeg.org/doxygen/7.0/structAVProgram.html#ae9dab38d4694e3da9cba0f882f4e43d3
func (p *Program) Metadata() *Dictionary {
return newDictionaryFromC(p.c.metadata)
Expand Down

0 comments on commit 08409f4

Please sign in to comment.