代码拉取完成,页面将自动刷新
package main
import (
"bytes"
"time"
"github.com/deepch/vdk/format/ts"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
//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()})
requestLogger.WithFields(logrus.Fields{
"call": "StreamChannelExist",
}).Errorln(ErrorStreamNotFound.Error())
return
}
if !RemoteAuthorization("HLS", c.Param("uuid"), c.Param("channel"), c.Query("token"), c.ClientIP()) {
requestLogger.WithFields(logrus.Fields{
"call": "RemoteAuthorization",
}).Errorln(ErrorStreamUnauthorized.Error())
return
}
c.Header("Content-Type", "application/x-mpegURL")
Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel"))
//If stream mode on_demand need wait ready segment's
for i := 0; i < 40; i++ {
index, seq, err := Storage.StreamHLSm3u8(c.Param("uuid"), c.Param("channel"))
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
requestLogger.WithFields(logrus.Fields{
"call": "StreamHLSm3u8",
}).Errorln(err.Error())
return
}
if seq >= 6 {
_, err := c.Writer.Write([]byte(index))
if err != nil {
c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
requestLogger.WithFields(logrus.Fields{
"call": "Write",
}).Errorln(err.Error())
return
}
return
}
time.Sleep(1 * time.Second)
}
}
//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()})
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()})
requestLogger.WithFields(logrus.Fields{
"call": "StreamCodecs",
}).Errorln(err.Error())
return
}
outfile := bytes.NewBuffer([]byte{})
Muxer := ts.NewMuxer(outfile)
Muxer.PaddingToMakeCounterCont = true
err = Muxer.WriteHeader(codecs)
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
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()})
requestLogger.WithFields(logrus.Fields{
"call": "StreamHLSTS",
}).Errorln(err.Error())
return
}
if len(seqData) == 0 {
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotHLSSegments.Error()})
requestLogger.WithFields(logrus.Fields{
"call": "seqData",
}).Errorln(ErrorStreamNotHLSSegments.Error())
return
}
for _, v := range seqData {
v.CompositionTime = 1
err = Muxer.WritePacket(*v)
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
requestLogger.WithFields(logrus.Fields{
"call": "WritePacket",
}).Errorln(err.Error())
return
}
}
err = Muxer.WriteTrailer()
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
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()})
requestLogger.WithFields(logrus.Fields{
"call": "Write",
}).Errorln(err.Error())
return
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。