-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
210 lines (165 loc) · 4.18 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/spf13/pflag"
)
var gstformat = `gst-launch-1.0 filesrc location="%s" !
decodebin name=decode !
videoconvert !
x264enc option-string=slice-max-size=1200 speed-preset=medium tune=zerolatency key-int-max=1 !
video/x-h264,profile=constrained-baseline !
queue max-size-time=100000000 !
rtph264pay config-interval=1 name=payloader !
multifilesink location="%s"`
func checkFatal(err error) {
if err != nil {
_, fileName, fileLine, _ := runtime.Caller(1)
log.Fatalf("FATAL %s:%d %v", filepath.Base(fileName), fileLine, err)
}
}
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if errors.Is(err, os.ErrNotExist) {
return false
}
checkFatal(err)
}
return true
}
var hostport = pflag.String("hostport", ":8088", "addr:port to bind and listen for http")
func main() {
var err error
pflag.Parse()
httpmode := false
if len(pflag.Args()) == 0 {
httpmode = true
println("http server on 8080")
} else if len(pflag.Args()) == 2 {
httpmode = false
} else {
checkFatal(fmt.Errorf("no argments for http, or two args: infile,outfile for file mode"))
}
if !httpmode {
infile := pflag.Args()[0]
outfile := pflag.Args()[1]
if !Exists(infile) {
checkFatal(fmt.Errorf("input file does not exist"))
}
zipbuf, err := runGstreamer(infile)
checkFatal(err)
err = ioutil.WriteFile(outfile, zipbuf, 0666)
checkFatal(err)
log.Println("output written to ", outfile)
return
}
log.Println("No --input flag, waiting for http requests")
http.HandleFunc("/idle-clip", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
httpError(fmt.Errorf("http POST ONLY"), w)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
httpError(err, w)
return
}
infile, err := ioutil.TempFile("", "idle-server-input")
if err != nil {
httpError(err, w)
return
}
defer os.Remove(infile.Name())
log.Println("temp file is ", infile.Name())
n, err := infile.Write(body)
if err != nil {
httpError(err, w)
return
}
if n != len(body) {
httpError(fmt.Errorf("bad file write len"), w)
return
}
zipbuf, err := runGstreamer(infile.Name())
if err != nil {
httpError(err, w)
return
}
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment; filename='idle-clip.zip'")
n, err = w.Write(zipbuf)
if err != nil {
httpError(err, w)
return
}
if n != len(zipbuf) {
httpError(fmt.Errorf("bad resp write len"), w)
return
}
})
err = http.ListenAndServe(*hostport, nil)
checkFatal(err)
}
func httpError(err error, rw http.ResponseWriter) {
log.Println(err.Error())
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
func runGstreamer(infile string) ([]byte, error) {
outdir, err := ioutil.TempDir("", "idle-server-dir")
if err != nil {
return nil, err
}
defer os.RemoveAll(outdir)
var gstcmd = fmt.Sprintf(gstformat, infile, outdir+"/rtp%d.rtp")
log.Printf("Running command2")
args := strings.Fields(gstcmd)
cmd := exec.Command(args[0], args[1:]...)
stdoutStderr, err := cmd.CombinedOutput()
log.Printf("output: %s\n", stdoutStderr)
checkFatal(err)
if err != nil {
return nil, fmt.Errorf("cmd.CombinedOutput() %w", err)
}
log.Printf("Command err code: %v", err)
// rtpfiles, err := ioutil.ReadDir(outdir)
// if err != nil {
// return nil, fmt.Errorf("ioutil.ReadDir(outdir) %w", err)
// }
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
i := 0
for ; i < 1000000; i++ {
base := fmt.Sprintf("rtp%d.rtp", i)
fullname := path.Join(outdir, base)
pktbody, err := ioutil.ReadFile(fullname)
if errors.Is(err, os.ErrNotExist) {
log.Println(fullname, "not found")
break
}
if err != nil {
return nil, fmt.Errorf("ioutil.ReadFile(...) %w", err)
}
f, err := w.Create(base)
if err != nil {
return nil, fmt.Errorf("w.Create() %w", err)
}
_, err = f.Write(pktbody)
if err != nil {
return nil, fmt.Errorf("f.Write(pktbody) %w", err)
}
}
w.Close() //important
log.Println(i, "packets zipped up")
return buf.Bytes(), nil
}