Skip to content

Commit

Permalink
test hls ll
Browse files Browse the repository at this point in the history
  • Loading branch information
deepch committed May 21, 2021
1 parent 816fac1 commit 92cea27
Show file tree
Hide file tree
Showing 15 changed files with 824 additions and 76 deletions.
276 changes: 276 additions & 0 deletions apiHTTPHLSLL.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
package main

import (
"github.com/deepch/vdk/format/mp4f"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

//HTTPAPIServerStreamHLSLLInit send client ts segment
func HTTPAPIServerStreamHLSLLInit(c *gin.Context) {
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",
}).Errorln(ErrorStreamNotFound.Error())
return
}
c.Header("Content-Type", "application/x-mpegURL")
Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel"))
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",
}).Errorln(err.Error())
return
}
Muxer := mp4f.NewMuxer(nil)
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",
}).Errorln(err.Error())
return
}
c.Header("Content-Type", "video/mp4")
_, buf := Muxer.GetInit(codecs)
_, 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",
}).Errorln(err.Error())
return
}

}

//HTTPAPIServerStreamHLSLLM3U8 send client m3u8 play list
func HTTPAPIServerStreamHLSLLM3U8(c *gin.Context) {
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",
}).Errorln(ErrorStreamNotFound.Error())
return
}
c.Header("Content-Type", "application/x-mpegURL")
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",
}).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",
}).Errorln(ErrorStreamNotFound.Error())
return
}
}

//HTTPAPIServerStreamHLSLLM4Segment send client ts segment
func HTTPAPIServerStreamHLSLLM4Segment(c *gin.Context) {
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",
}).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",
}).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",
}).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",
}).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",
}).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",
}).Errorln(err.Error())
return
}
}
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",
}).Errorln(err.Error())
return
}
}

//HTTPAPIServerStreamHLSLLM4Fragment send client ts segment
func HTTPAPIServerStreamHLSLLM4Fragment(c *gin.Context) {
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",
}).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",
}).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",
}).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",
}).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",
}).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",
}).Errorln(err.Error())
return
}
}
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",
}).Errorln(err.Error())
return
}
}
40 changes: 28 additions & 12 deletions apiHTTPRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"time"

"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"golang.org/x/net/websocket"
Expand Down Expand Up @@ -87,9 +88,15 @@ func HTTPAPIServer() {
/*
Stream video elements
*/

//HLS
public.GET("/stream/:uuid/channel/:channel/hls/live/index.m3u8", HTTPAPIServerStreamHLSM3U8)
public.GET("/stream/:uuid/channel/:channel/hls/live/segment/:seq/file.ts", HTTPAPIServerStreamHLSTS)
//HLS LL
public.GET("/stream/:uuid/channel/:channel/hlsll/live/index.m3u8", HTTPAPIServerStreamHLSLLM3U8)
public.GET("/stream/:uuid/channel/:channel/hlsll/live/init.mp4", HTTPAPIServerStreamHLSLLInit)
public.GET("/stream/:uuid/channel/:channel/hlsll/live/segment/:segment/:any", HTTPAPIServerStreamHLSLLM4Segment)
public.GET("/stream/:uuid/channel/:channel/hlsll/live/fragment/:segment/:fragment/:any", HTTPAPIServerStreamHLSLLM4Fragment)
//MSE
public.GET("/stream/:uuid/channel/:channel/mse", func(c *gin.Context) {
handler := websocket.Handler(HTTPAPIServerStreamMSE)
handler.ServeHTTP(c.Writer, c.Request)
Expand All @@ -114,17 +121,26 @@ func HTTPAPIServer() {
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
*/
if Storage.ServerHTTPS() {
go func() {
err := public.RunTLS(Storage.ServerHTTPSPort(), Storage.ServerHTTPSCert(), Storage.ServerHTTPSKey())
if err != nil {
log.WithFields(logrus.Fields{
"module": "http_router",
"func": "HTTPSAPIServer",
"call": "ServerHTTPSPort",
}).Fatalln(err.Error())
os.Exit(1)
}
}()
if Storage.ServerHTTPSAutoTLSEnable() {
go func() {
err := autotls.Run(public, Storage.ServerHTTPSAutoTLSName()+Storage.ServerHTTPSPort())
if err != nil {
log.Println("Start HTTPS Server Error", err)
}
}()
} else {
go func() {
err := public.RunTLS(Storage.ServerHTTPSPort(), Storage.ServerHTTPSCert(), Storage.ServerHTTPSKey())
if err != nil {
log.WithFields(logrus.Fields{
"module": "http_router",
"func": "HTTPSAPIServer",
"call": "ServerHTTPSPort",
}).Fatalln(err.Error())
os.Exit(1)
}
}()
}
}
err := public.Run(Storage.ServerHTTPPort())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/deepch/vdk v0.0.0-20210508200759-5adbbcc01f89
github.com/gin-gonic/autotls v0.0.3 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
Expand All @@ -14,7 +15,6 @@ require (
github.com/liip/sheriff v0.9.0
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pion/ice/v2 v2.0.15 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/ugorji/go v1.2.3 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
Expand Down
Loading

0 comments on commit 92cea27

Please sign in to comment.