From 2d71e69e435bc7ea6ab8c9ceccbeb27a0fd8d24d Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sat, 14 Aug 2021 17:08:35 +0200 Subject: [PATCH] hls: speed up loading by decreasing PTS --- internal/hls/muxer.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/hls/muxer.go b/internal/hls/muxer.go index 53a6ebe6..f0c71def 100644 --- a/internal/hls/muxer.go +++ b/internal/hls/muxer.go @@ -42,6 +42,7 @@ type Muxer struct { tsByName map[string]*tsFile tsDeleteCount int mutex sync.RWMutex + startPTS time.Duration } // NewMuxer allocates a Muxer. @@ -134,14 +135,17 @@ func (m *Muxer) WriteH264(pts time.Duration, nalus [][]byte) error { m.tsQueue = m.tsQueue[1:] m.tsDeleteCount++ } + } else if !m.tsCurrent.firstPacketWritten { + m.startPTS = pts } + pts = pts + ptsOffset - m.startPTS m.tsCurrent.setPCR(time.Since(m.startPCR)) err := m.tsCurrent.writeH264( m.h264SPS, m.h264PPS, - m.videoDTSEst.Feed(pts+ptsOffset), - pts+ptsOffset, + m.videoDTSEst.Feed(pts), + pts, idrPresent, nalus) if err != nil { @@ -174,6 +178,8 @@ func (m *Muxer) WriteAAC(pts time.Duration, aus [][]byte) error { m.tsQueue = m.tsQueue[1:] m.tsDeleteCount++ } + } else if !m.tsCurrent.firstPacketWritten { + m.startPTS = pts } } else { if !m.tsCurrent.firstPacketWritten { @@ -181,6 +187,8 @@ func (m *Muxer) WriteAAC(pts time.Duration, aus [][]byte) error { } } + pts = pts + ptsOffset - m.startPTS + for i, au := range aus { auPTS := pts + time.Duration(i)*1000*time.Second/time.Duration(m.aacConfig.SampleRate) @@ -189,7 +197,7 @@ func (m *Muxer) WriteAAC(pts time.Duration, aus [][]byte) error { err := m.tsCurrent.writeAAC( m.aacConfig.SampleRate, m.aacConfig.ChannelCount, - auPTS+ptsOffset, + auPTS, au) if err != nil { return err