From 69e617bc73c5582c81776b00c65abb4614acbccf Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 14 Aug 2022 14:55:24 +0200 Subject: [PATCH] hls: fix multiple timestamp issues introduced by previous commits --- internal/core/hls_muxer.go | 8 ++++++-- internal/core/streamtrack_h264.go | 25 +++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/internal/core/hls_muxer.go b/internal/core/hls_muxer.go index a40ab945..865f5c01 100644 --- a/internal/core/hls_muxer.go +++ b/internal/core/hls_muxer.go @@ -12,6 +12,7 @@ import ( "time" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/mpeg4audio" "github.com/aler9/gortsplib/pkg/ringbuffer" "github.com/aler9/gortsplib/pkg/rtpmpeg4audio" "github.com/gin-gonic/gin" @@ -384,8 +385,11 @@ func (m *hlsMuxer) runInner(innerCtx context.Context, innerReady chan struct{}) continue } - for _, au := range aus { - err = m.muxer.WriteAAC(pts, au) + for i, au := range aus { + err = m.muxer.WriteAAC( + pts+time.Duration(i)*mpeg4audio.SamplesPerAccessUnit* + time.Second/time.Duration(audioTrack.ClockRate()), + au) if err != nil { return fmt.Errorf("muxer error: %v", err) } diff --git a/internal/core/streamtrack_h264.go b/internal/core/streamtrack_h264.go index a4da2536..4deb1f28 100644 --- a/internal/core/streamtrack_h264.go +++ b/internal/core/streamtrack_h264.go @@ -54,9 +54,9 @@ func (t *streamTrackH264) updateTrackParameters(nalus [][]byte) { // remux is needed to // - fix corrupted streams // - make streams compatible with all protocols -func (t *streamTrackH264) remuxNALUs(dat *data) { +func (t *streamTrackH264) remuxNALUs(nalus [][]byte) [][]byte { n := 0 - for _, nalu := range dat.h264NALUs { + for _, nalu := range nalus { typ := h264.NALUType(nalu[0] & 0x1F) switch typ { case h264.NALUTypeSPS, h264.NALUTypePPS: @@ -69,10 +69,14 @@ func (t *streamTrackH264) remuxNALUs(dat *data) { n++ } + if n == 0 { + return nil + } + filteredNALUs := make([][]byte, n) i := 0 - for _, nalu := range dat.h264NALUs { + for _, nalu := range nalus { typ := h264.NALUType(nalu[0] & 0x1F) switch typ { case h264.NALUTypeSPS, h264.NALUTypePPS: @@ -95,15 +99,10 @@ func (t *streamTrackH264) remuxNALUs(dat *data) { i++ } - dat.h264NALUs = filteredNALUs + return filteredNALUs } func (t *streamTrackH264) generateRTPPackets(dat *data) { - // if remuxNALUs() returned nil, do not write any data - if dat.h264NALUs == nil { - return - } - pkts, err := t.rtpEncoder.Encode(dat.h264NALUs, dat.pts) if err != nil { return @@ -129,12 +128,14 @@ func (t *streamTrackH264) generateRTPPackets(dat *data) { } func (t *streamTrackH264) writeData(dat *data) { - t.updateTrackParameters(dat.h264NALUs) - t.remuxNALUs(dat) + if dat.h264NALUs != nil { + t.updateTrackParameters(dat.h264NALUs) + dat.h264NALUs = t.remuxNALUs(dat.h264NALUs) + } if dat.rtpPacket != nil { t.writeDataInner(dat) - } else { + } else if dat.h264NALUs != nil { t.generateRTPPackets(dat) } }