-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtsp_server.go
111 lines (96 loc) · 3.48 KB
/
rtsp_server.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
package main
import (
"fmt"
"log"
"os/exec"
"strings"
)
var rtspProcesses map[string]*exec.Cmd
var ffmpegProcesses map[string]*exec.Cmd
const (
nodeCommand string = "node"
nodeRtspPath string = "/home/pi/project/locuste/services/locuste.service.brain/node/rtsp-socket.js"
ffmpegCommand string = "ffmpeg -f rtsp -rtsp_transport udp -i rtsp://%s/live -f mpegts -codec:v mpeg1video -q 2 -an -b:v 50M -threads 32 -strict experimental -bf 0 -r 25 -muxdelay 0.001 http://localhost:%d/anafi"
)
func initRtspListener() {
rtspProcesses = make(map[string]*exec.Cmd)
ffmpegProcesses = make(map[string]*exec.Cmd)
}
func startVideoServer(name string, port int) {
renew := false
if process, ok := rtspProcesses[name]; ok {
if process == nil || process.ProcessState != nil && process.ProcessState.Exited() || process.Process == nil {
log.Println("Le processus JSMPEG-TS est HS pour ", name, " au port ", port, ". On redémarre")
renew = true
} else {
log.Println("Processus JSMPEG-TS déjà OK pour ", name, " au port ", port)
}
} else {
renew = true
}
if renew {
log.Println("Démarrage du processus de visulation pour ", name, " au port ", port)
rtspProcesses[name] = exec.Command(string(nodeCommand), string(nodeRtspPath), "anafi", string(fmt.Sprintf("%d", port+50)), string(fmt.Sprintf("%d", port)))
if err := rtspProcesses[name].Start(); err != nil {
failOnError(err, "Le processus JSMPEG-TS n'a pas pu démarrer")
}
go func(processName string) {
defer func() {
if r := recover(); r != nil {
AddOrUpdateExtCompStatus(processName, VideoServer, false)
}
}()
AddOrUpdateExtCompStatus(processName, VideoServer, true)
err := rtspProcesses[processName].Wait()
log.Println("Processus JSMPEG-TS arrêté pour ", name, " raison ", err)
AddOrUpdateExtCompStatus(processName, VideoServer, false)
}(name)
log.Println("Processus JSMPEG-TS OK pour ", name, " at ", port)
}
}
func startFfmpegStream(name string, port int) {
renew := false
if process, ok := ffmpegProcesses[name]; ok {
if process == nil || process.ProcessState != nil && process.ProcessState.Exited() || process.Process == nil {
log.Println("Le processus RTSP->FFMPEG est HS pour ", name, " au port ", port, ". On redémarre")
renew = true
} else {
log.Println("Processus RTSP->FFMPEG déjà OK pour ", name, " au port ", port)
}
} else {
renew = true
}
if renew {
log.Println("Démarrage du processus de visulation pour ", name, " au port ", port)
dip := name
if index := strings.LastIndex(name, "_"); index != -1 {
dip = name[index+1:]
}
command := strings.Fields(fmt.Sprintf(ffmpegCommand, dip, port+50))
ffmpegProcesses[name] = exec.Command(command[0], command[1:]...)
if err := ffmpegProcesses[name].Start(); err != nil {
AddOrUpdateExtCompStatus(name, VideoStream, false)
failOnError(err, "Le processus RTSP->FFMPEG n'a pas pu démarrer")
}
go func(processName string) {
defer func() {
if r := recover(); r != nil {
AddOrUpdateExtCompStatus(name, VideoStream, false)
}
}()
AddOrUpdateExtCompStatus(name, VideoStream, true)
err := ffmpegProcesses[processName].Wait()
log.Println("Processus RTSP->FFMPEG arrêté pour ", name, " raison ", err)
AddOrUpdateExtCompStatus(name, VideoStream, false)
}(name)
log.Println("Processus RTSP->FFMPEG OK pour ", name, " at ", port)
}
}
func onClose() {
for _, process := range rtspProcesses {
process.Process.Kill()
}
for _, process := range ffmpegProcesses {
process.Process.Kill()
}
}