cleanup custom events example

This commit is contained in:
tinyzimmer
2020-10-05 08:39:42 +03:00
parent 25ab6919a4
commit 3ae02e640e

View File

@@ -38,7 +38,7 @@ func createPipeline() (*gst.Pipeline, error) {
sinkpad := sink.GetStaticPad("sink") sinkpad := sink.GetStaticPad("sink")
// Add a probe for out custom event // Add a probe for out custom event
sinkpad.AddProbe(gst.PadProbeTypeEventDownstream, func(p *gst.Pad, info *gst.PadProbeInfo) gst.PadProbeReturn { sinkpad.AddProbe(gst.PadProbeTypeEventDownstream, func(self *gst.Pad, info *gst.PadProbeInfo) gst.PadProbeReturn {
// Retrieve the event from the probe // Retrieve the event from the probe
ev := info.GetEvent() ev := info.GetEvent()
@@ -58,7 +58,7 @@ func createPipeline() (*gst.Pipeline, error) {
fmt.Printf("Received custom event with count=%d send_eos=%v\n", customEvent.Count, customEvent.SendEOS) fmt.Printf("Received custom event with count=%d send_eos=%v\n", customEvent.Count, customEvent.SendEOS)
if customEvent.SendEOS { if customEvent.SendEOS {
fmt.Println("Send EOS is true, sending eos") fmt.Println("Send EOS is true, sending eos")
if !pipeline.SendEvent(gst.NewEOSEvent()) { if !pipeline.GetPipelineBus().Post(gst.NewEOSMessage(self)) {
fmt.Println("WARNING: Failed to send EOS to pipeline") fmt.Println("WARNING: Failed to send EOS to pipeline")
} }
} else { } else {
@@ -71,37 +71,41 @@ func createPipeline() (*gst.Pipeline, error) {
} }
func mainLoop(loop *gst.MainLoop, pipeline *gst.Pipeline) error { func mainLoop(loop *gst.MainLoop, pipeline *gst.Pipeline) error {
// Start the pipeline
pipeline.SetState(gst.StatePlaying)
// Create a watch on the pipeline to kill the main loop when EOS is received // Create a watch on the pipeline to kill the main loop when EOS is received
pipeline.GetPipelineBus().AddWatch(func(msg *gst.Message) bool { pipeline.GetPipelineBus().AddWatch(func(msg *gst.Message) bool {
switch msg.Type() { switch msg.Type() {
case gst.MessageEOS: case gst.MessageEOS:
fmt.Println("Got EOS message")
pipeline.Destroy() pipeline.Destroy()
loop.Quit() loop.Quit()
default:
fmt.Println(msg)
} }
return true return true
}) })
// Loop and on the third iteration send the custom event. // Start the pipeline
ticker := time.NewTicker(time.Second * 2) pipeline.SetState(gst.StatePlaying)
count := 0
for range ticker.C { go func() {
ev := ExampleCustomEvent{Count: count} // Loop and on the third iteration send the custom event.
if count == 3 { ticker := time.NewTicker(time.Second * 2)
ev.SendEOS = true count := 0
for range ticker.C {
ev := ExampleCustomEvent{Count: count}
if count == 3 {
ev.SendEOS = true
}
st := gst.MarshalStructure(ev)
if !pipeline.SendEvent(gst.NewCustomEvent(gst.EventTypeCustomDownstream, st)) {
fmt.Println("Warning: failed to send custom event")
}
if count == 3 {
break
}
count++
} }
st := gst.MarshalStructure(ev) }()
if !pipeline.SendEvent(gst.NewCustomEvent(gst.EventTypeCustomDownstream, st)) {
fmt.Println("Warning: failed to send custom event")
}
if count == 3 {
break
}
count++
}
return loop.RunError() return loop.RunError()
} }