From f9f4fd69509ed4e31320e541cfdcf88add485939 Mon Sep 17 00:00:00 2001 From: Quanqi Gu Date: Fri, 19 Mar 2021 17:34:29 +0800 Subject: [PATCH] FIX utils.Conf() would not read easy-darwin.ini configuration due to Server Object initialization --- routers/stats.go | 4 ++-- routers/sys.go | 4 ++-- rtsp/rtsp-server.go | 24 +++++++++++++++--------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/routers/stats.go b/routers/stats.go index cd0173e2..16173a97 100644 --- a/routers/stats.go +++ b/routers/stats.go @@ -49,7 +49,7 @@ func (h *APIHandler) Pushers(c *gin.Context) { } hostname := utils.GetRequestHostname(c.Request) pushers := make([]interface{}, 0) - for _, pusher := range rtsp.Instance.GetPushers() { + for _, pusher := range rtsp.GetServer().GetPushers() { port := pusher.Server().TCPPort rtsp := fmt.Sprintf("rtsp://%s:%d%s", hostname, port, pusher.Path()) if port == 554 { @@ -102,7 +102,7 @@ func (h *APIHandler) Players(c *gin.Context) { return } players := make([]*rtsp.Player, 0) - for _, pusher := range rtsp.Instance.GetPushers() { + for _, pusher := range rtsp.GetServer().GetPushers() { for _, player := range pusher.GetPlayers() { players = append(players, player) } diff --git a/routers/sys.go b/routers/sys.go index 0792e15a..c8d8598e 100644 --- a/routers/sys.go +++ b/routers/sys.go @@ -50,9 +50,9 @@ func init() { now := utils.DateTime(time.Now()) memData = append(memData, PercentData{Time: now, Used: mem.UsedPercent / 100}) cpuData = append(cpuData, PercentData{Time: now, Used: cpu[0] / 100}) - pusherData = append(pusherData, CountData{Time: now, Total: uint(rtsp.Instance.GetPusherSize())}) + pusherData = append(pusherData, CountData{Time: now, Total: uint(rtsp.GetServer().GetPusherSize())}) playerCnt := 0 - for _, pusher := range rtsp.Instance.GetPushers() { + for _, pusher := range rtsp.GetServer().GetPushers() { playerCnt += len(pusher.GetPlayers()) } playerData = append(playerData, CountData{Time: now, Total: uint(playerCnt)}) diff --git a/rtsp/rtsp-server.go b/rtsp/rtsp-server.go index 171f2483..b24177da 100644 --- a/rtsp/rtsp-server.go +++ b/rtsp/rtsp-server.go @@ -27,17 +27,23 @@ type Server struct { removePusherCh chan *Pusher } -var Instance *Server = &Server{ - SessionLogger: SessionLogger{log.New(os.Stdout, "[RTSPServer]", log.LstdFlags|log.Lshortfile)}, - Stoped: true, - TCPPort: utils.Conf().Section("rtsp").Key("port").MustInt(554), - pushers: make(map[string]*Pusher), - addPusherCh: make(chan *Pusher), - removePusherCh: make(chan *Pusher), -} +var ( + instance *Server + once sync.Once +) func GetServer() *Server { - return Instance + once.Do(func() { + instance = &Server{ + SessionLogger: SessionLogger{log.New(os.Stdout, "[RTSPServer]", log.LstdFlags|log.Lshortfile)}, + Stoped: true, + TCPPort: utils.Conf().Section("rtsp").Key("port").MustInt(554), + pushers: make(map[string]*Pusher), + addPusherCh: make(chan *Pusher), + removePusherCh: make(chan *Pusher), + } + }) + return instance } func (server *Server) Start() (err error) {