diff --git a/go.mod b/go.mod index 117c0df2..f1ede44a 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.16 require ( github.com/mattn/go-pointer v0.0.1 + github.com/stretchr/testify v1.8.0 github.com/tinyzimmer/go-glib v0.0.25 ) diff --git a/go.sum b/go.sum index ad14e8f9..4b9cdb5b 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,19 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0= github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc= -github.com/tinyzimmer/go-glib v0.0.24 h1:ktZZC22/9t88kGRgNEFV/SESgIWhGHE+q7Z7Qj++luw= -github.com/tinyzimmer/go-glib v0.0.24/go.mod h1:ltV0gO6xNFzZhsIRbFXv8RTq9NGoNT2dmAER4YmZfaM= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/tinyzimmer/go-glib v0.0.25 h1:2GpumtkxA0wpXhCXP6D3ksb5pGMfo9WbhgLvEw8njK4= +github.com/tinyzimmer/go-glib v0.0.25/go.mod h1:ltV0gO6xNFzZhsIRbFXv8RTq9NGoNT2dmAER4YmZfaM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/gst/gst_sample.go b/gst/gst_sample.go index fb135ce4..bfd1b5da 100644 --- a/gst/gst_sample.go +++ b/gst/gst_sample.go @@ -1,6 +1,9 @@ package gst // #include "gst.go.h" +// GstSample* new_empty_sample() { +// return gst_sample_new(NULL, NULL, NULL, NULL); +// } import "C" import ( @@ -30,6 +33,12 @@ func FromGstSampleUnsafeFull(sample unsafe.Pointer) *Sample { return s } +// NewEmptySample creates an empty GstSample. +// This is meant for test usage only. Tests in Go are not permitted to use CGO directly +func NewEmptySample() *Sample { + return FromGstSampleUnsafeFull(unsafe.Pointer(C.new_empty_sample())) +} + // Instance returns the underlying *GstSample instance. func (s *Sample) Instance() *C.GstSample { return C.toGstSample(unsafe.Pointer(s.sample)) } @@ -46,12 +55,18 @@ func (s *Sample) Copy() *Sample { // GetBuffer returns the buffer inside this sample. func (s *Sample) GetBuffer() *Buffer { - return FromGstBufferUnsafeNone(unsafe.Pointer(C.gst_sample_get_buffer((*C.GstSample)(s.Instance())))) + if p := unsafe.Pointer(C.gst_sample_get_buffer((*C.GstSample)(s.Instance()))); p != C.NULL { + return FromGstBufferUnsafeNone(p) + } + return nil } // GetBufferList gets the buffer list associated with this sample. func (s *Sample) GetBufferList() *BufferList { - return FromGstBufferListUnsafeNone(unsafe.Pointer(C.gst_sample_get_buffer_list(s.Instance()))) + if p := unsafe.Pointer(C.gst_sample_get_buffer_list(s.Instance())); p != C.NULL { + return FromGstBufferListUnsafeNone(p) + } + return nil } // GetCaps returns the caps associated with this sample. Take a ref if you need to hold on to them diff --git a/gst/gst_segment_test.go b/gst/gst_segment_test.go new file mode 100644 index 00000000..54fe6f9b --- /dev/null +++ b/gst/gst_segment_test.go @@ -0,0 +1,51 @@ +package gst_test + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tinyzimmer/go-gst/gst" +) + +func TestSampleReturnsValidBuffer(t *testing.T) { + sut := gst.NewEmptySample() + + buf1 := gst.NewEmptyBuffer() + sut.SetBuffer(buf1) + + // We cannot expect equivalance between Go Buffer wrapper + // and the one returned, however .Instance should match + buf2 := sut.GetBuffer() + assert.NotNil(t, buf2) + assert.Equal(t, buf1.Instance(), buf2.Instance()) +} + +func TestSampleReturnsNilBuffer(t *testing.T) { + sut := gst.NewEmptySample() + assert.Nil(t, sut.GetBuffer()) +} + +func TestSampleReturnsValidBufferList(t *testing.T) { + sut := gst.NewEmptySample() + + bufList1 := gst.NewBufferList(nil) + sut.SetBufferList(bufList1) + + // We cannot expect equivalance between Go Buffer wrapper + // and the one returned, however .Instance should match + bufList2 := sut.GetBufferList() + assert.NotNil(t, bufList2) + assert.Equal(t, bufList1.Instance(), bufList2.Instance()) +} + +func TestSampleReturnsNilBufferList(t *testing.T) { + sut := gst.NewEmptySample() + assert.Nil(t, sut.GetBufferList()) +} + +func TestMain(m *testing.M) { + gst.Init(nil) + defer gst.Deinit() + os.Exit(m.Run()) +}