port examples to new packages and add more postprocessors to reach a similar convenience feature set

This commit is contained in:
RSWilli
2025-09-16 22:36:07 +02:00
parent dc0ec1c3ee
commit 0eb8b28ee2
18 changed files with 528 additions and 1011 deletions

View File

@@ -37,48 +37,48 @@ import (
"fmt"
"os"
"strings"
"weak"
"github.com/go-gst/go-glib/glib"
"github.com/go-gst/go-gst/examples"
"github.com/go-gst/go-gst/gst"
"github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/go-gst/go-gst/pkg/gst"
)
var srcFile string
func buildPipeline() (*gst.Pipeline, error) {
gst.Init(nil)
gst.Init()
pipeline, err := gst.NewPipeline("")
if err != nil {
return nil, err
pipeline := gst.NewPipeline("")
src, ok := gst.ElementFactoryMake("filesrc", "").(*gst.Element)
if !ok {
return nil, fmt.Errorf("could not create filesource")
}
src, err := gst.NewElement("filesrc")
if err != nil {
return nil, err
decodebin, ok := gst.ElementFactoryMake("decodebin", "").(*gst.Bin)
if !ok {
return nil, fmt.Errorf("could not create decodebin")
}
decodebin, err := gst.NewElement("decodebin")
if err != nil {
return nil, err
}
src.Set("location", srcFile)
src.SetObjectProperty("location", srcFile)
pipeline.AddMany(src, decodebin)
src.Link(decodebin)
// prevent reference cycles with the connect handler:
weakDecodeBin := weak.Make(decodebin)
// Connect to decodebin's pad-added signal, that is emitted whenever
// it found another stream from the input file and found a way to decode it to its raw format.
// decodebin automatically adds a src-pad for this raw stream, which
// we can use to build the follow-up pipeline.
decodebin.Connect("pad-added", func(self *gst.Element, srcPad *gst.Pad) {
decodebin.ConnectPadAdded(func(srcPad *gst.Pad) {
// Try to detect whether this is video or audio
var isAudio, isVideo bool
caps := srcPad.GetCurrentCaps()
for i := 0; i < caps.GetSize(); i++ {
st := caps.GetStructureAt(i)
caps := srcPad.CurrentCaps()
for i := 0; i < int(caps.Size()); i++ {
st := caps.Structure(uint(i))
if strings.HasPrefix(st.Name(), "audio/") {
isAudio = true
}
@@ -93,84 +93,74 @@ func buildPipeline() (*gst.Pipeline, error) {
err := errors.New("could not detect media stream type")
// We can send errors directly to the pipeline bus if they occur.
// These will be handled downstream.
msg := gst.NewErrorMessage(self, gst.NewGError(1, err), fmt.Sprintf("Received caps: %s", caps.String()), nil)
pipeline.GetPipelineBus().Post(msg)
msg := gst.NewMessageError(weakDecodeBin.Value(), err, fmt.Sprintf("Received caps: %s", caps.String()))
pipeline.Bus().Post(msg)
return
}
if isAudio {
// decodebin found a raw audiostream, so we build the follow-up pipeline to
// play it on the default audio playback device (using autoaudiosink).
elements, err := gst.NewElementMany("queue", "audioconvert", "audioresample", "autoaudiosink")
audiosink, err := gst.ParseBinFromDescription("queue ! audioconvert ! audioresample ! autoaudiosink", true)
if err != nil {
// We can create custom errors (with optional structures) and send them to the pipeline bus.
// The first argument reflects the source of the error, the second is the error itself, followed by a debug string.
msg := gst.NewErrorMessage(self, gst.NewGError(2, err), "Could not create elements for audio pipeline", nil)
pipeline.GetPipelineBus().Post(msg)
msg := gst.NewMessageError(weakDecodeBin.Value(), err, "Could not create elements for audio pipeline")
pipeline.Bus().Post(msg)
return
}
pipeline.AddMany(elements...)
gst.ElementLinkMany(elements...)
pipeline.Add(audiosink)
// !!ATTENTION!!:
// This is quite important and people forget it often. Without making sure that
// the new elements have the same state as the pipeline, things will fail later.
// They would still be in Null state and can't process data.
for _, e := range elements {
e.SyncStateWithParent()
}
// The queue was the first element returned above
queue := elements[0]
audiosink.SyncStateWithParent()
// Get the queue element's sink pad and link the decodebin's newly created
// src pad for the audio stream to it.
sinkPad := queue.GetStaticPad("sink")
sinkPad := audiosink.StaticPad("sink")
srcPad.Link(sinkPad)
} else if isVideo {
// decodebin found a raw videostream, so we build the follow-up pipeline to
// display it using the autovideosink.
elements, err := gst.NewElementMany("queue", "videoconvert", "videoscale", "autovideosink")
videosink, err := gst.ParseBinFromDescription("queue ! videoconvert ! videoscale ! autovideosink", true)
if err != nil {
msg := gst.NewErrorMessage(self, gst.NewGError(2, err), "Could not create elements for video pipeline", nil)
pipeline.GetPipelineBus().Post(msg)
msg := gst.NewMessageError(weakDecodeBin.Value(), err, "Could not create elements for video pipeline")
pipeline.Bus().Post(msg)
return
}
pipeline.AddMany(elements...)
gst.ElementLinkMany(elements...)
pipeline.Add(videosink)
for _, e := range elements {
e.SyncStateWithParent()
}
videosink.SyncStateWithParent()
queue := elements[0]
// Get the queue element's sink pad and link the decodebin's newly created
// src pad for the video stream to it.
sinkPad := queue.GetStaticPad("sink")
sinkPad := videosink.StaticPad("sink")
srcPad.Link(sinkPad)
}
})
return pipeline, nil
}
func runPipeline(loop *glib.MainLoop, pipeline *gst.Pipeline) error {
func runPipeline(loop *glib.MainLoop, pipeline *gst.Pipeline) {
// Start the pipeline
pipeline.SetState(gst.StatePlaying)
// Add a message watch to the bus to quit on any error
pipeline.GetPipelineBus().AddWatch(func(msg *gst.Message) bool {
pipeline.Bus().AddWatch(0, func(bus *gst.Bus, msg *gst.Message) bool {
var err error
// If the stream has ended or any element posts an error to the
// bus, populate error.
switch msg.Type() {
case gst.MessageEOS:
case gst.MessageEos:
err = errors.New("end-of-stream")
case gst.MessageError:
// The parsed error implements the error interface, but also
// contains additional debug information.
gerr := msg.ParseError()
fmt.Println("go-gst-debug:", gerr.DebugString())
gerr, debug := msg.ParseError()
fmt.Println("go-gst-debug:", debug)
err = gerr
}
@@ -185,7 +175,7 @@ func runPipeline(loop *glib.MainLoop, pipeline *gst.Pipeline) error {
})
// Block on the main loop
return loop.RunError()
loop.Run()
}
func main() {
@@ -195,11 +185,14 @@ func main() {
flag.Usage()
os.Exit(1)
}
examples.RunLoop(func(loop *glib.MainLoop) error {
pipeline, err := buildPipeline()
if err != nil {
return err
}
return runPipeline(loop, pipeline)
})
pipeline, err := buildPipeline()
if err != nil {
panic(err)
}
mainloop := glib.NewMainLoop(glib.MainContextDefault(), false)
runPipeline(mainloop, pipeline)
}