Skip to content

Commit 92cea27

Browse files
committed
test hls ll
1 parent 816fac1 commit 92cea27

15 files changed

+824
-76
lines changed

apiHTTPHLSLL.go

+276
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package main
2+
3+
import (
4+
"github.com/deepch/vdk/format/mp4f"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
//HTTPAPIServerStreamHLSLLInit send client ts segment
11+
func HTTPAPIServerStreamHLSLLInit(c *gin.Context) {
12+
if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) {
13+
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
14+
log.WithFields(logrus.Fields{
15+
"module": "http_hlsll",
16+
"stream": c.Param("uuid"),
17+
"channel": c.Param("channel"),
18+
"func": "HTTPAPIServerStreamHLSLLInit",
19+
"call": "StreamChannelExist",
20+
}).Errorln(ErrorStreamNotFound.Error())
21+
return
22+
}
23+
c.Header("Content-Type", "application/x-mpegURL")
24+
Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel"))
25+
codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel"))
26+
if err != nil {
27+
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
28+
log.WithFields(logrus.Fields{
29+
"module": "http_hlsll",
30+
"stream": c.Param("uuid"),
31+
"channel": c.Param("channel"),
32+
"func": "HTTPAPIServerStreamHLSLLInit",
33+
"call": "StreamChannelCodecs",
34+
}).Errorln(err.Error())
35+
return
36+
}
37+
Muxer := mp4f.NewMuxer(nil)
38+
err = Muxer.WriteHeader(codecs)
39+
if err != nil {
40+
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
41+
log.WithFields(logrus.Fields{
42+
"module": "http_hlsll",
43+
"stream": c.Param("uuid"),
44+
"channel": c.Param("channel"),
45+
"func": "HTTPAPIServerStreamHLSLLInit",
46+
"call": "WriteHeader",
47+
}).Errorln(err.Error())
48+
return
49+
}
50+
c.Header("Content-Type", "video/mp4")
51+
_, buf := Muxer.GetInit(codecs)
52+
_, err = c.Writer.Write(buf)
53+
if err != nil {
54+
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
55+
log.WithFields(logrus.Fields{
56+
"module": "http_hlsll",
57+
"stream": c.Param("uuid"),
58+
"channel": c.Param("channel"),
59+
"func": "HTTPAPIServerStreamHLSLLInit",
60+
"call": "Write",
61+
}).Errorln(err.Error())
62+
return
63+
}
64+
65+
}
66+
67+
//HTTPAPIServerStreamHLSLLM3U8 send client m3u8 play list
68+
func HTTPAPIServerStreamHLSLLM3U8(c *gin.Context) {
69+
if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) {
70+
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
71+
log.WithFields(logrus.Fields{
72+
"module": "http_hlsll",
73+
"stream": c.Param("uuid"),
74+
"channel": c.Param("channel"),
75+
"func": "HTTPAPIServerStreamHLSLLM3U8",
76+
"call": "StreamChannelExist",
77+
}).Errorln(ErrorStreamNotFound.Error())
78+
return
79+
}
80+
c.Header("Content-Type", "application/x-mpegURL")
81+
Storage.StreamChannelRun(c.Param("uuid"), c.Param("channel"))
82+
index, err := Storage.HLSMuxerM3U8(c.Param("uuid"), c.Param("channel"), stringToInt(c.DefaultQuery("_HLS_msn", "-1")), stringToInt(c.DefaultQuery("_HLS_part", "-1")))
83+
if err != nil {
84+
log.WithFields(logrus.Fields{
85+
"module": "http_hlsll",
86+
"stream": c.Param("uuid"),
87+
"channel": c.Param("channel"),
88+
"func": "HTTPAPIServerStreamHLSLLM3U8",
89+
"call": "HLSMuxerM3U8",
90+
}).Errorln(ErrorStreamNotFound.Error())
91+
return
92+
}
93+
_, err = c.Writer.Write([]byte(index))
94+
if err != nil {
95+
log.WithFields(logrus.Fields{
96+
"module": "http_hlsll",
97+
"stream": c.Param("uuid"),
98+
"channel": c.Param("channel"),
99+
"func": "HTTPAPIServerStreamHLSLLM3U8",
100+
"call": "Write",
101+
}).Errorln(ErrorStreamNotFound.Error())
102+
return
103+
}
104+
}
105+
106+
//HTTPAPIServerStreamHLSLLM4Segment send client ts segment
107+
func HTTPAPIServerStreamHLSLLM4Segment(c *gin.Context) {
108+
c.Header("Content-Type", "video/mp4")
109+
if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) {
110+
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
111+
log.WithFields(logrus.Fields{
112+
"module": "http_hlsll",
113+
"stream": c.Param("uuid"),
114+
"channel": c.Param("channel"),
115+
"func": "HTTPAPIServerStreamHLSLLM4Segment",
116+
"call": "StreamChannelExist",
117+
}).Errorln(ErrorStreamNotFound.Error())
118+
return
119+
}
120+
codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel"))
121+
if err != nil {
122+
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
123+
log.WithFields(logrus.Fields{
124+
"module": "http_hlsll",
125+
"stream": c.Param("uuid"),
126+
"channel": c.Param("channel"),
127+
"func": "HTTPAPIServerStreamHLSLLM4Segment",
128+
"call": "StreamChannelCodecs",
129+
}).Errorln(err.Error())
130+
return
131+
}
132+
if codecs == nil {
133+
log.WithFields(logrus.Fields{
134+
"module": "http_hlsll",
135+
"stream": c.Param("uuid"),
136+
"channel": c.Param("channel"),
137+
"func": "HTTPAPIServerStreamHLSLLM4Segment",
138+
"call": "StreamCodecs",
139+
}).Errorln("Codec Null")
140+
return
141+
}
142+
Muxer := mp4f.NewMuxer(nil)
143+
err = Muxer.WriteHeader(codecs)
144+
if err != nil {
145+
log.WithFields(logrus.Fields{
146+
"module": "http_hlsll",
147+
"stream": c.Param("uuid"),
148+
"channel": c.Param("channel"),
149+
"func": "HTTPAPIServerStreamHLSLLM4Segment",
150+
"call": "WriteHeader",
151+
}).Errorln(err.Error())
152+
return
153+
}
154+
seqData, err := Storage.HLSMuxerSegment(c.Param("uuid"), c.Param("channel"), stringToInt(c.Param("segment")))
155+
if err != nil {
156+
log.WithFields(logrus.Fields{
157+
"module": "http_hlsll",
158+
"stream": c.Param("uuid"),
159+
"channel": c.Param("channel"),
160+
"func": "HTTPAPIServerStreamHLSLLM4Segment",
161+
"call": "HLSMuxerSegment",
162+
}).Errorln(err.Error())
163+
return
164+
}
165+
for _, v := range seqData {
166+
err = Muxer.WritePacket4(*v)
167+
if err != nil {
168+
log.WithFields(logrus.Fields{
169+
"module": "http_hlsll",
170+
"stream": c.Param("uuid"),
171+
"channel": c.Param("channel"),
172+
"func": "HTTPAPIServerStreamHLSLLM4Segment",
173+
"call": "WritePacket4",
174+
}).Errorln(err.Error())
175+
return
176+
}
177+
}
178+
buf := Muxer.Finalize()
179+
_, err = c.Writer.Write(buf)
180+
if err != nil {
181+
log.WithFields(logrus.Fields{
182+
"module": "http_hlsll",
183+
"stream": c.Param("uuid"),
184+
"channel": c.Param("channel"),
185+
"func": "HTTPAPIServerStreamHLSLLM4Segment",
186+
"call": "Write",
187+
}).Errorln(err.Error())
188+
return
189+
}
190+
}
191+
192+
//HTTPAPIServerStreamHLSLLM4Fragment send client ts segment
193+
func HTTPAPIServerStreamHLSLLM4Fragment(c *gin.Context) {
194+
c.Header("Content-Type", "video/mp4")
195+
if !Storage.StreamChannelExist(c.Param("uuid"), c.Param("channel")) {
196+
c.IndentedJSON(500, Message{Status: 0, Payload: ErrorStreamNotFound.Error()})
197+
log.WithFields(logrus.Fields{
198+
"module": "http_hlsll",
199+
"stream": c.Param("uuid"),
200+
"channel": c.Param("channel"),
201+
"func": "HTTPAPIServerStreamHLSLLM4Fragment",
202+
"call": "StreamChannelExist",
203+
}).Errorln(ErrorStreamNotFound.Error())
204+
return
205+
}
206+
codecs, err := Storage.StreamChannelCodecs(c.Param("uuid"), c.Param("channel"))
207+
if err != nil {
208+
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
209+
log.WithFields(logrus.Fields{
210+
"module": "http_hlsll",
211+
"stream": c.Param("uuid"),
212+
"channel": c.Param("channel"),
213+
"func": "HTTPAPIServerStreamHLSLLM4Fragment",
214+
"call": "StreamChannelCodecs",
215+
}).Errorln(err.Error())
216+
return
217+
}
218+
if codecs == nil {
219+
log.WithFields(logrus.Fields{
220+
"module": "http_hlsll",
221+
"stream": c.Param("uuid"),
222+
"channel": c.Param("channel"),
223+
"func": "HTTPAPIServerStreamHLSLLM4Fragment",
224+
"call": "StreamCodecs",
225+
}).Errorln("Codec Null")
226+
return
227+
}
228+
Muxer := mp4f.NewMuxer(nil)
229+
err = Muxer.WriteHeader(codecs)
230+
if err != nil {
231+
log.WithFields(logrus.Fields{
232+
"module": "http_hlsll",
233+
"stream": c.Param("uuid"),
234+
"channel": c.Param("channel"),
235+
"func": "HTTPAPIServerStreamHLSLLM4Fragment",
236+
"call": "WriteHeader",
237+
}).Errorln(err.Error())
238+
return
239+
}
240+
seqData, err := Storage.HLSMuxerFragment(c.Param("uuid"), c.Param("channel"), stringToInt(c.Param("segment")), stringToInt(c.Param("fragment")))
241+
if err != nil {
242+
log.WithFields(logrus.Fields{
243+
"module": "http_hlsll",
244+
"stream": c.Param("uuid"),
245+
"channel": c.Param("channel"),
246+
"func": "HTTPAPIServerStreamHLSLLM4Fragment",
247+
"call": "HLSMuxerFragment",
248+
}).Errorln(err.Error())
249+
return
250+
}
251+
for _, v := range seqData {
252+
err = Muxer.WritePacket4(*v)
253+
if err != nil {
254+
log.WithFields(logrus.Fields{
255+
"module": "http_hlsll",
256+
"stream": c.Param("uuid"),
257+
"channel": c.Param("channel"),
258+
"func": "HTTPAPIServerStreamHLSLLM4Fragment",
259+
"call": "WritePacket4",
260+
}).Errorln(err.Error())
261+
return
262+
}
263+
}
264+
buf := Muxer.Finalize()
265+
_, err = c.Writer.Write(buf)
266+
if err != nil {
267+
log.WithFields(logrus.Fields{
268+
"module": "http_hlsll",
269+
"stream": c.Param("uuid"),
270+
"channel": c.Param("channel"),
271+
"func": "HTTPAPIServerStreamHLSLLM4Fragment",
272+
"call": "Write",
273+
}).Errorln(err.Error())
274+
return
275+
}
276+
}

apiHTTPRouter.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"time"
77

8+
"github.com/gin-gonic/autotls"
89
"github.com/gin-gonic/gin"
910
"github.com/sirupsen/logrus"
1011
"golang.org/x/net/websocket"
@@ -87,9 +88,15 @@ func HTTPAPIServer() {
8788
/*
8889
Stream video elements
8990
*/
90-
91+
//HLS
9192
public.GET("/stream/:uuid/channel/:channel/hls/live/index.m3u8", HTTPAPIServerStreamHLSM3U8)
9293
public.GET("/stream/:uuid/channel/:channel/hls/live/segment/:seq/file.ts", HTTPAPIServerStreamHLSTS)
94+
//HLS LL
95+
public.GET("/stream/:uuid/channel/:channel/hlsll/live/index.m3u8", HTTPAPIServerStreamHLSLLM3U8)
96+
public.GET("/stream/:uuid/channel/:channel/hlsll/live/init.mp4", HTTPAPIServerStreamHLSLLInit)
97+
public.GET("/stream/:uuid/channel/:channel/hlsll/live/segment/:segment/:any", HTTPAPIServerStreamHLSLLM4Segment)
98+
public.GET("/stream/:uuid/channel/:channel/hlsll/live/fragment/:segment/:fragment/:any", HTTPAPIServerStreamHLSLLM4Fragment)
99+
//MSE
93100
public.GET("/stream/:uuid/channel/:channel/mse", func(c *gin.Context) {
94101
handler := websocket.Handler(HTTPAPIServerStreamMSE)
95102
handler.ServeHTTP(c.Writer, c.Request)
@@ -114,17 +121,26 @@ func HTTPAPIServer() {
114121
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
115122
*/
116123
if Storage.ServerHTTPS() {
117-
go func() {
118-
err := public.RunTLS(Storage.ServerHTTPSPort(), Storage.ServerHTTPSCert(), Storage.ServerHTTPSKey())
119-
if err != nil {
120-
log.WithFields(logrus.Fields{
121-
"module": "http_router",
122-
"func": "HTTPSAPIServer",
123-
"call": "ServerHTTPSPort",
124-
}).Fatalln(err.Error())
125-
os.Exit(1)
126-
}
127-
}()
124+
if Storage.ServerHTTPSAutoTLSEnable() {
125+
go func() {
126+
err := autotls.Run(public, Storage.ServerHTTPSAutoTLSName()+Storage.ServerHTTPSPort())
127+
if err != nil {
128+
log.Println("Start HTTPS Server Error", err)
129+
}
130+
}()
131+
} else {
132+
go func() {
133+
err := public.RunTLS(Storage.ServerHTTPSPort(), Storage.ServerHTTPSCert(), Storage.ServerHTTPSKey())
134+
if err != nil {
135+
log.WithFields(logrus.Fields{
136+
"module": "http_router",
137+
"func": "HTTPSAPIServer",
138+
"call": "ServerHTTPSPort",
139+
}).Fatalln(err.Error())
140+
os.Exit(1)
141+
}
142+
}()
143+
}
128144
}
129145
err := public.Run(Storage.ServerHTTPPort())
130146
if err != nil {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.15
44

55
require (
66
github.com/deepch/vdk v0.0.0-20210508200759-5adbbcc01f89
7+
github.com/gin-gonic/autotls v0.0.3 // indirect
78
github.com/gin-gonic/gin v1.6.3
89
github.com/go-playground/validator/v10 v10.4.1 // indirect
910
github.com/golang/protobuf v1.4.3 // indirect
@@ -14,7 +15,6 @@ require (
1415
github.com/liip/sheriff v0.9.0
1516
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1617
github.com/modern-go/reflect2 v1.0.1 // indirect
17-
github.com/pion/ice/v2 v2.0.15 // indirect
1818
github.com/sirupsen/logrus v1.7.0
1919
github.com/ugorji/go v1.2.3 // indirect
2020
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect

0 commit comments

Comments
 (0)