diff --git a/internal/core/rtmp_conn.go b/internal/core/rtmp_conn.go index 555207a3..0b6efb5a 100644 --- a/internal/core/rtmp_conn.go +++ b/internal/core/rtmp_conn.go @@ -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 { diff --git a/internal/hls/muxer_variant_fmp4_segmenter.go b/internal/hls/muxer_variant_fmp4_segmenter.go index 8f1d2b4c..e6296481 100644 --- a/internal/hls/muxer_variant_fmp4_segmenter.go +++ b/internal/hls/muxer_variant_fmp4_segmenter.go @@ -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, diff --git a/internal/hls/muxer_variant_mpegts_segmenter.go b/internal/hls/muxer_variant_mpegts_segmenter.go index dbaa9f22..2fb15c58 100644 --- a/internal/hls/muxer_variant_mpegts_segmenter.go +++ b/internal/hls/muxer_variant_mpegts_segmenter.go @@ -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 {