Fixed more channel layout functions when nil

This commit is contained in:
Quentin Renard
2024-09-19 15:47:15 +02:00
parent e85989bf5e
commit 96484ce883
2 changed files with 23 additions and 10 deletions
+10 -2
View File
@@ -3,7 +3,6 @@ package astiav
//#include "channel_layout.h"
import "C"
import (
"errors"
"unsafe"
)
@@ -56,6 +55,9 @@ func newChannelLayoutFromC(c *C.AVChannelLayout) ChannelLayout {
}
func (l ChannelLayout) Channels() int {
if l.c == nil {
return 0
}
return int(l.c.nb_channels)
}
@@ -70,7 +72,7 @@ func (l ChannelLayout) String() string {
func (l ChannelLayout) Describe(b []byte) (int, error) {
if l.c == nil {
return 0, errors.New("astiav: channel layout is nil")
return 0, nil
}
ret := C.av_channel_layout_describe(l.c, (*C.char)(unsafe.Pointer(&b[0])), C.size_t(len(b)))
if err := newError(ret); err != nil {
@@ -83,10 +85,16 @@ func (l ChannelLayout) Describe(b []byte) (int, error) {
}
func (l ChannelLayout) Valid() bool {
if l.c == nil {
return false
}
return C.av_channel_layout_check(l.c) > 0
}
func (l ChannelLayout) Compare(l2 ChannelLayout) (equal bool, err error) {
if l.c == nil || l2.c == nil {
return l.c == nil && l2.c == nil, nil
}
ret := C.av_channel_layout_compare(l.c, l2.c)
if err := newError(ret); err != nil {
return false, err
+13 -8
View File
@@ -7,12 +7,17 @@ import (
)
func TestChannelLayout(t *testing.T) {
cl := ChannelLayoutStereo
require.Equal(t, 2, cl.Channels())
require.Equal(t, "stereo", cl.String())
require.True(t, cl.Valid())
require.True(t, cl.Equal(ChannelLayoutStereo))
require.False(t, cl.Equal(ChannelLayoutMono))
cl = ChannelLayout{}
require.Equal(t, "", cl.String())
cl1 := ChannelLayoutStereo
require.Equal(t, 2, cl1.Channels())
require.Equal(t, "stereo", cl1.String())
require.True(t, cl1.Valid())
require.True(t, cl1.Equal(ChannelLayoutStereo))
require.False(t, cl1.Equal(ChannelLayoutMono))
cl2 := ChannelLayout{}
require.Equal(t, 0, cl2.Channels())
require.False(t, cl2.Valid())
require.Equal(t, "", cl2.String())
require.False(t, cl1.Equal(cl2))
cl3 := ChannelLayout{}
require.True(t, cl2.Equal(cl3))
}