diff --git a/apiHTTPChannel.go b/apiHTTPChannel.go index 6648cf2..0d322c1 100644 --- a/apiHTTPChannel.go +++ b/apiHTTPChannel.go @@ -7,26 +7,25 @@ import ( //HTTPAPIServerStreamChannelCodec function return codec info struct func HTTPAPIServerStreamChannelCodec(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_stream", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamChannelCodec", + }) + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamChannelCodec", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamChannelCodec", - "call": "StreamChannelCodec", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelCodec", }).Errorln(err.Error()) return } @@ -69,28 +68,27 @@ func HTTPAPIServerStreamChannelReload(c *gin.Context) { //HTTPAPIServerStreamChannelEdit function edit stream func HTTPAPIServerStreamChannelEdit(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_stream", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamChannelEdit", + }) + var payload ChannelST err := c.BindJSON(&payload) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamEdit", - "call": "BindJSON", + requestLogger.WithFields(logrus.Fields{ + "call": "BindJSON", }).Errorln(err.Error()) return } err = Storage.StreamChannelEdit(c.Param("uuid"), c.Param("channel"), payload) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamChannelEdit", - "call": "StreamChannelEdit", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelEdit", }).Errorln(err.Error()) return } @@ -99,15 +97,18 @@ func HTTPAPIServerStreamChannelEdit(c *gin.Context) { //HTTPAPIServerStreamChannelDelete function delete stream func HTTPAPIServerStreamChannelDelete(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_stream", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamChannelDelete", + }) + err := Storage.StreamChannelDelete(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamChannelDelete", - "call": "StreamChannelDelete", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelDelete", }).Errorln(err.Error()) return } @@ -116,28 +117,27 @@ func HTTPAPIServerStreamChannelDelete(c *gin.Context) { //HTTPAPIServerStreamChannelAdd function add new stream func HTTPAPIServerStreamChannelAdd(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_stream", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamChannelAdd", + }) + var payload ChannelST err := c.BindJSON(&payload) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamChannelAdd", - "call": "BindJSON", + requestLogger.WithFields(logrus.Fields{ + "call": "BindJSON", }).Errorln(err.Error()) return } err = Storage.StreamChannelAdd(c.Param("uuid"), c.Param("channel"), payload) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamChannelAdd", - "call": "StreamChannelAdd", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelAdd", }).Errorln(err.Error()) return } diff --git a/apiHTTPHLS.go b/apiHTTPHLS.go index af9ff8b..72008f7 100644 --- a/apiHTTPHLS.go +++ b/apiHTTPHLS.go @@ -9,16 +9,19 @@ import ( "github.com/sirupsen/logrus" ) -//HTTPAPIServerStreamHLSTS send client m3u8 play list +//HTTPAPIServerStreamHLSM3U8 send client m3u8 play list func HTTPAPIServerStreamHLSM3U8(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_hls", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamHLSM3U8", + }) + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSM3U8", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } @@ -29,12 +32,8 @@ func HTTPAPIServerStreamHLSM3U8(c *gin.Context) { index, seq, err := Storage.StreamHLSm3u8(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSM3U8", - "call": "StreamHLSm3u8", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamHLSm3u8", }).Errorln(err.Error()) return } @@ -42,12 +41,8 @@ func HTTPAPIServerStreamHLSM3U8(c *gin.Context) { _, err := c.Writer.Write([]byte(index)) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSM3U8", - "call": "Write", + requestLogger.WithFields(logrus.Fields{ + "call": "Write", }).Errorln(err.Error()) return } @@ -59,26 +54,25 @@ func HTTPAPIServerStreamHLSM3U8(c *gin.Context) { //HTTPAPIServerStreamHLSTS send client ts segment func HTTPAPIServerStreamHLSTS(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_hls", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamHLSTS", + }) + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "StreamCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamCodecs", }).Errorln(err.Error()) return } @@ -88,35 +82,23 @@ func HTTPAPIServerStreamHLSTS(c *gin.Context) { err = Muxer.WriteHeader(codecs) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "WriteHeader", + requestLogger.WithFields(logrus.Fields{ + "call": "WriteHeader", }).Errorln(err.Error()) return } seqData, err := Storage.StreamHLSTS(c.Param("uuid"), c.Param("channel"), stringToInt(c.Param("seq"))) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "StreamHLSTS", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamHLSTS", }).Errorln(err.Error()) return } if len(seqData) == 0 { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotHLSSegments.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "seqData", + requestLogger.WithFields(logrus.Fields{ + "call": "seqData", }).Errorln(ErrorStreamNotHLSSegments.Error()) return } @@ -125,12 +107,8 @@ func HTTPAPIServerStreamHLSTS(c *gin.Context) { err = Muxer.WritePacket(*v) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "WritePacket", + requestLogger.WithFields(logrus.Fields{ + "call": "WritePacket", }).Errorln(err.Error()) return } @@ -138,24 +116,16 @@ func HTTPAPIServerStreamHLSTS(c *gin.Context) { err = Muxer.WriteTrailer() if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "WriteTrailer", + requestLogger.WithFields(logrus.Fields{ + "call": "WriteTrailer", }).Errorln(err.Error()) return } _, err = c.Writer.Write(outfile.Bytes()) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hls", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSTS", - "call": "Write", + requestLogger.WithFields(logrus.Fields{ + "call": "Write", }).Errorln(err.Error()) return } diff --git a/apiHTTPHLSLL.go b/apiHTTPHLSLL.go index 8357dc5..48f5100 100644 --- a/apiHTTPHLSLL.go +++ b/apiHTTPHLSLL.go @@ -9,14 +9,17 @@ import ( //HTTPAPIServerStreamHLSLLInit send client ts segment func HTTPAPIServerStreamHLSLLInit(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_hlsll", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamHLSLLInit", + }) + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLInit", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } @@ -25,12 +28,8 @@ func HTTPAPIServerStreamHLSLLInit(c *gin.Context) { codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLInit", - "call": "StreamChannelCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelCodecs", }).Errorln(err.Error()) return } @@ -38,12 +37,8 @@ func HTTPAPIServerStreamHLSLLInit(c *gin.Context) { err = Muxer.WriteHeader(codecs) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLInit", - "call": "WriteHeader", + requestLogger.WithFields(logrus.Fields{ + "call": "WriteHeader", }).Errorln(err.Error()) return } @@ -52,28 +47,26 @@ func HTTPAPIServerStreamHLSLLInit(c *gin.Context) { _, err = c.Writer.Write(buf) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLInit", - "call": "Write", + requestLogger.WithFields(logrus.Fields{ + "call": "Write", }).Errorln(err.Error()) return } - } //HTTPAPIServerStreamHLSLLM3U8 send client m3u8 play list func HTTPAPIServerStreamHLSLLM3U8(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_hlsll", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamHLSLLM3U8", + }) + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM3U8", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } @@ -81,23 +74,15 @@ func HTTPAPIServerStreamHLSLLM3U8(c *gin.Context) { Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel")) index, err := Storage.HLSMuxerM3U8(c.Param("uuid"), c.Param("channel"), stringToInt(c.DefaultQuery("_HLS_msn", "-1")), stringToInt(c.DefaultQuery("_HLS_part", "-1"))) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM3U8", - "call": "HLSMuxerM3U8", + requestLogger.WithFields(logrus.Fields{ + "call": "HLSMuxerM3U8", }).Errorln(ErrorStreamNotFound.Error()) return } _, err = c.Writer.Write([]byte(index)) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM3U8", - "call": "Write", + requestLogger.WithFields(logrus.Fields{ + "call": "Write", }).Errorln(ErrorStreamNotFound.Error()) return } @@ -105,72 +90,55 @@ func HTTPAPIServerStreamHLSLLM3U8(c *gin.Context) { //HTTPAPIServerStreamHLSLLM4Segment send client ts segment func HTTPAPIServerStreamHLSLLM4Segment(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_hlsll", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamHLSLLM4Segment", + }) + c.Header("Content-Type", "video/mp4") if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Segment", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Segment", - "call": "StreamChannelCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelCodecs", }).Errorln(err.Error()) return } if codecs == nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Segment", - "call": "StreamCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamCodecs", }).Errorln("Codec Null") return } Muxer := mp4f.NewMuxer(nil) err = Muxer.WriteHeader(codecs) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Segment", - "call": "WriteHeader", + requestLogger.WithFields(logrus.Fields{ + "call": "WriteHeader", }).Errorln(err.Error()) return } seqData, err := Storage.HLSMuxerSegment(c.Param("uuid"), c.Param("channel"), stringToInt(c.Param("segment"))) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Segment", - "call": "HLSMuxerSegment", + requestLogger.WithFields(logrus.Fields{ + "call": "HLSMuxerSegment", }).Errorln(err.Error()) return } for _, v := range seqData { err = Muxer.WritePacket4(*v) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Segment", - "call": "WritePacket4", + requestLogger.WithFields(logrus.Fields{ + "call": "WritePacket4", }).Errorln(err.Error()) return } @@ -178,12 +146,8 @@ func HTTPAPIServerStreamHLSLLM4Segment(c *gin.Context) { buf := Muxer.Finalize() _, err = c.Writer.Write(buf) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Segment", - "call": "Write", + requestLogger.WithFields(logrus.Fields{ + "call": "Write", }).Errorln(err.Error()) return } @@ -191,72 +155,55 @@ func HTTPAPIServerStreamHLSLLM4Segment(c *gin.Context) { //HTTPAPIServerStreamHLSLLM4Fragment send client ts segment func HTTPAPIServerStreamHLSLLM4Fragment(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_hlsll", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamHLSLLM4Fragment", + }) + c.Header("Content-Type", "video/mp4") if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Fragment", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Fragment", - "call": "StreamChannelCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelCodecs", }).Errorln(err.Error()) return } if codecs == nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Fragment", - "call": "StreamCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamCodecs", }).Errorln("Codec Null") return } Muxer := mp4f.NewMuxer(nil) err = Muxer.WriteHeader(codecs) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Fragment", - "call": "WriteHeader", + requestLogger.WithFields(logrus.Fields{ + "call": "WriteHeader", }).Errorln(err.Error()) return } seqData, err := Storage.HLSMuxerFragment(c.Param("uuid"), c.Param("channel"), stringToInt(c.Param("segment")), stringToInt(c.Param("fragment"))) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Fragment", - "call": "HLSMuxerFragment", + requestLogger.WithFields(logrus.Fields{ + "call": "HLSMuxerFragment", }).Errorln(err.Error()) return } for _, v := range seqData { err = Muxer.WritePacket4(*v) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Fragment", - "call": "WritePacket4", + requestLogger.WithFields(logrus.Fields{ + "call": "WritePacket4", }).Errorln(err.Error()) return } @@ -264,12 +211,8 @@ func HTTPAPIServerStreamHLSLLM4Fragment(c *gin.Context) { buf := Muxer.Finalize() _, err = c.Writer.Write(buf) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_hlsll", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamHLSLLM4Fragment", - "call": "Write", + requestLogger.WithFields(logrus.Fields{ + "call": "Write", }).Errorln(err.Error()) return } diff --git a/apiHTTPMSE.go b/apiHTTPMSE.go index 07ba590..04db962 100644 --- a/apiHTTPMSE.go +++ b/apiHTTPMSE.go @@ -10,94 +10,69 @@ import ( //HTTPAPIServerStreamMSE func func HTTPAPIServerStreamMSE(ws *websocket.Conn) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_mse", + "stream": ws.Request().FormValue("uuid"), + "channel": ws.Request().FormValue("channel"), + "func": "HTTPAPIServerStreamMSE", + }) + defer func() { err := ws.Close() - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "Close", + requestLogger.WithFields(logrus.Fields{ + "call": "Close", }).Errorln(err) log.Println("Client Full Exit") }() if !Storage.StreamChannelExist(ws.Request().FormValue("uuid"), ws.Request().FormValue("channel")) { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } Storage.StreamChannelRun(ws.Request().FormValue("uuid"), ws.Request().FormValue("channel")) err := ws.SetWriteDeadline(time.Now().Add(5 * time.Second)) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "SetWriteDeadline", + requestLogger.WithFields(logrus.Fields{ + "call": "SetWriteDeadline", }).Errorln(err.Error()) return } cid, ch, _, err := Storage.ClientAdd(ws.Request().FormValue("uuid"), ws.Request().FormValue("channel"), MSE) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "ClientAdd", + requestLogger.WithFields(logrus.Fields{ + "call": "ClientAdd", }).Errorln(err.Error()) return } defer Storage.ClientDelete(ws.Request().FormValue("uuid"), cid, ws.Request().FormValue("channel")) codecs, err := Storage.StreamChannelCodecs(ws.Request().FormValue("uuid"), ws.Request().FormValue("channel")) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "StreamCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamCodecs", }).Errorln(err.Error()) return } muxerMSE := mp4f.NewMuxer(nil) err = muxerMSE.WriteHeader(codecs) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "WriteHeader", + requestLogger.WithFields(logrus.Fields{ + "call": "WriteHeader", }).Errorln(err.Error()) return } meta, init := muxerMSE.GetInit(codecs) err = websocket.Message.Send(ws, append([]byte{9}, meta...)) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "Send", + requestLogger.WithFields(logrus.Fields{ + "call": "Send", }).Errorln(err.Error()) return } err = websocket.Message.Send(ws, init) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "Send", + requestLogger.WithFields(logrus.Fields{ + "call": "Send", }).Errorln(err.Error()) return } @@ -111,12 +86,8 @@ func HTTPAPIServerStreamMSE(ws *websocket.Conn) { var message string err := websocket.Message.Receive(ws, &message) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "Receive", + requestLogger.WithFields(logrus.Fields{ + "call": "Receive", }).Errorln(err.Error()) return } @@ -126,21 +97,13 @@ func HTTPAPIServerStreamMSE(ws *websocket.Conn) { for { select { case <-controlExit: - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "controlExit", + requestLogger.WithFields(logrus.Fields{ + "call": "controlExit", }).Errorln("Client Reader Exit") return case <-noVideo.C: - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "ErrorStreamNoVideo", + requestLogger.WithFields(logrus.Fields{ + "call": "ErrorStreamNoVideo", }).Errorln(ErrorStreamNoVideo.Error()) return case pck := <-ch: @@ -153,35 +116,23 @@ func HTTPAPIServerStreamMSE(ws *websocket.Conn) { } ready, buf, err := muxerMSE.WritePacket(*pck, false) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "WritePacket", + requestLogger.WithFields(logrus.Fields{ + "call": "WritePacket", }).Errorln(err.Error()) return } if ready { err := ws.SetWriteDeadline(time.Now().Add(10 * time.Second)) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "SetWriteDeadline", + requestLogger.WithFields(logrus.Fields{ + "call": "SetWriteDeadline", }).Errorln(err.Error()) return } err = websocket.Message.Send(ws, buf) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_mse", - "stream": ws.Request().FormValue("uuid"), - "channel": ws.Request().FormValue("channel"), - "func": "HTTPAPIServerStreamMSE", - "call": "Send", + requestLogger.WithFields(logrus.Fields{ + "call": "Send", }).Errorln(err.Error()) return } diff --git a/apiHTTPStream.go b/apiHTTPStream.go index 581b385..f6c90fa 100644 --- a/apiHTTPStream.go +++ b/apiHTTPStream.go @@ -12,23 +12,24 @@ func HTTPAPIServerStreams(c *gin.Context) { //HTTPAPIServerStreamsMultiControlAdd function add new stream's func HTTPAPIServerStreamsMultiControlAdd(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_stream", + "func": "HTTPAPIServerStreamsMultiControlAdd", + }) + var payload StorageST err := c.BindJSON(&payload) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "func": "HTTPAPIServerStreamsMultiControlAdd", - "call": "BindJSON", + requestLogger.WithFields(logrus.Fields{ + "call": "BindJSON", }).Errorln(err.Error()) return } if payload.Streams == nil || len(payload.Streams) < 1 { c.IndentedJSON(400, Message{Status: 0, Payload: ErrorStreamsLen0.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "func": "HTTPAPIServerStreamsMultiControlAdd", - "call": "len(payload)", + requestLogger.WithFields(logrus.Fields{ + "call": "len(payload)", }).Errorln(ErrorStreamsLen0.Error()) return } @@ -37,10 +38,8 @@ func HTTPAPIServerStreamsMultiControlAdd(c *gin.Context) { for k, v := range payload.Streams { err = Storage.StreamAdd(k, v) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_stream", + requestLogger.WithFields(logrus.Fields{ "stream": k, - "func": "HTTPAPIServerStreamsMultiControlAdd", "call": "StreamAdd", }).Errorln(err.Error()) resp[k] = Message{Status: 0, Payload: err.Error()} @@ -58,23 +57,24 @@ func HTTPAPIServerStreamsMultiControlAdd(c *gin.Context) { //HTTPAPIServerStreamsMultiControlDelete function delete stream's func HTTPAPIServerStreamsMultiControlDelete(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_stream", + "func": "HTTPAPIServerStreamsMultiControlDelete", + }) + var payload []string err := c.BindJSON(&payload) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "func": "HTTPAPIServerStreamsMultiControlDelete", - "call": "BindJSON", + requestLogger.WithFields(logrus.Fields{ + "call": "BindJSON", }).Errorln(err.Error()) return } if len(payload) < 1 { c.IndentedJSON(400, Message{Status: 0, Payload: ErrorStreamsLen0.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_stream", - "func": "HTTPAPIServerStreamsMultiControlDelete", - "call": "len(payload)", + requestLogger.WithFields(logrus.Fields{ + "call": "len(payload)", }).Errorln(ErrorStreamsLen0.Error()) return } @@ -83,10 +83,8 @@ func HTTPAPIServerStreamsMultiControlDelete(c *gin.Context) { for _, key := range payload { err := Storage.StreamDelete(key) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_stream", + requestLogger.WithFields(logrus.Fields{ "stream": key, - "func": "HTTPAPIServerStreamsMultiControlDelete", "call": "StreamDelete", }).Errorln(err.Error()) resp[key] = Message{Status: 0, Payload: err.Error()} diff --git a/apiHTTPWebRTC.go b/apiHTTPWebRTC.go index 5dd00b9..a0323d9 100644 --- a/apiHTTPWebRTC.go +++ b/apiHTTPWebRTC.go @@ -10,14 +10,17 @@ import ( //HTTPAPIServerStreamWebRTC stream video over WebRTC func HTTPAPIServerStreamWebRTC(c *gin.Context) { + requestLogger := log.WithFields(logrus.Fields{ + "module": "http_webrtc", + "stream": c.Param("uuid"), + "channel": c.Param("channel"), + "func": "HTTPAPIServerStreamWebRTC", + }) + if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) { c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_webrtc", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamWebRTC", - "call": "StreamChannelExist", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamChannelExist", }).Errorln(ErrorStreamNotFound.Error()) return } @@ -25,12 +28,8 @@ func HTTPAPIServerStreamWebRTC(c *gin.Context) { codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel")) if err != nil { c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_webrtc", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamWebRTC", - "call": "StreamCodecs", + requestLogger.WithFields(logrus.Fields{ + "call": "StreamCodecs", }).Errorln(err.Error()) return } @@ -38,24 +37,16 @@ func HTTPAPIServerStreamWebRTC(c *gin.Context) { answer, err := muxerWebRTC.WriteHeader(codecs, c.PostForm("data")) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_webrtc", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamWebRTC", - "call": "WriteHeader", + requestLogger.WithFields(logrus.Fields{ + "call": "WriteHeader", }).Errorln(err.Error()) return } _, err = c.Writer.Write([]byte(answer)) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_webrtc", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamWebRTC", - "call": "Write", + requestLogger.WithFields(logrus.Fields{ + "call": "Write", }).Errorln(err.Error()) return } @@ -63,12 +54,8 @@ func HTTPAPIServerStreamWebRTC(c *gin.Context) { cid, ch, _, err := Storage.ClientAdd(c.Param("uuid"), c.Param("channel"), WEBRTC) if err != nil { c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_webrtc", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamWebRTC", - "call": "ClientAdd", + requestLogger.WithFields(logrus.Fields{ + "call": "ClientAdd", }).Errorln(err.Error()) return } @@ -79,12 +66,8 @@ func HTTPAPIServerStreamWebRTC(c *gin.Context) { select { case <-noVideo.C: c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNoVideo.Error()}) - log.WithFields(logrus.Fields{ - "module": "http_webrtc", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamWebRTC", - "call": "ErrorStreamNoVideo", + requestLogger.WithFields(logrus.Fields{ + "call": "ErrorStreamNoVideo", }).Errorln(ErrorStreamNoVideo.Error()) return case pck := <-ch: @@ -97,12 +80,8 @@ func HTTPAPIServerStreamWebRTC(c *gin.Context) { } err = muxerWebRTC.WritePacket(*pck) if err != nil { - log.WithFields(logrus.Fields{ - "module": "http_webrtc", - "stream": c.Param("uuid"), - "channel": c.Param("channel"), - "func": "HTTPAPIServerStreamWebRTC", - "call": "WritePacket", + requestLogger.WithFields(logrus.Fields{ + "call": "WritePacket", }).Errorln(err.Error()) return } diff --git a/streamCore.go b/streamCore.go index 9da328b..a49e740 100644 --- a/streamCore.go +++ b/streamCore.go @@ -19,52 +19,37 @@ func StreamServerRunStreamDo(streamID string, channelID string) { } }() for { - log.WithFields(logrus.Fields{ + baseLogger := log.WithFields(logrus.Fields{ "module": "core", "stream": streamID, "channel": channelID, "func": "StreamServerRunStreamDo", - "call": "Run", - }).Infoln("Run stream") + }) + + baseLogger.WithFields(logrus.Fields{"call": "Run"}).Infoln("Run stream") opt, err := Storage.StreamChannelControl(streamID, channelID) if err != nil { - log.WithFields(logrus.Fields{ - "module": "core", - "stream": streamID, - "channel": channelID, - "func": "StreamChannelControl", - "call": "Exit", + baseLogger.WithFields(logrus.Fields{ + "call": "StreamChannelControl", }).Infoln("Exit", err) return } if opt.OnDemand && !Storage.ClientHas(streamID, channelID) { - log.WithFields(logrus.Fields{ - "module": "core", - "stream": streamID, - "channel": channelID, - "func": "StreamServerRunStreamDo", - "call": "ClientHas", + baseLogger.WithFields(logrus.Fields{ + "call": "ClientHas", }).Infoln("Stop stream no client") return } status, err = StreamServerRunStream(streamID, channelID, opt) if status > 0 { - log.WithFields(logrus.Fields{ - "module": "core", - "stream": streamID, - "channel": channelID, - "func": "StreamServerRunStreamDo", - "call": "StreamServerRunStream", + baseLogger.WithFields(logrus.Fields{ + "call": "StreamServerRunStream", }).Infoln("Stream exit by signal or not client") return } if err != nil { log.WithFields(logrus.Fields{ - "module": "core", - "stream": streamID, - "channel": channelID, - "func": "StreamServerRunStreamDo", - "call": "Restart", + "call": "Restart", }).Errorln("Stream error restart stream", err) } time.Sleep(2 * time.Second)