Skip to content

Commit 6b5ecf1

Browse files
committed
Feat: ts file and ts file type
1 parent c509743 commit 6b5ecf1

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

cmd/server.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"fmt"
6+
"image"
7+
"image/color"
8+
"image/png"
59
"log"
10+
"math/rand"
611
"net"
712
"net/http"
813
"path"
@@ -50,45 +55,64 @@ func Server(cmd *cobra.Command, args []string) {
5055
}
5156
e := gin.Default()
5257
utils.Cors(e)
53-
e.GET("/:app/*channel", func(c *gin.Context) {
54-
appName := c.Param("app")
55-
channelStr := strings.Trim(c.Param("channel"), "/")
58+
e.GET("/:app/*channel", func(ctx *gin.Context) {
59+
appName := ctx.Param("app")
60+
channelStr := strings.Trim(ctx.Param("channel"), "/")
5661
channelSplitd := strings.Split(channelStr, "/")
5762
fileName := channelSplitd[0]
5863
fileExt := path.Ext(channelStr)
5964
channelName := strings.TrimSuffix(fileName, fileExt)
6065

6166
channel, ok := channels.Load(appName)
6267
if !ok {
63-
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
68+
ctx.AbortWithStatusJSON(http.StatusNotFound, gin.H{
6469
"error": "app not found",
6570
})
6671
return
6772
}
6873
switch fileExt {
6974
case ".flv":
70-
w := httpflv.NewHttpFLVWriter(c.Writer)
75+
w := httpflv.NewHttpFLVWriter(ctx.Writer)
7176
defer w.Close()
7277
channel.AddPlayer(w)
7378
w.SendPacket()
7479
case ".m3u8":
75-
b, err := channel.GenM3U8PlayList(fmt.Sprintf("/%s/%s", appName, channelName))
80+
b, err := channel.GenM3U8File(func(tsName string) (tsPath string) {
81+
return fmt.Sprintf("/%s/%s/%s.%s", appName, channelName, tsName, ctx.DefaultQuery("t", "ts"))
82+
})
7683
if err != nil {
77-
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
84+
ctx.AbortWithStatusJSON(http.StatusNotFound, gin.H{
7885
"error": err.Error(),
7986
})
8087
return
8188
}
82-
c.Data(http.StatusOK, hls.M3U8ContentType, b.Bytes())
89+
ctx.Data(http.StatusOK, hls.M3U8ContentType, b)
8390
case ".ts":
8491
b, err := channel.GetTsFile(channelSplitd[1])
8592
if err != nil {
86-
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
93+
ctx.AbortWithStatusJSON(http.StatusNotFound, gin.H{
94+
"error": err.Error(),
95+
})
96+
return
97+
}
98+
ctx.Data(http.StatusOK, hls.TSContentType, b)
99+
case ".png":
100+
b, err := channel.GetTsFile(strings.TrimRight(channelSplitd[1], ".png"))
101+
if err != nil {
102+
ctx.AbortWithStatusJSON(http.StatusNotFound, gin.H{
87103
"error": err.Error(),
88104
})
89105
return
90106
}
91-
c.Data(http.StatusOK, hls.TSContentType, b)
107+
img := image.NewGray(image.Rect(0, 0, 1, 1))
108+
img.Set(1, 1, color.Gray{uint8(rand.Intn(255))})
109+
f := bytes.NewBuffer(nil)
110+
err = png.Encode(f, img)
111+
if err != nil {
112+
panic(err)
113+
}
114+
ctx.Data(http.StatusOK, "image/png", f.Bytes())
115+
ctx.Writer.Write(b)
92116
}
93117
})
94118
go http.Serve(httpl, e.Handler())

protocol/hls/cache.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io/fs"
7-
"path"
7+
"path/filepath"
88
"strings"
99
"sync"
1010

@@ -38,7 +38,7 @@ func (tc *TSCache) all() []*TSItem {
3838
return items
3939
}
4040

41-
func (tc *TSCache) GenM3U8PlayList(tsBasePath string) *bytes.Buffer {
41+
func (tc *TSCache) GenM3U8File(tsPath func(tsName string) (tsPath string)) []byte {
4242
var seq int64
4343
var maxDuration int64
4444
m3u8body := bytes.NewBuffer(nil)
@@ -53,14 +53,14 @@ func (tc *TSCache) GenM3U8PlayList(tsBasePath string) *bytes.Buffer {
5353
if seq == 0 {
5454
seq = item.SeqNum
5555
}
56-
fmt.Fprintf(m3u8body, "#EXTINF:%.3f,\n%s.ts\n#EXT-X-BYTERANGE:%d\n", float64(item.Duration)/float64(1000), path.Join(tsBasePath, item.TsName), len(item.Data))
56+
fmt.Fprintf(m3u8body, "#EXTINF:%.3f,\n%s\n#EXT-X-BYTERANGE:%d\n", float64(item.Duration)/float64(1000), tsPath(item.TsName), len(item.Data))
5757
}
5858
w := bytes.NewBuffer(make([]byte, 0, m3u8body.Len()+256))
5959
fmt.Fprintf(w,
6060
"#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:NO\n#EXT-X-TARGETDURATION:%d\n#EXT-X-MEDIA-SEQUENCE:%d\n\n",
6161
maxDuration/1000+1, seq)
6262
m3u8body.WriteTo(w)
63-
return w
63+
return w.Bytes()
6464
}
6565

6666
func (tc *TSCache) PushItem(item *TSItem) {
@@ -75,7 +75,7 @@ func (tc *TSCache) PushItem(item *TSItem) {
7575
}
7676

7777
func (tc *TSCache) GetItem(tsName string) (*TSItem, error) {
78-
tsName = strings.TrimSuffix(tsName, ".ts")
78+
tsName = strings.TrimSuffix(tsName, filepath.Ext(tsName))
7979
tc.lock.RLock()
8080
defer tc.lock.RUnlock()
8181
for e := tc.l.Front(); e != nil; e = e.Next() {

protocol/hls/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (source *Source) cut() {
133133
source.flushAudio()
134134

135135
source.seq++
136-
source.tsCache.PushItem(NewTSItem(fmt.Sprint(time.Now().UnixMilli()), source.stat.durationMs(), source.seq, source.btswriter.Bytes()))
136+
source.tsCache.PushItem(NewTSItem(fmt.Sprint(time.Now().UnixMicro()), source.stat.durationMs(), source.seq, source.btswriter.Bytes()))
137137

138138
source.btswriter.Reset()
139139
source.stat.resetAndNew()

server/channel.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package server
22

33
import (
4-
"bytes"
54
"errors"
65
"sync"
76
"sync/atomic"
@@ -218,11 +217,11 @@ func (c *Channel) InitdHlsPlayer() bool {
218217

219218
var ErrHlsPlayerNotInit = errors.New("hls player not init")
220219

221-
func (c *Channel) GenM3U8PlayList(tsBashPath string) (*bytes.Buffer, error) {
220+
func (c *Channel) GenM3U8File(tsPath func(tsName string) (tsPath string)) ([]byte, error) {
222221
if !c.InitdHlsPlayer() {
223222
return nil, ErrHlsPlayerNotInit
224223
}
225-
return c.HlsPlayer().GetCacheInc().GenM3U8PlayList(tsBashPath), nil
224+
return c.HlsPlayer().GetCacheInc().GenM3U8File(tsPath), nil
226225
}
227226

228227
func (c *Channel) GetTsFile(tsName string) ([]byte, error) {

0 commit comments

Comments
 (0)