Skip to content

Commit

Permalink
Fix: play in ios
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Jan 21, 2024
1 parent 58de2ae commit 1e1e5f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions protocol/hls/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (tc *TSCache) all() []*TSItem {
return items
}

func (tc *TSCache) GenM3U8File(tsPath func(tsName string) (tsPath string)) []byte {
func (tc *TSCache) GenM3U8File(tsPath func(tsName string) (tsPath string)) ([]byte, error) {
var seq int64
var maxDuration int64
m3u8body := bytes.NewBuffer(nil)
Expand All @@ -53,14 +53,20 @@ func (tc *TSCache) GenM3U8File(tsPath func(tsName string) (tsPath string)) []byt
if seq == 0 {
seq = item.SeqNum
}
fmt.Fprintf(m3u8body, "#EXTINF:%.3f,\n%s\n#EXT-X-BYTERANGE:%d\n", float64(item.Duration)/float64(1000), tsPath(item.TsName), len(item.Data))
_, err := fmt.Fprintf(m3u8body, "#EXTINF:%.3f,\n%s\n", float64(item.Duration)/float64(1000), tsPath(item.TsName))
if err != nil {
return nil, err
}
}
w := bytes.NewBuffer(make([]byte, 0, m3u8body.Len()+256))
fmt.Fprintf(w,
"#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",
"#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:NO\n#EXT-X-TARGETDURATION:%d\n#EXT-X-MEDIA-SEQUENCE:%d\n",
maxDuration/1000+1, seq)
m3u8body.WriteTo(w)
return w.Bytes()
_, err := m3u8body.WriteTo(w)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}

func (tc *TSCache) PushItem(item *TSItem) {
Expand Down
2 changes: 1 addition & 1 deletion server/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (c *Channel) GenM3U8File(tsPath func(tsName string) (tsPath string)) ([]byt
if !c.InitdHlsPlayer() {
return nil, ErrHlsPlayerNotInit
}
return c.HlsPlayer().GetCacheInc().GenM3U8File(tsPath), nil
return c.HlsPlayer().GetCacheInc().GenM3U8File(tsPath)
}

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

0 comments on commit 1e1e5f7

Please sign in to comment.