From 682a4b03d75f50abaf63da6834f33dc6df7dfbb0 Mon Sep 17 00:00:00 2001 From: RSWilli Date: Fri, 30 Aug 2024 11:23:40 +0200 Subject: [PATCH] change emit docs, add tests for signal emits --- gst/gst_element.go | 12 +++-- gst/gst_element_emit_test.go | 95 ++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 gst/gst_element_emit_test.go diff --git a/gst/gst_element.go b/gst/gst_element.go index bfc1014..1038f68 100644 --- a/gst/gst_element.go +++ b/gst/gst_element.go @@ -200,12 +200,14 @@ func (e *Element) ContinueState(ret StateChangeReturn) StateChangeReturn { return StateChangeReturn(C.gst_element_continue_state(e.Instance(), C.GstStateChangeReturn(ret))) } -// Emit is a wrapper around g_signal_emitv() and emits the signal specified by the string s to an Object. Arguments to -// callback functions connected to this signal must be specified in args. Emit() returns an interface{} which must be -// type asserted as the Go equivalent type to the return value for native C callback. +// Emit is a wrapper around g_signal_emitv() and emits the signal +// specified by the string s to an Object. This means that it performs identically to +// g_signal_emit_by_name. Arguments to callback functions connected to this signal must +// be specified in args. Emit() returns an interface{} which contains the go equivalent of the C return value. // -// Note that this code is unsafe in that the types of values in args are not checked against whether they are suitable -// for the callback. +// Make sure that the Types are known to go-glib. Special types need to be registered with +// RegisterGValueMarshalers before calling Emit. Sub libraries of go-gst (sdp, rtp, webrtc, base, etc.) +// register their types when they are imported via init() functions. func (e *Element) Emit(signal string, args ...interface{}) (interface{}, error) { // We are wrapping this for the same reason as Connect. if e.TypeFromInstance() != glib.Type(C.GST_TYPE_ELEMENT) { diff --git a/gst/gst_element_emit_test.go b/gst/gst_element_emit_test.go new file mode 100644 index 0000000..a7f5693 --- /dev/null +++ b/gst/gst_element_emit_test.go @@ -0,0 +1,95 @@ +package gst_test + +import ( + "errors" + "strings" + "testing" + + "github.com/go-gst/go-glib/glib" + "github.com/go-gst/go-gst/gst" +) + +func TestSignalEmitSimpleReturnValue(t *testing.T) { + gst.Init(nil) + webrtcbin, err := gst.NewElement("webrtcbin") + + if err != nil { + t.Fatal(err) + } + + okI, err := webrtcbin.Emit("add-turn-server", "turn://user:password@host:1234") + + if err != nil { + t.Fatal(err) + } + + ok := okI.(bool) + + if !ok { + t.Fatal("Failed to add turn server") + } +} + +func TestSignalEmitGObjectReturnValue(t *testing.T) { + gst.Init(nil) + + elements := []string{ + "rtpbin", "name=rtpbin", + + "videotestsrc", "!", "videoconvert", "!", "queue", "!", + "x264enc", "bframes=0", "speed-preset=ultrafast", "tune=zerolatency", "name=encoder", "!", "queue", "!", "rtph264pay", "config-interval=1", "!", "rtpbin.send_rtp_sink_0", "rtpbin.send_rtp_src_0", "!", + + "udpsink", "host=127.0.0.1", "port=5510", "sync=false", "async=false", "rtpbin.send_rtcp_src_0", "!", + + "udpsink", "host=127.0.0.1", "port=5511", "sync=false", "async=false", + + "udpsrc", "port=5511", "caps=\"application/x-rtcp\"", "!", "rtpbin.recv_rtcp_sink_0", + } + + pipeline, err := gst.NewPipelineFromString(strings.Join(elements, " ")) + if err != nil { + t.Fatal(err) + } + + rtpbin, err := pipeline.GetElementByName("rtpbin") + if err != nil { + t.Fatal(err) + } + + errchan := make(chan error) + + rtpbin.Connect("on-new-ssrc", func(bin *gst.Element, sessionID uint, ssrc uint32) { + retI, err := rtpbin.Emit("get-internal-session", sessionID) + + if err != nil { + errchan <- err + } + + rtpSession, ok := retI.(*glib.Object) + + if !ok { + errchan <- errors.New("could not cast return value to *glib.Object") + } + + bw, err := rtpSession.GetProperty("bandwidth") + + if err != nil { + errchan <- err + } + + _ = bw + + close(errchan) + }) + + pipeline.SetState(gst.StatePlaying) + + err = <-errchan + + if err != nil { + t.Fatal(err) + } + + pipeline.SetState(gst.StateNull) + +}