1 Star 0 Fork 4

mech001/EasyDarwin

forked from DLGCY_GB28181/EasyDarwin 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 5.34 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/penggy/EasyGoLib/db"
"github.com/EasyDarwin/EasyDarwin/models"
"github.com/EasyDarwin/EasyDarwin/routers"
"github.com/EasyDarwin/EasyDarwin/rtsp"
figure "github.com/common-nighthawk/go-figure"
"github.com/penggy/EasyGoLib/utils"
"github.com/penggy/service"
)
var (
gitCommitCode string
buildDateTime string
)
type program struct {
httpPort int
httpServer *http.Server
rtspPort int
rtspServer *rtsp.Server
}
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("http://%s:%d", utils.LocalIP(), p.httpPort)
log.Println("http server start -->", link)
go func() {
if err := p.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Println("start http server error", err)
}
log.Println("http 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("rtsp://%s%s", utils.LocalIP(), sport)
log.Println("rtsp server start -->", link)
go func() {
if err := p.rtspServer.Start(); 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
}
err = models.Init()
if err != nil {
return
}
err = routers.Init()
if err != nil {
return
}
p.StartRTSP()
p.StartHTTP()
if !utils.Debug {
log.Println("log files -->", utils.LogDir())
log.SetOutput(utils.GetLogWriter())
}
go func() {
for range routers.API.RestartChan {
p.StopHTTP()
p.StopRTSP()
utils.ReloadConf()
p.StartRTSP()
p.StartHTTP()
}
}()
go func() {
log.Printf("demon pull streams")
for {
var streams []models.Stream
db.SQLite.Find(&streams)
if err := db.SQLite.Find(&streams).Error; err != nil {
log.Printf("find stream err:%v", err)
return
}
for i := len(streams) - 1; i > -1; i-- {
v := streams[i]
if rtsp.GetServer().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.URL, int64(v.HeartbeatInterval)*1000, agent)
if err != nil {
continue
}
client.CustomPath = v.CustomPath
err = client.Start(time.Duration(v.IdleTimeout) * time.Second)
if err != nil {
log.Printf("Pull stream err :%v", err)
continue
}
pusher := rtsp.NewClientPusher(client)
rtsp.GetServer().AddPusher(pusher)
//streams = streams[0:i]
//streams = append(streams[:i], streams[i+1:]...)
}
time.Sleep(10 * time.Second)
}
}()
return
}
func (p *program) Stop(s service.Service) (err error) {
defer log.Println("********** STOP **********")
defer utils.CloseLogWriter()
p.StopHTTP()
p.StopRTSP()
models.Close()
return
}
func main() {
flag.StringVar(&utils.FlagVarConfFile, "config", "", "configure file path")
flag.Parse()
tail := flag.Args()
// log
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
sec := utils.Conf().Section("service")
svcConfig := &service.Config{
Name: sec.Key("name").MustString("EasyDarwin_Service"),
DisplayName: sec.Key("display_name").MustString("EasyDarwin_Service"),
Description: sec.Key("description").MustString("EasyDarwin_Service"),
}
httpPort := utils.Conf().Section("http").Key("port").MustInt(10008)
rtspServer := rtsp.GetServer()
p := &program{
httpPort: httpPort,
rtspPort: rtspServer.TCPPort,
rtspServer: rtspServer,
}
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()
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mech001/EasyDarwin.git
git@gitee.com:mech001/EasyDarwin.git
mech001
EasyDarwin
EasyDarwin
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385