Skip to content

Commit

Permalink
hls source: fix crash in case of invalid EXT-X-MEDIA-SEQUENCE
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Dec 27, 2022
1 parent 241c81d commit 3154176
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/hls/client_downloader_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func findSegmentWithInvPosition(segments []*gm3u8.MediaSegment, pos int) *gm3u8.

func findSegmentWithID(seqNo uint64, segments []*gm3u8.MediaSegment, id uint64) (*gm3u8.MediaSegment, int) {
index := int(int64(id) - int64(seqNo))
if index >= len(segments) {
if index < 0 || index >= len(segments) {
return nil, 0
}

Expand Down
73 changes: 73 additions & 0 deletions internal/hls/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,76 @@ func TestClient(t *testing.T) {
})
}
}

func TestClientInvalidSequenceID(t *testing.T) {
router := gin.New()
firstPlaylist := true

router.GET("/stream.m3u8", func(ctx *gin.Context) {
ctx.Writer.Header().Set("Content-Type", `application/x-mpegURL`)

if firstPlaylist {
firstPlaylist = false
io.Copy(ctx.Writer, bytes.NewReader([]byte(
`#EXTM3U
#EXT-X-VERSION:3
#EXT-X-ALLOW-CACHE:NO
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:2
#EXTINF:2,
segment1.ts
#EXTINF:2,
segment1.ts
#EXTINF:2,
segment1.ts
`)))
} else {
io.Copy(ctx.Writer, bytes.NewReader([]byte(
`#EXTM3U
#EXT-X-VERSION:3
#EXT-X-ALLOW-CACHE:NO
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:4
#EXTINF:2,
segment1.ts
#EXTINF:2,
segment1.ts
#EXTINF:2,
segment1.ts
`)))
}
})

router.GET("/segment1.ts", func(ctx *gin.Context) {
ctx.Writer.Header().Set("Content-Type", `video/MP2T`)
mpegtsSegment(ctx.Writer)
})

s, err := newTestHLSServer(router, false)
require.NoError(t, err)
defer s.close()

packetRecv := make(chan struct{})

c, err := NewClient(
"http://localhost:5780/stream.m3u8",
"",
func(*format.H264, *format.MPEG4Audio) error {
return nil
},
func(pts time.Duration, nalus [][]byte) {
close(packetRecv)
},
nil,
testLogger{},
)
require.NoError(t, err)

<-packetRecv

// c.Close()
err = <-c.Wait()
require.EqualError(t, err, "following segment not found or not ready yet")

c.Close()
}

0 comments on commit 3154176

Please sign in to comment.