Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for io.EOF to C.AVERROR_EOF mapping #73

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions io_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "C"
import (
"errors"
"fmt"
"io"
"sync"
"unsafe"
)
Expand Down Expand Up @@ -241,6 +242,8 @@ func goAstiavIOContextReadFunc(opaque unsafe.Pointer, buf *C.uint8_t, bufSize C.
var e Error
if errors.As(err, &e) {
return C.int(e)
} else if errors.Is(err, io.EOF) {
return C.AVERROR_EOF
}
return C.AVERROR_UNKNOWN
}
Expand Down
69 changes: 42 additions & 27 deletions io_context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package astiav

import (
"io"
"os"
"path/filepath"
"testing"
Expand All @@ -9,34 +10,48 @@ import (
)

func TestIOContext(t *testing.T) {
var seeked bool
rb := []byte("read")
wb := []byte("write")
var written []byte
c, err := AllocIOContext(8, true, func(b []byte) (int, error) {
copy(b, rb)
return len(rb), nil
}, func(offset int64, whence int) (n int64, err error) {
seeked = true
return offset, nil
}, func(b []byte) (int, error) {
written = make([]byte, len(b))
copy(written, b)
return len(b), nil
t.Run("read write seek", func(t *testing.T) {
var seeked bool
rb := []byte("read")
wb := []byte("write")
var written []byte
c, err := AllocIOContext(8, true, func(b []byte) (int, error) {
copy(b, rb)
return len(rb), nil
}, func(offset int64, whence int) (n int64, err error) {
seeked = true
return offset, nil
}, func(b []byte) (int, error) {
written = make([]byte, len(b))
copy(written, b)
return len(b), nil
})
require.NoError(t, err)
defer c.Free()
b := make([]byte, 6)
n, err := c.Read(b)
require.NoError(t, err)
require.Equal(t, 4, n)
require.Equal(t, rb, b[:n])
_, err = c.Seek(2, 0)
require.NoError(t, err)
require.True(t, seeked)
c.Write(wb)
c.Flush()
require.Equal(t, wb, written)
})

t.Run("io.EOF is mapped to AVERROR_EOF when reading", func(t *testing.T) {
c, err := AllocIOContext(8, false, func(b []byte) (int, error) {
return 0, io.EOF
}, nil, nil)
require.NoError(t, err)
defer c.Free()
b := make([]byte, 100)
n, err := c.Read(b)
require.ErrorIs(t, err, ErrEof)
require.Equal(t, 0, n)
})
require.NoError(t, err)
defer c.Free()
b := make([]byte, 6)
n, err := c.Read(b)
require.NoError(t, err)
require.Equal(t, 4, n)
require.Equal(t, rb, b[:n])
_, err = c.Seek(2, 0)
require.NoError(t, err)
require.True(t, seeked)
c.Write(wb)
c.Flush()
require.Equal(t, wb, written)
}

func TestOpenIOContext(t *testing.T) {
Expand Down