From f6204847578000091cb94cc634d3ca96a20c7204 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 10 Apr 2022 17:07:42 +0200 Subject: [PATCH] rtmp: always send decoder config before IDR frames --- internal/core/rtmp_conn.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/internal/core/rtmp_conn.go b/internal/core/rtmp_conn.go index 988cf1e6..29efc975 100644 --- a/internal/core/rtmp_conn.go +++ b/internal/core/rtmp_conn.go @@ -337,8 +337,6 @@ func (c *rtmpConn) runRead(ctx context.Context) error { continue } - // TODO: send H264DecoderConfig instead of NALUs? - // wait until we receive an IDR if !videoFirstIDRFound { if !h264.IDRPresent(data.h264NALUs) { @@ -350,6 +348,29 @@ func (c *rtmpConn) runRead(ctx context.Context) error { videoDTSEst = h264.NewDTSEstimator() } + if h264.IDRPresent(data.h264NALUs) { + codec := nh264.Codec{ + SPS: map[int][]byte{ + 0: videoTrack.SPS(), + }, + PPS: map[int][]byte{ + 0: videoTrack.PPS(), + }, + } + b := make([]byte, 128) + var n int + codec.ToConfig(b, &n) + b = b[:n] + + err = c.conn.WritePacket(av.Packet{ + Type: av.H264DecoderConfig, + Data: b, + }) + if err != nil { + return err + } + } + avcc, err := h264.EncodeAVCC(data.h264NALUs) if err != nil { return err