-
Notifications
You must be signed in to change notification settings - Fork 1
/
gostreamer.go
79 lines (67 loc) · 1.79 KB
/
gostreamer.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
package gostreamer
import (
"context"
"fmt"
"os/exec"
)
// Gstreamer represents a gstreamer pipe
type Gstreamer struct {
cmdName string
pipe []GstreamerPlugin
args []string
}
const defaultGstBin = "gst-launch-1.0"
// NewGstreamer creates a new Gstreamer instance
func NewGstreamer(args ...string) (*Gstreamer, error) {
return NewGstreamerFromPath(defaultGstBin, args...)
}
// NewGstreamerFromPath creates a new Gstreamer instance, uses given path to gst-launch binary
func NewGstreamerFromPath(gstBin string, args ...string) (*Gstreamer, error) {
if err := exec.Command(gstBin, "--version").Run(); err != nil {
return nil, err
}
gst := &Gstreamer{
cmdName: gstBin,
args: []string{"-v"},
}
gst.args = append(gst.args, args...)
return gst, nil
}
// PipeString outputs the current pipe
func (gp *Gstreamer) PipeString() (out string) {
for _, p := range gp.pipe {
out += fmt.Sprintf("%s ! ", p)
}
return
}
// Add add a GstreamerPlugin to the pipe
func (gp *Gstreamer) Add(p GstreamerPlugin) *Gstreamer {
gp.pipe = append(gp.pipe, p)
return gp
}
//AddStr Creates a GstreamerGenericPlugin from given string and adds it to the pipe
func (gp *Gstreamer) AddStr(p ...string) *Gstreamer {
gp.pipe = append(gp.pipe, GstreamerGenericPlugin{
cmds: p,
})
return gp
}
//AddSink adds a sink to pipe
func (gp *Gstreamer) AddSink(sink Sink) Sink {
gp.pipe = append(gp.pipe, sink)
sink.bind(gp)
return sink
}
func (gp *Gstreamer) createCmd(ctx context.Context) *exec.Cmd {
var pipeArgs = make([]string, 0, len(gp.pipe)*2)
pipeArgs = append(pipeArgs, gp.args...)
for i, p := range gp.pipe {
for _, cmd := range p.Cmds() {
pipeArgs = append(pipeArgs, cmd)
}
if i != len(gp.pipe)-1 {
pipeArgs = append(pipeArgs, "!")
}
}
return exec.CommandContext(ctx, "gst-launch-1.0", pipeArgs...)
}