generated from streamingriver/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupervisor.go
105 lines (90 loc) · 2.56 KB
/
supervisor.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
package main
import (
"io"
"io/ioutil"
"log"
"os/exec"
"strings"
)
// Supervisor generates real config files
type Supervisor struct {
App *App
Logger *log.Logger
Output io.WriteCloser
}
func (sv Supervisor) templateFFMPEG(name, url string) string {
template := `[program:{{name}}]
command=/bin/bash -c "mkdir -p /dev/shm/{{name}}; cd /dev/shm/{{name}}; /ffmpeg -nostats -nostdin -user-agent "streamingriveriptv/1.0" -i "{{url}}" -codec copy -map 0:0 -map 0:1 -map_metadata 0 -f hls -hls_list_size 3 -hls_flags delete_segments -hls_time 5 -segment_list_size 3 -hls_segment_filename file%%07d.ts stream.m3u8"
autostart = true
startsec = 1
user = root
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/dev/fd/2
stderr_logfile_maxbytes=0
autorestart=true
startretries=5000000000
stopasgroup=true
killasgroup=true
stdout_events_enabled=true
stderr_events_enabled=true
`
template = strings.ReplaceAll(template, "{{name}}", name)
template = strings.ReplaceAll(template, "{{url}}", url)
return template
}
func (sv Supervisor) templateCache(name, url string) string {
template := `[program:proxy-{{name}}]
command=/opt/tools/hls-proxy_linux_amd64 --url "http://localhost:9005/{{name}}/stream.m3u8" --name "{{name}}" --frontend http://127.0.0.1:8085
autostart = true
startsec = 1
user = root
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/dev/fd/2
stderr_logfile_maxbytes=0
autorestart=true
startretries=5000000000
stopasgroup=true
killasgroup=true
stdout_events_enabled=true
stderr_events_enabled=true
`
template = strings.ReplaceAll(template, "{{name}}", name)
template = strings.ReplaceAll(template, "{{url}}", url)
return template
}
// GenerateFFMPEG file
func (sv *Supervisor) GenerateFFMPEG(programs []Program) {
sv.generate(programs, "ffmpeg")
}
// GenerateCache file
func (sv *Supervisor) GenerateCache(programs []Program) {
sv.generate(programs, "cache")
}
func (sv *Supervisor) generate(programs []Program, t string) {
output := ""
for _, program := range programs {
if t == "ffmpeg" {
output += sv.templateFFMPEG(program.Name, program.URL)
} else {
output += sv.templateCache(program.Name, program.URL)
}
}
if sv.Output != nil {
sv.Output.Write([]byte(output))
return
}
err := ioutil.WriteFile(sv.App.path+"/"+t+sv.App.ext, []byte(output), 0755)
if err != nil {
sv.Logger.Printf("%s", err)
}
sv.reload()
}
func (sv *Supervisor) reload() {
cmd := exec.Command(sv.App.SupervisorPath, "-c", sv.App.SupervisorConfig, "reload")
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("%v (%s)", err, output)
}
}