-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
266 lines (245 loc) · 6.29 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/EasyDarwin/EasyDarwin/models"
"github.com/EasyDarwin/EasyDarwin/routers"
"github.com/EasyDarwin/EasyDarwin/rtsp"
"github.com/EasyDarwin/EasyDarwin/utils"
figure "github.com/common-nighthawk/go-figure"
"github.com/EasyDarwin/EasyDarwin/service"
)
var (
gitCommitCode string
buildDateTime string
)
type program struct {
httpPort int
httpServer *http.Server
rtspPort int
rtspServer *rtsp.Server
cert string
key string
streamSecret string
}
func (p *program) StopHTTP() (err error) {
if p.httpServer == nil {
err = fmt.Errorf("HTTP Server Not Found")
return
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err = p.httpServer.Shutdown(ctx); err != nil {
return
}
return
}
func (p *program) StartHTTP() (err error) {
p.httpServer = &http.Server{
Addr: fmt.Sprintf(":%d", p.httpPort),
Handler: routers.Router,
ReadHeaderTimeout: 5 * time.Second,
}
link := fmt.Sprintf("https://%s:%d", utils.LocalIP(), p.httpPort)
log.Println("https server start -->", link)
go func() {
if err := p.httpServer.ListenAndServeTLS(p.cert, p.key); err != nil && err != http.ErrServerClosed {
log.Println("start http server error", err)
}
log.Println("https server end")
}()
return
}
func (p *program) StartRTSP() (err error) {
if p.rtspServer == nil {
err = fmt.Errorf("RTSP Server Not Found")
return
}
sport := ""
if p.rtspPort != 554 {
sport = fmt.Sprintf(":%d", p.rtspPort)
}
link := fmt.Sprintf("rtsps://%s%s", utils.LocalIP(), sport)
log.Println("rtsp server start -->", link)
go func() {
if err := p.rtspServer.Start(p.cert, p.key, p.streamSecret); err != nil {
log.Println("start rtsp server error", err)
}
log.Println("rtsp server end")
}()
return
}
func (p *program) StopRTSP() (err error) {
if p.rtspServer == nil {
err = fmt.Errorf("RTSP Server Not Found")
return
}
p.rtspServer.Stop()
return
}
func (p *program) Start(s service.Service) (err error) {
log.Println("********** START **********")
if utils.IsPortInUse(p.httpPort) {
err = fmt.Errorf("HTTP port[%d] In Use", p.httpPort)
return
}
if utils.IsPortInUse(p.rtspPort) {
err = fmt.Errorf("RTSP port[%d] In Use", p.rtspPort)
return
}
// Init API server
err = routers.Init()
if err != nil {
return
}
p.StartRTSP()
p.StartHTTP()
// TODO: log sestup
go func() {
for range routers.API.RestartChan {
p.StopHTTP()
p.StopRTSP()
// utils.ReloadConf()
// TODO : reload config and init
p.StartRTSP()
p.StartHTTP()
}
}()
// Restart pushers
go func() {
log.Printf("demon pull streams")
for {
var streams []*models.Stream
streams, err := models.GetAllStream()
if err != nil {
log.Printf("find stream err:%v", err)
time.Sleep(10 * time.Second)
continue
}
for _, v := range streams {
if nil != rtsp.Instance.GetPusher(v.CustomPath, nil) {
continue
}
agent := fmt.Sprintf("EasyDarwinGo/%s", routers.BuildVersion)
if routers.BuildDateTime != "" {
agent = fmt.Sprintf("%s(%s)", agent, routers.BuildDateTime)
}
client, err := rtsp.NewRTSPClient(
rtsp.GetServer(), v.ID, v.URL, int64(v.HeartbeatInterval)*1000, agent)
if err != nil {
continue
}
client.CustomPath = v.CustomPath
pusher := rtsp.NewClientPusher(client)
err = client.Start(time.Duration(v.IdleTimeout) * time.Second)
if err != nil {
log.Printf("Pull stream err :%v", err)
continue
}
rtsp.GetServer().AddPusher(pusher)
//streams = streams[0:i]
//streams = append(streams[:i], streams[i+1:]...)
}
time.Sleep(10 * time.Second)
}
}()
// Restart record
go func() {
log.Printf("demon pull recorders")
for {
tasks := models.GetAllTasks()
for _, task := range tasks {
log.Print(task.String())
// TODO: More safe pusher.AddPlayer
pusher := rtsp.Instance.GetPusher(task.PlayPath, nil)
if nil == pusher {
continue
}
if nil != pusher.GetPlayer(task.ID) {
continue
}
log.Printf("Task[%s] off, restarting", task.ID)
recorder, err := rtsp.NewRecorder(task, pusher)
if nil != err {
log.Printf("NewRecorder error[%v]", err)
continue
}
if err = pusher.AddPlayer(recorder); nil != err {
log.Printf("Addplayer error[%v]", err)
continue
}
}
time.Sleep(10 * time.Second)
}
}()
return
}
func (p *program) Stop(s service.Service) (err error) {
defer log.Println("********** STOP **********")
// defer utils.CloseLogWriter()
// TODO: stop log
p.StopHTTP()
p.StopRTSP()
return
}
func main() {
// TODO: input config filepathf
// flag.StringVar(&utils.FlagVarConfFile, "config", "", "configure file path")
flag.Parse()
tail := flag.Args()
// log
log.SetOutput(os.Stdout)
log.SetPrefix("[EasyDarwin] ")
log.SetFlags(log.Lshortfile | log.LstdFlags)
log.Printf("git commit code:%s", gitCommitCode)
log.Printf("build date:%s", buildDateTime)
routers.BuildVersion = fmt.Sprintf("%s.%s", routers.BuildVersion, gitCommitCode)
routers.BuildDateTime = buildDateTime
svcConfig := &service.Config{
Name: config.Service.Name,
DisplayName: config.Service.DisplayName,
Description: config.Service.Description,
}
httpPort := config.HTTP.Port
cert := utils.Conf().Section("tls").Key("cert").MustString("")
key := utils.Conf().Section("tls").Key("key").MustString("")
streamSecret := utils.Conf().Section("rtsp").Key("stream_secret_key").MustString("")
rtspServer := rtsp.GetServer()
p := &program{
httpPort: httpPort,
rtspPort: rtspServer.TCPPort,
rtspServer: rtspServer,
cert: cert,
key: key,
streamSecret: streamSecret,
}
s, err := service.New(p, svcConfig)
if err != nil {
log.Println(err)
utils.PauseExit()
}
if len(tail) > 0 {
cmd := strings.ToLower(tail[0])
if cmd == "install" || cmd == "stop" || cmd == "start" || cmd == "uninstall" {
figure.NewFigure("EasyDarwin", "", false).Print()
log.Println(svcConfig.Name, cmd, "...")
if err = service.Control(s, cmd); err != nil {
log.Println(err)
utils.PauseExit()
}
log.Println(svcConfig.Name, cmd, "ok")
return
}
}
figure.NewFigure("EasyDarwin", "", false).Print()
if err = s.Run(); err != nil {
log.Println(err)
utils.PauseExit()
}
}