hls: speed up loading by decreasing PTS

This commit is contained in:
aler9 2021-08-14 17:08:35 +02:00
parent 30910d52d2
commit 2d71e69e43

View File

@ -42,6 +42,7 @@ type Muxer struct {
tsByName map[string]*tsFile tsByName map[string]*tsFile
tsDeleteCount int tsDeleteCount int
mutex sync.RWMutex mutex sync.RWMutex
startPTS time.Duration
} }
// NewMuxer allocates a Muxer. // NewMuxer allocates a Muxer.
@ -134,14 +135,17 @@ func (m *Muxer) WriteH264(pts time.Duration, nalus [][]byte) error {
m.tsQueue = m.tsQueue[1:] m.tsQueue = m.tsQueue[1:]
m.tsDeleteCount++ m.tsDeleteCount++
} }
} else if !m.tsCurrent.firstPacketWritten {
m.startPTS = pts
} }
pts = pts + ptsOffset - m.startPTS
m.tsCurrent.setPCR(time.Since(m.startPCR)) m.tsCurrent.setPCR(time.Since(m.startPCR))
err := m.tsCurrent.writeH264( err := m.tsCurrent.writeH264(
m.h264SPS, m.h264SPS,
m.h264PPS, m.h264PPS,
m.videoDTSEst.Feed(pts+ptsOffset), m.videoDTSEst.Feed(pts),
pts+ptsOffset, pts,
idrPresent, idrPresent,
nalus) nalus)
if err != nil { if err != nil {
@ -174,6 +178,8 @@ func (m *Muxer) WriteAAC(pts time.Duration, aus [][]byte) error {
m.tsQueue = m.tsQueue[1:] m.tsQueue = m.tsQueue[1:]
m.tsDeleteCount++ m.tsDeleteCount++
} }
} else if !m.tsCurrent.firstPacketWritten {
m.startPTS = pts
} }
} else { } else {
if !m.tsCurrent.firstPacketWritten { 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 { for i, au := range aus {
auPTS := pts + time.Duration(i)*1000*time.Second/time.Duration(m.aacConfig.SampleRate) 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( err := m.tsCurrent.writeAAC(
m.aacConfig.SampleRate, m.aacConfig.SampleRate,
m.aacConfig.ChannelCount, m.aacConfig.ChannelCount,
auPTS+ptsOffset, auPTS,
au) au)
if err != nil { if err != nil {
return err return err