-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsinks.go
143 lines (119 loc) · 3.33 KB
/
sinks.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
package main
import (
"bufio"
"io"
"os"
"os/exec"
"syscall"
log "github.com/s00500/env_logger"
)
type StreamSink interface {
StartInstance() (io.WriteCloser, func())
}
type FFPlaySink struct {
}
func (sink FFPlaySink) StartInstance() (io.WriteCloser, func()) {
additionalArgs := []string{"-fast", "-flags2", "fast", "-fflags", "nobuffer", "-flags", "low_delay", "-strict", "experimental", "-vf", "setpts=N/60/TB", "-framedrop", "-sync", "ext", "-probesize", "32", "-analyzeduration", "0"}
args := []string{"-i", "-"}
args = append(args, additionalArgs...)
cmd := exec.Command("ffplay", args...)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal("Could not get ffplay stdin")
}
log.MustFatal(cmd.Start())
return stdin, func() {
cmd.Process.Signal(syscall.SIGKILL) // Not ellegant... could try sigterm and wait before...
}
}
type GstSink struct {
Args []string
}
func (sink GstSink) StartInstance() (io.WriteCloser, func()) {
cmd := exec.Command("gst-launch-1.0", sink.Args...)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal("Could not get gstreamer stdin")
}
log.MustFatal(cmd.Start())
return stdin, func() {
cmd.Process.Signal(syscall.SIGKILL) // Not ellegant... could try sigterm and wait before...
}
}
type UdpSink struct {
}
func (sink UdpSink) StartInstance() (io.WriteCloser, func()) {
additionalArgs := []string{"-vcodec", "mpeg4", "-v", "0", "-f", "mpegts", "udp://127.0.0.1:23000"}
args := []string{"-i", "-"}
args = append(args, additionalArgs...)
cmd := exec.Command("ffmpeg", args...)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal("Could not get ffmpeg stdin")
}
log.MustFatal(cmd.Start())
return stdin, func() {
cmd.Process.Signal(syscall.SIGKILL) // Not ellegant... could try sigterm and wait before...
}
}
type FifoSink struct {
Path string
}
func (sink FifoSink) StartInstance() (io.WriteCloser, func()) {
os.Remove(sink.Path)
err := syscall.Mkfifo(sink.Path, 0666)
if err != nil {
log.Fatal("Make named pipe file error ", sink.Path, " ", err)
}
f, err := os.OpenFile(sink.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
log.MustFatal(err)
return f, func() {
f.Close()
os.Remove(sink.Path)
}
}
type FileSink struct {
Path string
}
func (sink FileSink) StartInstance() (io.WriteCloser, func()) {
f, err := os.OpenFile(sink.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
log.MustFatal(err)
return f, func() {
f.Close()
}
}
type HelloVideoSink struct {
}
func (sink HelloVideoSink) StartInstance() (io.WriteCloser, func()) {
logger := log.GetLoggerForPrefix("hellosink")
cmd := exec.Command("/etc/hello_video.bin")
cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH=/opt/vc/lib")
stdin, err := cmd.StdinPipe()
if err != nil {
logger.Fatal("Could not get hellovideo stdin")
}
stderr, err := cmd.StderrPipe()
if err != nil {
logger.Fatal("Could not get hellovideo stderr")
}
go func() {
errScanner := bufio.NewScanner(stderr)
for errScanner.Scan() {
logger.Error(errScanner.Text())
}
}()
stdout, err := cmd.StdoutPipe()
if err != nil {
logger.Fatal("Could not get hellovideo stdout")
}
go func() {
outScanner := bufio.NewScanner(stdout)
for outScanner.Scan() {
logger.Warn(outScanner.Text())
}
}()
log.MustFatal(cmd.Start())
return stdin, func() {
cmd.Process.Signal(syscall.SIGKILL) // Not ellegant... could try sigterm and wait before...
}
}