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) {