diff --git a/channel_layout.go b/channel_layout.go index a897052..35420a1 100644 --- a/channel_layout.go +++ b/channel_layout.go @@ -75,15 +75,15 @@ type ChannelLayout struct { c *C.struct_AVChannelLayout } -func newChannelLayoutFromC(c *C.struct_AVChannelLayout) *ChannelLayout { - return &ChannelLayout{c: c} +func newChannelLayoutFromC(c *C.struct_AVChannelLayout) ChannelLayout { + return ChannelLayout{c: c} } -func (l *ChannelLayout) NbChannels() int { +func (l ChannelLayout) NbChannels() int { return int(l.c.nb_channels) } -func (l *ChannelLayout) String() string { +func (l ChannelLayout) String() string { b := make([]byte, 1024) n, err := l.Describe(b) if err != nil { @@ -92,7 +92,7 @@ func (l *ChannelLayout) String() string { return string(b[:n]) } -func (l *ChannelLayout) Describe(b []byte) (int, error) { +func (l ChannelLayout) Describe(b []byte) (int, error) { ret := C.av_channel_layout_describe(l.c, (*C.char)(unsafe.Pointer(&b[0])), cUlong(len(b))) if ret < 0 { return 0, newError(ret) @@ -100,11 +100,11 @@ func (l *ChannelLayout) Describe(b []byte) (int, error) { return int(ret), nil } -func (l *ChannelLayout) Valid() bool { +func (l ChannelLayout) Valid() bool { return C.av_channel_layout_check(l.c) > 0 } -func (l *ChannelLayout) Compare(l2 *ChannelLayout) (equal bool, err error) { +func (l ChannelLayout) Compare(l2 ChannelLayout) (equal bool, err error) { ret := C.av_channel_layout_compare(l.c, l2.c) if ret < 0 { return false, newError(ret) @@ -112,11 +112,19 @@ func (l *ChannelLayout) Compare(l2 *ChannelLayout) (equal bool, err error) { return ret == 0, nil } -func (l *ChannelLayout) Equal(l2 *ChannelLayout) bool { +func (l ChannelLayout) Equal(l2 ChannelLayout) bool { v, _ := l.Compare(l2) return v } -func (l *ChannelLayout) copy(dst *C.struct_AVChannelLayout) error { +func (l ChannelLayout) copy(dst *C.struct_AVChannelLayout) error { return newError(C.av_channel_layout_copy(dst, l.c)) } + +func (l ChannelLayout) clone() (ChannelLayout, error) { + // TODO Should it be freed? + cl := C.struct_AVChannelLayout{} + err := l.copy(&cl) + dst := newChannelLayoutFromC(&cl) + return dst, err +} diff --git a/codec.go b/codec.go index d4d199a..d1d75c2 100644 --- a/codec.go +++ b/codec.go @@ -28,13 +28,13 @@ func (c *Codec) String() string { return c.Name() } -func (c *Codec) ChannelLayouts() (o []*ChannelLayout) { +func (c *Codec) ChannelLayouts() (o []ChannelLayout) { if c.c.ch_layouts == nil { return nil } size := unsafe.Sizeof(*c.c.ch_layouts) for i := 0; ; i++ { - v := newChannelLayoutFromC((*C.struct_AVChannelLayout)(unsafe.Pointer(uintptr(unsafe.Pointer(c.c.ch_layouts)) + uintptr(i)*size))) + v, _ := newChannelLayoutFromC((*C.struct_AVChannelLayout)(unsafe.Pointer(uintptr(unsafe.Pointer(c.c.ch_layouts)) + uintptr(i)*size))).clone() if !v.Valid() { break } diff --git a/codec_context.go b/codec_context.go index 6cfa3ca..a9b9f60 100644 --- a/codec_context.go +++ b/codec_context.go @@ -53,11 +53,12 @@ func (cc *CodecContext) SetChannels(channels int) { cc.c.channels = C.int(channels) } -func (cc *CodecContext) ChannelLayout() *ChannelLayout { - return newChannelLayoutFromC(&cc.c.ch_layout) +func (cc *CodecContext) ChannelLayout() ChannelLayout { + l, _ := newChannelLayoutFromC(&cc.c.ch_layout).clone() + return l } -func (cc *CodecContext) SetChannelLayout(l *ChannelLayout) { +func (cc *CodecContext) SetChannelLayout(l ChannelLayout) { l.copy(&cc.c.ch_layout) //nolint: errcheck } diff --git a/codec_parameters.go b/codec_parameters.go index a351111..8a487e6 100644 --- a/codec_parameters.go +++ b/codec_parameters.go @@ -28,11 +28,12 @@ func (cp *CodecParameters) BitRate() int64 { return int64(cp.c.bit_rate) } -func (cp *CodecParameters) ChannelLayout() *ChannelLayout { - return newChannelLayoutFromC(&cp.c.ch_layout) +func (cp *CodecParameters) ChannelLayout() ChannelLayout { + l, _ := newChannelLayoutFromC(&cp.c.ch_layout).clone() + return l } -func (cp *CodecParameters) SetChannelLayout(l *ChannelLayout) { +func (cp *CodecParameters) SetChannelLayout(l ChannelLayout) { l.copy(&cp.c.ch_layout) //nolint: errcheck } diff --git a/codec_test.go b/codec_test.go index b2bacfc..915ab24 100644 --- a/codec_test.go +++ b/codec_test.go @@ -20,7 +20,7 @@ func TestCodec(t *testing.T) { c = astiav.FindDecoderByName("aac") require.NotNil(t, c) - els := []*astiav.ChannelLayout{ + els := []astiav.ChannelLayout{ astiav.ChannelLayoutMono, astiav.ChannelLayoutStereo, astiav.ChannelLayoutSurround, diff --git a/frame.go b/frame.go index 522c70b..6ae69eb 100644 --- a/frame.go +++ b/frame.go @@ -38,11 +38,12 @@ func (f *Frame) AllocSamples(align int) error { return newError(C.av_samples_alloc(&f.c.data[0], &f.c.linesize[0], f.c.ch_layout.nb_channels, f.c.nb_samples, (C.enum_AVSampleFormat)(f.c.format), C.int(align))) } -func (f *Frame) ChannelLayout() *ChannelLayout { - return newChannelLayoutFromC(&f.c.ch_layout) +func (f *Frame) ChannelLayout() ChannelLayout { + l, _ := newChannelLayoutFromC(&f.c.ch_layout).clone() + return l } -func (f *Frame) SetChannelLayout(l *ChannelLayout) { +func (f *Frame) SetChannelLayout(l ChannelLayout) { l.copy(&f.c.ch_layout) //nolint: errcheck }