Merge pull request #26 from go-gst/device-monitor

Device monitor
This commit is contained in:
Wilhelm Bartel
2023-08-28 21:30:17 +02:00
committed by GitHub
7 changed files with 310 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
// This example uses gstreamer's device monitor api.
//
// https://gstreamer.freedesktop.org/documentation/gstreamer/gstdevicemonitor.html
package main
import (
"fmt"
"github.com/go-gst/go-glib/glib"
"github.com/go-gst/go-gst/examples"
"github.com/go-gst/go-gst/gst"
)
func runPipeline(loop *glib.MainLoop) error {
gst.Init(nil)
fmt.Println("Running device monitor")
// if len(os.Args) < 2 {
// fmt.Printf("USAGE: %s <uri>\n", os.Args[0])
// os.Exit(1)
// }
// uri := os.Args[1]
fmt.Println("Creating device monitor")
monitor := gst.NewDeviceMonitor()
fmt.Println("Created device monitor", monitor)
// if err != nil {
// fmt.Println("ERROR:", err)
// os.Exit(2)
// }
caps := gst.NewCapsFromString("video/x-raw")
monitor.AddFilter("Video/Source", caps)
fmt.Println("Getting device monitor bus")
bus := monitor.GetBus()
fmt.Println("Got device monitor bus", bus)
bus.AddWatch(func(msg *gst.Message) bool {
switch msg.Type() {
case gst.MessageDeviceAdded:
message := msg.ParseDeviceAdded().GetDisplayName()
fmt.Println("Added: ", message)
case gst.MessageDeviceRemoved:
message := msg.ParseDeviceRemoved().GetDisplayName()
fmt.Println("Removed: ", message)
default:
// All messages implement a Stringer. However, this is
// typically an expensive thing to do and should be avoided.
fmt.Println("Type: ", msg.Type())
fmt.Println("Message: ", msg)
}
return true
})
fmt.Println("Starting device monitor")
monitor.Start()
fmt.Println("Started device monitor")
devices := monitor.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)
})
}

View File

@@ -0,0 +1,78 @@
// This example uses gstreamer's device provider api.
//
// https://gstreamer.freedesktop.org/documentation/gstreamer/gstdeviceprovider.html
package main
import (
"fmt"
"os"
"github.com/go-gst/go-glib/glib"
"github.com/go-gst/go-gst/examples"
"github.com/go-gst/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 <uri>\n", os.Args[0])
// os.Exit(1)
// }
// uri := os.Args[1]
fmt.Println("Creating device monitor")
// provider := gst.FindDeviceProviderByName("foo")
// fmt.Println("Created device provider", provider)
provider := gst.FindDeviceProviderByName("avfdeviceprovider")
fmt.Println("Created device provider", provider)
if provider == nil {
fmt.Println("No provider found")
os.Exit(2)
}
fmt.Println("Getting device provider bus")
bus := provider.GetBus()
fmt.Println("Got device provider bus", bus)
bus.AddWatch(func(msg *gst.Message) bool {
switch msg.Type() {
case gst.MessageDeviceAdded:
message := msg.ParseDeviceAdded().GetDisplayName()
fmt.Println("Added: ", message)
case gst.MessageDeviceRemoved:
message := msg.ParseDeviceRemoved().GetDisplayName()
fmt.Println("Removed: ", message)
default:
// All messages implement a Stringer. However, this is
// typically an expensive thing to do and should be avoided.
fmt.Println("Type: ", msg.Type())
fmt.Println("Message: ", msg)
}
return true
})
fmt.Println("Starting device monitor")
provider.Start()
fmt.Println("Started device monitor")
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)
})
}

View File

@@ -15,6 +15,7 @@ GstChildProxy * toGstChildProxy (void *p) { re
GstClock * toGstClock (void *p) { return (GST_CLOCK(p)); }
GstContext * toGstContext (void *p) { return (GST_CONTEXT_CAST(p)); }
GstDevice * toGstDevice (void *p) { return (GST_DEVICE_CAST(p)); }
GstDeviceProvider * toGstDeviceProvider (void *p) { return (GST_DEVICE_PROVIDER_CAST(p)); }
GstElementFactory * toGstElementFactory (void *p) { return (GST_ELEMENT_FACTORY(p)); }
GstElementClass * toGstElementClass (void *p) { return (GST_ELEMENT_CLASS(p)); }
GstElement * toGstElement (void *p) { return (GST_ELEMENT(p)); }

View File

@@ -31,6 +31,7 @@ extern GstChildProxy * toGstChildProxy (void *
extern GstClock * toGstClock (void *p);
extern GstContext * toGstContext (void *p);
extern GstDevice * toGstDevice (void *p);
extern GstDeviceProvider * toGstDeviceProvider (void *p);
extern GstElementFactory * toGstElementFactory (void *p);
extern GstElementClass * toGstElementClass (void *p);
extern GstElement * toGstElement (void *p);

82
gst/gst_device_monitor.go Normal file
View File

@@ -0,0 +1,82 @@
package gst
// #include "gst.go.h"
import "C"
import (
"unsafe"
"github.com/go-gst/go-glib/glib"
)
// DeviceMonitor is a Go representation of a GstDeviceMonitor.
type DeviceMonitor struct {
ptr *C.GstDeviceMonitor
bus *Bus
}
func NewDeviceMonitor() *DeviceMonitor {
monitor := C.gst_device_monitor_new()
if monitor == nil {
return nil
}
return &DeviceMonitor{ptr: monitor}
}
func (d *DeviceMonitor) AddFilter(classes string, caps *Caps) uint {
var cClasses *C.gchar
if classes != "" {
cClasses = C.CString(classes)
defer C.free(unsafe.Pointer(cClasses))
}
filterId := C.gst_device_monitor_add_filter(d.ptr, cClasses, caps.Instance())
return uint(filterId)
}
func (d *DeviceMonitor) RemoveFilter(filterId uint) bool {
return gobool(C.gst_device_monitor_remove_filter(d.ptr, C.guint(filterId)))
}
// GetBus returns the message bus for this pipeline.
func (d *DeviceMonitor) GetBus() *Bus {
if d.bus == nil {
cBus := C.gst_device_monitor_get_bus(d.ptr)
d.bus = FromGstBusUnsafeFull(unsafe.Pointer(cBus))
}
return d.bus
}
func (d *DeviceMonitor) Start() bool {
return gobool(C.gst_device_monitor_start(d.ptr))
//should return if we were able to add the filter
}
func (d *DeviceMonitor) Stop() {
C.gst_device_monitor_stop(d.ptr)
//should return if we were able to add the filter
}
func (d *DeviceMonitor) GetDevices() []*Device {
glist := C.gst_device_monitor_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
}
func (d *DeviceMonitor) SetShowAllDevices(show bool) {
C.gst_device_monitor_set_show_all_devices(d.ptr, gboolean(show))
}
func (d *DeviceMonitor) GetShowAllDevices() bool {
return gobool(C.gst_device_monitor_get_show_all_devices(d.ptr))
}
//gst_device_monitor_get_providers

View File

@@ -0,0 +1,55 @@
package gst
// #include "gst.go.h"
import "C"
import (
"unsafe"
"github.com/go-gst/go-glib/glib"
)
// DeviceProvider is a Go representation of a GstDeviceProvider.
type DeviceProvider struct{ *Object }
// FromGstDeviceProviderUnsafeNone wraps the given device with a ref and finalizer.
func FromGstDeviceProviderUnsafeNone(deviceProvider unsafe.Pointer) *DeviceProvider {
return &DeviceProvider{wrapObject(glib.TransferNone(deviceProvider))}
}
// FromGstDeviceProviderUnsafeFull wraps the given device with a finalizer.
func FromGstDeviceProviderUnsafeFull(deviceProvider unsafe.Pointer) *DeviceProvider {
return &DeviceProvider{wrapObject(glib.TransferFull(deviceProvider))}
}
// Instance returns the underlying GstDevice object.
func (d *DeviceProvider) Instance() *C.GstDeviceProvider { return C.toGstDeviceProvider(d.Unsafe()) }
func (d *DeviceProvider) GetDevices() []*Device {
glist := C.gst_device_provider_get_devices((*C.GstDeviceProvider)(d.Instance()))
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
}
// GetBus returns the message bus for this pipeline.
func (d *DeviceProvider) GetBus() *Bus {
cBus := C.gst_device_provider_get_bus((*C.GstDeviceProvider)(d.Instance()))
bus := FromGstBusUnsafeFull(unsafe.Pointer(cBus))
return bus
}
func (d *DeviceProvider) Start() bool {
return gobool(C.gst_device_provider_start((*C.GstDeviceProvider)(d.Instance())))
}
func (d *DeviceProvider) Stop() {
C.gst_device_provider_stop((*C.GstDeviceProvider)(d.Instance()))
}

View File

@@ -0,0 +1,18 @@
package gst
// #include "gst.go.h"
import "C"
import (
"unsafe"
)
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 FromGstDeviceProviderUnsafeFull(unsafe.Pointer(provider))
}