diff --git a/internal/hls/muxer_test.go b/internal/hls/muxer_test.go index 9a6f8c9a..bbcd535f 100644 --- a/internal/hls/muxer_test.go +++ b/internal/hls/muxer_test.go @@ -159,10 +159,10 @@ func TestMuxerVideoAudio(t *testing.T) { `\n` + `#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` + `#EXTINF:4,\n` + - `([0-9]+\.ts)\n` + + `(seg0\.ts)\n` + `#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` + `#EXTINF:1,\n` + - `([0-9]+\.ts)\n$`) + `(seg1\.ts)\n$`) ma = re.FindStringSubmatch(string(byts)) } else { re := regexp.MustCompile(`^#EXTM3U\n` + @@ -493,10 +493,10 @@ func TestMuxerVideoOnly(t *testing.T) { `\n` + `#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` + `#EXTINF:4,\n` + - `([0-9]+\.ts)\n` + + `(seg0\.ts)\n` + `#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` + `#EXTINF:1,\n` + - `([0-9]+\.ts)\n$`) + `(seg1\.ts)\n$`) ma = re.FindStringSubmatch(string(byts)) } else { re := regexp.MustCompile(`^#EXTM3U\n` + @@ -720,7 +720,7 @@ func TestMuxerAudioOnly(t *testing.T) { `\n` + `#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` + `#EXTINF:1,\n` + - `([0-9]+\.ts)\n$`) + `(seg0\.ts)\n$`) ma = re.FindStringSubmatch(string(byts)) } else { re := regexp.MustCompile(`^#EXTM3U\n` + @@ -940,7 +940,7 @@ func TestMuxerDoubleRead(t *testing.T) { `\n` + `#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` + `#EXTINF:2,\n` + - `([0-9]+\.ts)\n$`) + `(seg0\.ts)\n$`) ma := re.FindStringSubmatch(string(byts)) require.NotEqual(t, 0, len(ma)) diff --git a/internal/hls/muxer_variant_mpegts_segment.go b/internal/hls/muxer_variant_mpegts_segment.go index 6aacdf19..4f8d2d7c 100644 --- a/internal/hls/muxer_variant_mpegts_segment.go +++ b/internal/hls/muxer_variant_mpegts_segment.go @@ -28,6 +28,7 @@ type muxerVariantMPEGTSSegment struct { } func newMuxerVariantMPEGTSSegment( + id uint64, startTime time.Time, segmentMaxSize uint64, videoTrack *gortsplib.TrackH264, @@ -40,7 +41,7 @@ func newMuxerVariantMPEGTSSegment( audioTrack: audioTrack, writer: writer, startTime: startTime, - name: strconv.FormatInt(startTime.Unix(), 10), + name: "seg" + strconv.FormatUint(id, 10), } return t diff --git a/internal/hls/muxer_variant_mpegts_segmenter.go b/internal/hls/muxer_variant_mpegts_segmenter.go index 41a3617b..fa4102c1 100644 --- a/internal/hls/muxer_variant_mpegts_segmenter.go +++ b/internal/hls/muxer_variant_mpegts_segmenter.go @@ -21,6 +21,7 @@ type muxerVariantMPEGTSSegmenter struct { onSegmentReady func(*muxerVariantMPEGTSSegment) writer *mpegts.Writer + nextSegmentID uint64 currentSegment *muxerVariantMPEGTSSegment videoDTSExtractor *h264.DTSExtractor startPCR time.Time @@ -49,6 +50,12 @@ func newMuxerVariantMPEGTSSegmenter( return m } +func (m *muxerVariantMPEGTSSegmenter) genSegmentID() uint64 { + id := m.nextSegmentID + m.nextSegmentID++ + return id +} + func (m *muxerVariantMPEGTSSegmenter) writeH264(now time.Time, pts time.Duration, nalus [][]byte) error { idrPresent := false nonIDRPresent := false @@ -86,8 +93,13 @@ func (m *muxerVariantMPEGTSSegmenter) writeH264(now time.Time, pts time.Duration pts -= m.startDTS // create first segment - m.currentSegment = newMuxerVariantMPEGTSSegment(now, m.segmentMaxSize, - m.videoTrack, m.audioTrack, m.writer) + m.currentSegment = newMuxerVariantMPEGTSSegment( + m.genSegmentID(), + now, + m.segmentMaxSize, + m.videoTrack, + m.audioTrack, + m.writer) } else { if !idrPresent && !nonIDRPresent { return nil @@ -107,8 +119,13 @@ func (m *muxerVariantMPEGTSSegmenter) writeH264(now time.Time, pts time.Duration (dts-*m.currentSegment.startDTS) >= m.segmentDuration { m.currentSegment.finalize(dts) m.onSegmentReady(m.currentSegment) - m.currentSegment = newMuxerVariantMPEGTSSegment(now, m.segmentMaxSize, - m.videoTrack, m.audioTrack, m.writer) + m.currentSegment = newMuxerVariantMPEGTSSegment( + m.genSegmentID(), + now, + m.segmentMaxSize, + m.videoTrack, + m.audioTrack, + m.writer) } } @@ -133,8 +150,13 @@ func (m *muxerVariantMPEGTSSegmenter) writeAAC(now time.Time, pts time.Duration, pts = 0 // create first segment - m.currentSegment = newMuxerVariantMPEGTSSegment(now, m.segmentMaxSize, - m.videoTrack, m.audioTrack, m.writer) + m.currentSegment = newMuxerVariantMPEGTSSegment( + m.genSegmentID(), + now, + m.segmentMaxSize, + m.videoTrack, + m.audioTrack, + m.writer) } else { pts -= m.startDTS @@ -143,8 +165,13 @@ func (m *muxerVariantMPEGTSSegmenter) writeAAC(now time.Time, pts time.Duration, (pts-*m.currentSegment.startDTS) >= m.segmentDuration { m.currentSegment.finalize(pts) m.onSegmentReady(m.currentSegment) - m.currentSegment = newMuxerVariantMPEGTSSegment(now, m.segmentMaxSize, - m.videoTrack, m.audioTrack, m.writer) + m.currentSegment = newMuxerVariantMPEGTSSegment( + m.genSegmentID(), + now, + m.segmentMaxSize, + m.videoTrack, + m.audioTrack, + m.writer) } } } else {