forked from go-gst/go-gst
-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
159 lines (137 loc) · 3.27 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"errors"
"fmt"
"github.com/tinyzimmer/go-glib/glib"
"github.com/tinyzimmer/go-gst/examples"
"github.com/tinyzimmer/go-gst/gst"
"os"
"time"
)
type workflow struct {
*gst.Pipeline
}
func (w *workflow) newSrc() {
src, err := gst.NewElementWithName("videotestsrc", "src2")
if err != nil {
fmt.Printf("err %v\n", err)
return
}
src.Set("is-live", true)
w.Add(src)
caps, err := gst.NewElementWithName("capsfilter", "caps2")
if err != nil {
fmt.Printf("err %v\n", err)
return
}
caps.Set("caps", gst.NewCapsFromString("video/x-raw , width=640, height=360"))
w.Add(caps)
src.Link(caps)
// Get a sink pad on compositor
mixer, err := w.GetElementByName("mixer")
if err != nil {
fmt.Printf("err %v\n", err)
return
}
pad := mixer.GetRequestPad("sink_%u")
pad.SetProperty("xpos", 640)
pad.SetProperty("ypos", 0)
caps.GetStaticPad("src").Link(pad)
caps.SyncStateWithParent()
src.SyncStateWithParent()
}
func (w *workflow) delSrc() {
mixer, err := w.GetElementByName("mixer")
if err != nil {
fmt.Printf("err %v\n", err)
return
}
src, err := w.GetElementByName("src2")
if err != nil {
fmt.Printf("err %v\n", err)
return
}
caps, err := w.GetElementByName("caps2")
if err != nil {
fmt.Printf("err %v\n", err)
return
}
pad := mixer.GetStaticPad("sink_1")
if pad == nil {
fmt.Printf("pad is null\n")
return
}
src.SetState(gst.StateNull)
caps.SetState(gst.StateNull)
w.Remove(src)
w.Remove(caps)
mixer.ReleaseRequestPad(pad)
}
func createPipeline() (*gst.Pipeline, error) {
gst.Init(nil)
var err error
var w workflow
w.Pipeline, err = gst.NewPipeline("")
if err != nil {
fmt.Println(err)
os.Exit(2)
}
elements, err := gst.NewElementMany("videotestsrc", "capsfilter", "compositor", "autovideosink")
caps := elements[1]
caps.SetProperty("caps", gst.NewCapsFromString("video/x-raw , width=640, height=360"))
caps.SetProperty("name", "caps1")
mixer := elements[2]
mixer.SetProperty("name", "mixer")
if err != nil {
fmt.Printf("err %v\n", err)
return nil, err
}
w.AddMany(elements...)
gst.ElementLinkMany(elements...)
go func() {
time.Sleep(time.Second)
w.newSrc()
time.Sleep(time.Second)
w.delSrc()
//runtime.GC()
}()
return w.Pipeline, nil
}
func runPipeline(loop *glib.MainLoop, pipeline *gst.Pipeline) error {
// 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 {
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:
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())
err = gerr
}
// If either condition triggered an error, log and quit
if err != nil {
fmt.Println("ERROR:", err.Error())
loop.Quit()
return false
}
return true
})
// Block on the main loop
return loop.RunError()
}
func main() {
examples.RunLoop(func(loop *glib.MainLoop) error {
pipeline, err := createPipeline()
if err != nil {
return err
}
return runPipeline(loop, pipeline)
})
}