Files
go-gst/examples/launch/main.go
2025-09-16 22:36:08 +02:00

100 lines
2.6 KiB
Go

// This is a simplified go-reimplementation of the gst-launch-<version> cli tool.
// It has no own parameters and simply parses the cli arguments as launch syntax.
// When the parsing succeeded, the pipeline is run until the stream ends or an error happens.
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/go-gst/go-gst/pkg/gst"
)
func main() {
gst.Init()
// Let GStreamer create a pipeline from the parsed launch syntax on the cli.
res, err := gst.ParseLaunch(strings.Join(os.Args[1:], " "))
if err != nil {
fmt.Printf("Parse error: %v", err)
return
}
pipeline := res.(gst.Pipeline)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
for msg := range pipeline.GetBus().Messages(ctx) {
switch msg.Type() {
case gst.MessageEOS: // When end-of-stream is received stop the main loop
fmt.Println("End-of-stream received")
cancel()
case gst.MessageError: // Error messages are always fatal
debug, err := msg.ParseError()
fmt.Println("ERROR:", err.Error())
if debug != "" {
fmt.Println("DEBUG:", debug)
}
cancel()
case gst.MessageAny:
case gst.MessageApplication:
case gst.MessageAsyncDone:
case gst.MessageAsyncStart:
case gst.MessageBuffering:
case gst.MessageClockLost:
case gst.MessageClockProvide:
case gst.MessageDeviceAdded:
case gst.MessageDeviceChanged:
case gst.MessageDeviceRemoved:
case gst.MessageDurationChanged:
case gst.MessageElement:
case gst.MessageExtended:
case gst.MessageHaveContext:
case gst.MessageInfo:
case gst.MessageInstantRateRequest:
case gst.MessageLatency:
case gst.MessageNeedContext:
case gst.MessageNewClock:
case gst.MessageProgress:
case gst.MessagePropertyNotify:
case gst.MessageQos:
case gst.MessageRedirect:
case gst.MessageRequestState:
case gst.MessageResetTime:
case gst.MessageSegmentDone:
case gst.MessageSegmentStart:
case gst.MessageStateChanged:
old, state, pending := msg.ParseStateChanged()
fmt.Printf("State changed: %s => %s (%s)\n", old, state, pending)
case gst.MessageStateDirty:
case gst.MessageStepDone:
case gst.MessageStepStart:
case gst.MessageStreamCollection:
case gst.MessageStreamStart:
case gst.MessageStreamStatus:
case gst.MessageStreamsSelected:
case gst.MessageStructureChange:
case gst.MessageTag:
case gst.MessageToc:
case gst.MessageUnknown:
case gst.MessageWarning:
default:
panic("unexpected gst.MessageType")
}
}
}()
// Start the pipeline
pipeline.SetState(gst.StatePlaying)
<-ctx.Done()
pipeline.BlockSetState(gst.StateNull, gst.ClockTime(time.Second))
}