From 9f4a034ade75f200ca3f8e526e76a7a431fce91b Mon Sep 17 00:00:00 2001 From: RSWilli Date: Mon, 11 Nov 2024 13:31:13 +0100 Subject: [PATCH] expose clock types and ToGstClock, add convinience function to create a SystemClock --- gst/gst_clock.go | 11 +++++++++++ gst/gst_system_clock.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/gst/gst_clock.go b/gst/gst_clock.go index 6c288e7..7992290 100644 --- a/gst/gst_clock.go +++ b/gst/gst_clock.go @@ -167,6 +167,17 @@ func FromGstClockUnsafeFull(clock unsafe.Pointer) *Clock { return wrapClock(glib.TransferFull(clock)) } +// ToGstClock wraps the given glib.Object or gst.Object in a Clock instance. +func ToGstClock(obj interface{}) *Clock { + switch obj := obj.(type) { + case *Object: + return &Clock{Object: obj} + case *glib.Object: + return wrapClock(obj) + } + return nil +} + // Instance returns the underlying GstClock instance. func (c *Clock) Instance() *C.GstClock { return C.toGstClock(c.Unsafe()) } diff --git a/gst/gst_system_clock.go b/gst/gst_system_clock.go index d0b2417..66f7514 100644 --- a/gst/gst_system_clock.go +++ b/gst/gst_system_clock.go @@ -4,12 +4,45 @@ package gst import "C" import ( "unsafe" + + "github.com/go-gst/go-glib/glib" ) // SystemClock wraps GstSystemClock type SystemClock struct{ *Clock } +var TYPE_SYSTEM_CLOCK = glib.Type(C.GST_TYPE_SYSTEM_CLOCK) + +// ClockType represents GstClockType +type ClockType int + +const ( + //time since Epoch + ClockTypeRealtime = C.GST_CLOCK_TYPE_REALTIME + //monotonic time since some unspecified starting point + ClockTypeMonotonic = C.GST_CLOCK_TYPE_MONOTONIC + // some other time source is used (Since: 1.0.5) + ClockTypeOther = C.GST_CLOCK_TYPE_OTHER + // time since Epoch, but using International Atomic Time as reference (Since: 1.18) + ClockTypeTAI = C.GST_CLOCK_TYPE_TAI +) + // ObtainSystemClock returns the default SystemClock. func ObtainSystemClock() *SystemClock { return &SystemClock{FromGstClockUnsafeFull(unsafe.Pointer(C.gst_system_clock_obtain()))} } + +// NewSystemClock creates a new instance of a SystemClock, with the given clock type parameter +// +// This is only a convenience wrapper for glib.NewObjectWithProperties +func NewSystemClock(clockType ClockType) (*SystemClock, error) { + clockObj, err := glib.NewObjectWithProperties(TYPE_SYSTEM_CLOCK, map[string]any{ + "clock-type": clockType, + }) + + if err != nil { + return nil, err + } + + return &SystemClock{ToGstClock(clockObj)}, nil +}