From 96c3d9eba1ce8961462f96748b1782901059ed1f Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Tue, 29 Jun 2021 19:51:26 +0200 Subject: [PATCH] HLS: skip frames received before the first IDR frame --- internal/hlsconverter/converter.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/hlsconverter/converter.go b/internal/hlsconverter/converter.go index a7995a75..4cd4fb00 100644 --- a/internal/hlsconverter/converter.go +++ b/internal/hlsconverter/converter.go @@ -405,7 +405,7 @@ func (c *Converter) runInner(innerCtx context.Context) error { // send them together. marker := (pair.buf[1] >> 7 & 0x1) > 0 if marker { - isIDR := func() bool { + bufferHasIDR := func() bool { for _, nalu := range videoBuf { typ := h264.NALUType(nalu[0] & 0x1F) if typ == h264.NALUTypeIDR { @@ -415,11 +415,20 @@ func (c *Converter) runInner(innerCtx context.Context) error { return false }() + // we received a marker packet but + // - no IDR has been stored yet in current file + // - there's no IDR in the buffer + // data cannot be parsed, clear buffer + if !bufferHasIDR && !curTSFile.firstPacketWritten { + videoBuf = nil + continue + } + err := func() error { c.tsMutex.Lock() defer c.tsMutex.Unlock() - if isIDR { + if bufferHasIDR { if curTSFile.firstPacketWritten && curTSFile.Duration() >= c.hlsSegmentDuration { if curTSFile != nil { @@ -436,17 +445,13 @@ func (c *Converter) runInner(innerCtx context.Context) error { c.tsDeleteCount++ } } - } else { - if !curTSFile.firstPacketWritten { - return nil - } } curTSFile.SetPCR(time.Since(startPCR)) err := curTSFile.WriteH264( videoDTSEst.Feed(pts+ptsOffset), pts+ptsOffset, - isIDR, + bufferHasIDR, videoBuf) if err != nil { return err