-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (110 loc) · 3.03 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"log"
"strings"
"github.com/go-gst/go-glib/glib"
"github.com/go-gst/go-gst/gst"
)
type CustomData struct {
pipeline *gst.Pipeline
source *gst.Element
convert *gst.Element
resample *gst.Element
sink *gst.Element
}
func runPipeline(mainLoop *glib.MainLoop) error {
gst.Init(nil)
data := CustomData{}
var err error
data.source, err = gst.NewElementWithName("uridecodebin", "source")
if err != nil {
log.Fatal(err)
}
data.convert, err = gst.NewElementWithName("audioconvert", "convert")
if err != nil {
log.Fatal(err)
}
data.resample, err = gst.NewElementWithName("audioresample", "resample")
if err != nil {
log.Fatal(err)
}
data.sink, err = gst.NewElementWithName("autoaudiosink", "sink")
if err != nil {
log.Fatal(err)
}
data.pipeline, err = gst.NewPipeline("test_pipeline")
if err != nil {
log.Fatal(err)
}
err = data.pipeline.AddMany(data.source, data.convert, data.resample, data.sink)
if err != nil {
log.Fatal(err)
}
err = gst.ElementLinkMany(data.convert, data.resample, data.sink)
if err != nil {
log.Fatal(err)
}
err = data.source.Set("uri", "https://gstreamer.freedesktop.org/data/media/sintel_trailer-480p.webm")
if err != nil {
log.Fatal(err)
}
data.source.Connect("pad-added", func(src *gst.Element, newPad *gst.Pad) {
sinkPad := data.convert.GetStaticPad("sink")
defer sinkPad.Unref()
if sinkPad.IsLinked() {
log.Println("sink is already linked")
return
}
log.Printf("Received new pad '%s' from '%s':\n", newPad.GetName(), src.GetName())
newPadCaps := newPad.GetCurrentCaps()
defer newPadCaps.Unref()
newPadStruct := newPadCaps.GetStructureAt(0)
newPadType := newPadStruct.Name()
if !strings.HasPrefix(newPadType, "audio/x-raw") {
log.Printf("It has type '%s' which is not raw audio. Ignoring.\n", newPadType)
return
}
ret := newPad.Link(sinkPad)
if ret == gst.PadLinkOK {
log.Printf("Link succeeded (type '%s').\n", newPadType)
} else {
log.Printf("Type is '%s' but link failed.\n", newPadType)
}
})
data.pipeline.GetBus().AddWatch(func(msg *gst.Message) bool {
switch msg.Type() {
case gst.MessageEOS:
log.Println(msg.String())
data.pipeline.BlockSetState(gst.StateNull)
mainLoop.Quit()
case gst.MessageError:
err := msg.ParseError()
log.Println("Error:", err.Error())
debug := err.DebugString()
if len(debug) > 0 {
log.Println("Debug: ", debug)
}
case gst.MessageStateChanged:
if msg.Source() == data.pipeline.GetName() {
oldState, newState := msg.ParseStateChanged()
log.Printf("Pipeline state changed from %s to %s:\n", oldState.String(), newState.String())
}
default:
log.Println("Unexpected message received.", msg.String())
}
return true
})
err = data.pipeline.SetState(gst.StatePlaying)
if err != nil {
log.Fatal(err)
}
return mainLoop.RunError()
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
mainLoop := glib.NewMainLoop(glib.MainContextDefault(), false)
if err := runPipeline(mainLoop); err != nil {
fmt.Println("ERROR!", err)
}
}