From c698bf1b612d71c2b5a3c9498756fe87592b6501 Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Thu, 14 Apr 2022 20:21:49 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E5=A2=9E=E5=BC=BA=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7=EF=BC=8Crtmp=E8=BD=ACmpegts=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8nalu=E4=B8=AD=E7=9A=84sps=E5=92=8Cpps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 23 +++++++++------ pkg/avc/avc_test.go | 9 ++++++ pkg/remux/rtmp2mpegts.go | 60 ++++++++++++++++++++++++++++++++++------ 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 6fc0873..c8714e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,15 @@ +/app/demo/push_rtmp_hook +/windows + /.idea /.vscode - -lal_record -logs -testdata +/.trash /bin /release /tmp /playground -profile.out -coverage.html -/coverage.txt -delay.txt +/windows/bin -/.trash /SECRET.md /TODO.md /pre-commit.sh @@ -22,6 +18,15 @@ delay.txt /conf/self.edge.conf.json /conf/https.conf.json +lal_record +logs +testdata + +profile.out +coverage.html +coverage.txt +delay.txt + *.aac *.h264 *.flv diff --git a/pkg/avc/avc_test.go b/pkg/avc/avc_test.go index 3d7033b..8f74271 100644 --- a/pkg/avc/avc_test.go +++ b/pkg/avc/avc_test.go @@ -345,3 +345,12 @@ func TestIssue135(t *testing.T) { b, err := avc.SpsPpsSeqHeader2Annexb(dst) nazalog.Debugf("%s, %+v", hex.Dump(b), err) } + +func TestTryParseSeqHeader(t *testing.T) { + // https://github.com/q191201771/lal/issues/143 + payload, err := hex.DecodeString("17000000000142000affe1000a6742000af80f0044be0801000568ce388000") + // "6742000af80f0044be08 68ce388000" + assert.Equal(t, nil, err) + err = avc.TryParseSeqHeader(payload) + assert.Equal(t, nil, err) +} diff --git a/pkg/remux/rtmp2mpegts.go b/pkg/remux/rtmp2mpegts.go index b9e8261..555d28c 100644 --- a/pkg/remux/rtmp2mpegts.go +++ b/pkg/remux/rtmp2mpegts.go @@ -10,7 +10,6 @@ package remux import ( "encoding/hex" - "github.com/q191201771/lal/pkg/aac" "github.com/q191201771/lal/pkg/avc" "github.com/q191201771/lal/pkg/base" @@ -183,6 +182,8 @@ func (s *Rtmp2MpegtsRemuxer) feedVideo(msg base.RtmpMsg) { Log.Errorf("[%s] iterate nalu failed. err=%+v, header=%+v, payload=%s", err, s.UniqueKey, msg.Header, hex.Dump(nazabytes.Prefix(msg.Payload, 32))) return } + + var vps, sps, pps []byte for _, nal := range nals { var nalType uint8 switch codecId { @@ -194,12 +195,55 @@ func (s *Rtmp2MpegtsRemuxer) feedVideo(msg base.RtmpMsg) { //Log.Debugf("[%s] naltype=%d, len=%d(%d), cts=%d, key=%t.", s.UniqueKey, nalType, nalBytes, len(msg.Payload), cts, msg.IsVideoKeyNalu()) - // 过滤掉原流中的sps pps aud - // sps pps前面已经缓存过了,后面有自己的写入逻辑 - // aud有自己的写入逻辑 - if (codecId == base.RtmpCodecIdAvc && (nalType == avc.NaluTypeSps || nalType == avc.NaluTypePps || nalType == avc.NaluTypeAud)) || - (codecId == base.RtmpCodecIdHevc && (nalType == hevc.NaluTypeVps || nalType == hevc.NaluTypeSps || nalType == hevc.NaluTypePps || nalType == hevc.NaluTypeAud)) { - continue + // 处理 sps pps aud + // + // aud 过滤掉,我们有自己的添加aud的逻辑 + // + // sps pps + // 注意,有的流,seq header中的sps和pps是错误的,需要从nals里获取sps pps并更新 + // 见 https://github.com/q191201771/lal/issues/143 + // + // TODO(chef): rtmp转其他类型的模块也存在这个问题,应该抽象出一个统一处理的地方 + // + if codecId == base.RtmpCodecIdAvc { + if nalType == avc.NaluTypeAud { + continue + } else if nalType == avc.NaluTypeSps { + sps = nal + continue + } else if nalType == avc.NaluTypePps { + pps = nal + if len(sps) != 0 && len(pps) != 0 { + s.spspps = s.spspps[0:0] + s.spspps = append(s.spspps, avc.NaluStartCode4...) + s.spspps = append(s.spspps, sps...) + s.spspps = append(s.spspps, avc.NaluStartCode4...) + s.spspps = append(s.spspps, pps...) + } + continue + } + } else if codecId == base.RtmpCodecIdHevc { + if nalType == hevc.NaluTypeAud { + continue + } else if nalType == hevc.NaluTypeVps { + vps = nal + continue + } else if nalType == avc.NaluTypeSps { + sps = nal + continue + } else if nalType == avc.NaluTypePps { + pps = nal + if len(vps) != 0 && len(sps) != 0 && len(pps) != 0 { + s.spspps = s.spspps[0:0] + s.spspps = append(s.spspps, avc.NaluStartCode4...) + s.spspps = append(s.spspps, vps...) + s.spspps = append(s.spspps, avc.NaluStartCode4...) + s.spspps = append(s.spspps, sps...) + s.spspps = append(s.spspps, avc.NaluStartCode4...) + s.spspps = append(s.spspps, pps...) + } + continue + } } // tag中的首个nalu前面写入aud @@ -335,7 +379,7 @@ func (s *Rtmp2MpegtsRemuxer) appendSpsPps(out []byte) ([]byte, error) { } func (s *Rtmp2MpegtsRemuxer) videoSeqHeaderCached() bool { - return s.spspps != nil + return len(s.spspps) != 0 } func (s *Rtmp2MpegtsRemuxer) audioCacheEmpty() bool {