fix DTS error in case of H264 NALUs without POC

This commit is contained in:
aler9 2022-06-15 10:50:59 +02:00
parent 039d106efa
commit 3e5f62156d
3 changed files with 56 additions and 4 deletions

View File

@ -353,7 +353,21 @@ func (c *rtmpConn) runRead(ctx context.Context) error {
}
pts := data.h264PTS - *videoInitialPTS
idrPresent := h264.IDRPresent(data.h264NALUs)
idrPresent := false
nonIDRPresent := false
for _, nalu := range data.h264NALUs {
typ := h264.NALUType(nalu[0] & 0x1F)
switch typ {
case h264.NALUTypeIDR:
idrPresent = true
case h264.NALUTypeNonIDR:
nonIDRPresent = true
}
}
var dts time.Duration
// wait until we receive an IDR
@ -375,6 +389,10 @@ func (c *rtmpConn) runRead(ctx context.Context) error {
dts = 0
pts -= videoStartDTS
} else {
if !idrPresent && !nonIDRPresent {
continue
}
var err error
dts, err = videoDTSExtractor.Extract(data.h264NALUs, pts)
if err != nil {

View File

@ -122,13 +122,29 @@ func (m *muxerVariantFMP4Segmenter) adjustPartDuration(du time.Duration) {
}
func (m *muxerVariantFMP4Segmenter) writeH264(pts time.Duration, nalus [][]byte) error {
idrPresent := false
nonIDRPresent := false
for _, nalu := range nalus {
typ := h264.NALUType(nalu[0] & 0x1F)
switch typ {
case h264.NALUTypeIDR:
idrPresent = true
case h264.NALUTypeNonIDR:
nonIDRPresent = true
}
}
if !idrPresent && !nonIDRPresent {
return nil
}
avcc, err := h264.AVCCEncode(nalus)
if err != nil {
return err
}
idrPresent := h264.IDRPresent(nalus)
return m.writeH264Entry(&fmp4VideoSample{
pts: pts,
nalus: nalus,

View File

@ -79,7 +79,21 @@ func newMuxerVariantMPEGTSSegmenter(
func (m *muxerVariantMPEGTSSegmenter) writeH264(pts time.Duration, nalus [][]byte) error {
now := time.Now()
idrPresent := h264.IDRPresent(nalus)
idrPresent := false
nonIDRPresent := false
for _, nalu := range nalus {
typ := h264.NALUType(nalu[0] & 0x1F)
switch typ {
case h264.NALUTypeIDR:
idrPresent = true
case h264.NALUTypeNonIDR:
nonIDRPresent = true
}
}
var dts time.Duration
if m.currentSegment == nil {
@ -105,6 +119,10 @@ func (m *muxerVariantMPEGTSSegmenter) writeH264(pts time.Duration, nalus [][]byt
m.currentSegment = newMuxerVariantMPEGTSSegment(now, m.segmentMaxSize,
m.videoTrack, m.audioTrack, m.writer.WriteData)
} else {
if !idrPresent && !nonIDRPresent {
return nil
}
var err error
dts, err = m.videoDTSExtractor.Extract(nalus, pts)
if err != nil {