diff --git a/examples/device_provider/main.go b/examples/device_provider/main.go new file mode 100644 index 0000000..9b07f0c --- /dev/null +++ b/examples/device_provider/main.go @@ -0,0 +1,56 @@ +// This example uses gstreamer's discoverer api. +// +// https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-libs/html/GstDiscoverer.html +// To detect as much information from a given URI. +// The amount of time that the discoverer is allowed to use is limited by a timeout. +// This allows to handle e.g. network problems gracefully. When the timeout hits before +// discoverer was able to detect anything, discoverer will report an error. +// In this example, we catch this error and stop the application. +// Discovered information could for example contain the stream's duration or whether it is +// seekable (filesystem) or not (some http servers). +package main + +import ( + "fmt" + + "github.com/tinyzimmer/go-glib/glib" + "github.com/tinyzimmer/go-gst/examples" + "github.com/tinyzimmer/go-gst/gst" +) + +func runPipeline(loop *glib.MainLoop) error { + + gst.Init(nil) + fmt.Println("Running device provider") + // if len(os.Args) < 2 { + // fmt.Printf("USAGE: %s \n", os.Args[0]) + // os.Exit(1) + // } + + // uri := os.Args[1] + fmt.Println("Creating device monitor") + + provider := gst.FindDeviceProviderByName("decklinkdeviceprovider") + fmt.Println("Created device provider", provider) + + // if err != nil { + // fmt.Println("ERROR:", err) + // os.Exit(2) + // } + + fmt.Println("listing devices from provider") + devices := provider.GetDevices() + for i, v := range devices { + fmt.Printf("Device: %d %s\n", i, v.GetDisplayName()) + } + + loop.Run() + + return nil +} + +func main() { + examples.RunLoop(func(loop *glib.MainLoop) error { + return runPipeline(loop) + }) +} diff --git a/gst/gst_device_provider.go b/gst/gst_device_provider.go new file mode 100644 index 0000000..3a8d503 --- /dev/null +++ b/gst/gst_device_provider.go @@ -0,0 +1,29 @@ +package gst + +// #include "gst.go.h" +import "C" + +import ( + "unsafe" + + "github.com/tinyzimmer/go-glib/glib" +) + +// DeviceMonitor is a Go representation of a GstDeviceMonitor. +type DeviceProvider struct { + ptr *C.GstDeviceProvider +} + +func (d *DeviceProvider) GetDevices() []*Device { + glist := C.gst_device_provider_get_devices(d.ptr) + if glist == nil { + return nil + } + goList := glib.WrapList(uintptr(unsafe.Pointer(glist))) + out := make([]*Device, 0) + goList.Foreach(func(item interface{}) { + pt := item.(unsafe.Pointer) + out = append(out, FromGstDeviceUnsafeFull(pt)) + }) + return out +} diff --git a/gst/gst_device_provider_factory.go b/gst/gst_device_provider_factory.go new file mode 100644 index 0000000..1d9d53b --- /dev/null +++ b/gst/gst_device_provider_factory.go @@ -0,0 +1,23 @@ +package gst + +// #include "gst.go.h" +import "C" + +import ( + "unsafe" +) + +// DeviceMonitor is a Go representation of a GstDeviceMonitor. +type DeviceProviderFactory struct { + ptr *C.GstDeviceProviderFactory +} + +func FindDeviceProviderByName(factoryName string) *DeviceProvider { + cFactoryName := C.CString(factoryName) + defer C.free(unsafe.Pointer(cFactoryName)) + provider := C.gst_device_provider_factory_get_by_name((*C.gchar)(unsafe.Pointer(cFactoryName))) + if provider == nil { + return nil + } + return &DeviceProvider{ptr: provider} +}