From db4a8fc1b8c2c83c0ae9dbf74194b205c528d66b Mon Sep 17 00:00:00 2001 From: tinyzimmer <38474291+tinyzimmer@users.noreply.github.com> Date: Wed, 30 Sep 2020 21:36:59 +0300 Subject: [PATCH] fix linting and add GstContext --- .golangci.yml | 9 ++++ Makefile | 2 +- gst/gst.go.h | 13 +++++ gst/gst_context.go | 102 ++++++++++++++++++++++++++++++++++++++ gst/gst_wrappers.go | 11 ++++ hack/{test.go => caps.go} | 2 +- hack/clock_id_wait.go | 2 +- hack/sleep.go | 2 +- hack/test.c | 21 -------- 9 files changed, 139 insertions(+), 25 deletions(-) create mode 100644 .golangci.yml create mode 100644 gst/gst_context.go rename hack/{test.go => caps.go} (94%) delete mode 100644 hack/test.c diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..63c2158 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,9 @@ +run: + timeout: 10m + + skip-dirs: + - hack/ + - cmd/ + + skip-files: + - hack/.* \ No newline at end of file diff --git a/Makefile b/Makefile index f75313e..608dfbf 100644 --- a/Makefile +++ b/Makefile @@ -17,4 +17,4 @@ $(GOLANGCI_LINT): ln -s golangci-lint-$(GOLANGCI_VERSION)-$(shell uname | tr A-Z a-z)-amd64/golangci-lint $(GOLANGCI_LINT) lint: $(GOLANGCI_LINT) - $(GOLANGCI_LINT) run -v --timeout 300s --skip-dirs cmd/ --skip-files hack/* \ No newline at end of file + $(GOLANGCI_LINT) run -v \ No newline at end of file diff --git a/gst/gst.go.h b/gst/gst.go.h index 9f489b2..b90a383 100644 --- a/gst/gst.go.h +++ b/gst/gst.go.h @@ -16,6 +16,7 @@ inline GstBus * toGstBus (void *p) { return (GST_BUS inline GstCapsFeatures * toGstCapsFeatures (void *p) { return (GST_CAPS_FEATURES(p)); } inline GstCaps * toGstCaps (void *p) { return (GST_CAPS(p)); } inline GstClock * toGstClock (void *p) { return (GST_CLOCK(p)); } +inline GstContext * toGstContext (void *p) { return (GST_CONTEXT_CAST(p)); } inline GstDevice * toGstDevice (void *p) { return (GST_DEVICE_CAST(p)); } inline GstElementFactory * toGstElementFactory (void *p) { return (GST_ELEMENT_FACTORY(p)); } inline GstElement * toGstElement (void *p) { return (GST_ELEMENT(p)); } @@ -100,6 +101,18 @@ inline GType getCapsType () return GST_TYPE_CAPS; } +/* Context utilities */ + +inline gboolean contextIsWritable (GstContext * ctx) +{ + return gst_context_is_writable(ctx); +} + +inline GstContext * makeContextWritable (GstContext * ctx) +{ + return gst_context_make_writable(ctx); +} + /* Object Utilities */ inline GObjectClass * getGObjectClass (void * p) { return (G_OBJECT_GET_CLASS(p)); } diff --git a/gst/gst_context.go b/gst/gst_context.go new file mode 100644 index 0000000..d6b1619 --- /dev/null +++ b/gst/gst_context.go @@ -0,0 +1,102 @@ +package gst + +// #include "gst.go.h" +import "C" +import "unsafe" + +// Context wraps a GstContext object. +type Context struct { + ptr *C.GstContext +} + +// NewContext creates a new context. +// +// // Example +// +// ctx := gst.NewContext("test-context", false) +// fmt.Println(ctx.IsPersistent()) +// +// ctx = gst.NewContext("test-context", true) +// fmt.Println(ctx.IsPersistent()) +// +// // false +// // true +// +func NewContext(ctxType string, persistent bool) *Context { + cStr := C.CString(ctxType) + defer C.free(unsafe.Pointer(cStr)) + ctx := C.gst_context_new((*C.gchar)(unsafe.Pointer(cStr)), gboolean(persistent)) + if ctx == nil { + return nil + } + return wrapContext(ctx) +} + +// Instance returns the underlying GstContext instance. +func (c *Context) Instance() *C.GstContext { return C.toGstContext(unsafe.Pointer(c.ptr)) } + +// GetType returns the type of the context. +// +// // Example +// +// ctx := gst.NewContext("test-context", false) +// fmt.Println(ctx.GetType()) +// +// // test-context +// +func (c *Context) GetType() string { + return C.GoString(C.gst_context_get_context_type(c.Instance())) +} + +// GetStructure returns the structure of the context. You should not modify or unref it. +func (c *Context) GetStructure() *Structure { + st := C.gst_context_get_structure(c.Instance()) + if st == nil { + return nil + } + return wrapStructure(st) +} + +// HasContextType checks if the context has the given type. +// +// // Example +// +// ctx := gst.NewContext("test-context", false) +// fmt.Println(ctx.HasContextType("test-context")) +// fmt.Println(ctx.HasContextType("another-context")) +// +// // true +// // false +// +func (c *Context) HasContextType(ctxType string) bool { + cStr := C.CString(ctxType) + defer C.free(unsafe.Pointer(cStr)) + return gobool(C.gst_context_has_context_type( + c.Instance(), + (*C.gchar)(unsafe.Pointer(cStr)), + )) +} + +// IsPersistent checks if the context is persistent. +func (c *Context) IsPersistent() bool { + return gobool(C.gst_context_is_persistent(c.Instance())) +} + +// IsWritable returns true if the context is writable. +func (c *Context) IsWritable() bool { + return gobool(C.contextIsWritable(c.Instance())) +} + +// MakeWritable returns a writable version of the context. +func (c *Context) MakeWritable() *Context { + return wrapContext(C.makeContextWritable(c.Instance())) +} + +// WritableStructure returns a writable version of the structure. You should still not unref it. +func (c *Context) WritableStructure() *Structure { + st := C.gst_context_writable_structure(c.Instance()) + if st == nil { + return nil + } + return wrapStructure(st) +} diff --git a/gst/gst_wrappers.go b/gst/gst_wrappers.go index ea35c73..d652eef 100644 --- a/gst/gst_wrappers.go +++ b/gst/gst_wrappers.go @@ -115,6 +115,10 @@ func init() { T: glib.Type(C.GST_TYPE_CHILD_PROXY), F: marshalChildProxy, }, + { + T: glib.Type(C.GST_TYPE_CONTEXT), + F: marshalContext, + }, // Boxed {T: glib.Type(C.gst_message_get_type()), F: marshalMessage}, @@ -134,6 +138,7 @@ func wrapBus(obj *glib.Object) *Bus { return &Bus{Object: func wrapCaps(caps *C.GstCaps) *Caps { return &Caps{native: caps} } func wrapChildProxy(c *C.GstChildProxy) *ChildProxy { return &ChildProxy{ptr: c} } func wrapClock(obj *glib.Object) *Clock { return &Clock{wrapObject(obj)} } +func wrapContext(ctx *C.GstContext) *Context { return &Context{ptr: ctx} } func wrapDevice(obj *glib.Object) *Device { return &Device{wrapObject(obj)} } func wrapElement(obj *glib.Object) *Element { return &Element{wrapObject(obj)} } func wrapGhostPad(obj *glib.Object) *GhostPad { return &GhostPad{wrapPad(obj)} } @@ -328,3 +333,9 @@ func marshalChildProxy(p uintptr) (interface{}, error) { obj := (*C.GstChildProxy)(unsafe.Pointer(c)) return wrapChildProxy(obj), nil } + +func marshalContext(p uintptr) (interface{}, error) { + c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p))) + obj := (*C.GstContext)(unsafe.Pointer(c)) + return wrapContext(obj), nil +} diff --git a/hack/test.go b/hack/caps.go similarity index 94% rename from hack/test.go rename to hack/caps.go index 872604e..829cea1 100644 --- a/hack/test.go +++ b/hack/caps.go @@ -6,7 +6,7 @@ import ( "github.com/tinyzimmer/go-gst/gst" ) -func main() { +func capsWeirdness() { gst.Init(nil) caps := gst.NewCapsFromString("audio/x-raw") diff --git a/hack/clock_id_wait.go b/hack/clock_id_wait.go index 758fb1f..04f905b 100644 --- a/hack/clock_id_wait.go +++ b/hack/clock_id_wait.go @@ -7,7 +7,7 @@ import ( "github.com/tinyzimmer/go-gst/gst" ) -func main() { +func wait() { gst.Init(nil) clock := gst.ObtainSystemClock() diff --git a/hack/sleep.go b/hack/sleep.go index 51f6a07..be1558b 100644 --- a/hack/sleep.go +++ b/hack/sleep.go @@ -7,7 +7,7 @@ import ( "time" ) -func main() { +func sleep() { go func() { C.sleep(1000000000) fmt.Println("I slept") diff --git a/hack/test.c b/hack/test.c deleted file mode 100644 index 1d193ef..0000000 --- a/hack/test.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -gboolean caps_map_func (GstCapsFeatures * features, GstStructure * structure, gpointer user_data) -{ - printf(gst_caps_features_to_string(features)); - return TRUE; -} - -int main () { - gst_init(NULL, NULL); - - GstCaps * caps = gst_caps_from_string("audio/x-raw"); - - gst_caps_filter_and_map_in_place(caps, caps_map_func, NULL); - gst_caps_foreach(caps, caps_map_func, NULL); - - gst_caps_unref(caps); - - return 0; -} \ No newline at end of file