hls: use segment IDs as segment names

when hlsVariant is mpegts, this prevents conflicts that were previously
caused by the fact that the timestamp was used as segment name.
This commit is contained in:
aler9 2022-08-27 16:20:25 +02:00
parent 4c0833664d
commit 42b6e1f922
3 changed files with 43 additions and 15 deletions

View File

@ -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))

View File

@ -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

View File

@ -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 {