forked from tinyzimmer/go-gst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (53 loc) · 1.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 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 (
"errors"
"fmt"
"os"
"strings"
"github.com/tinyzimmer/go-glib/glib"
"github.com/tinyzimmer/go-gst/examples"
"github.com/tinyzimmer/go-gst/gst"
)
func runPipeline(mainLoop *glib.MainLoop) error {
if len(os.Args) == 1 {
return errors.New("Pipeline string cannot be empty")
}
gst.Init(&os.Args)
// Let GStreamer create a pipeline from the parsed launch syntax on the cli.
pipeline, err := gst.NewPipelineFromString(strings.Join(os.Args[1:], " "))
if err != nil {
return err
}
// Add a message handler to the pipeline bus, printing interesting information to the console.
pipeline.GetPipelineBus().AddWatch(func(msg *gst.Message) bool {
switch msg.Type() {
case gst.MessageEOS: // When end-of-stream is received stop the main loop
pipeline.BlockSetState(gst.StateNull)
mainLoop.Quit()
case gst.MessageError: // Error messages are always fatal
err := msg.ParseError()
fmt.Println("ERROR:", err.Error())
if debug := err.DebugString(); debug != "" {
fmt.Println("DEBUG:", debug)
}
mainLoop.Quit()
default:
// All messages implement a Stringer. However, this is
// typically an expensive thing to do and should be avoided.
fmt.Println(msg)
}
return true
})
// Start the pipeline
pipeline.SetState(gst.StatePlaying)
// Block on the main loop
return mainLoop.RunError()
}
func main() {
examples.RunLoop(func(loop *glib.MainLoop) error {
return runPipeline(loop)
})
}