From a5db98521dde72ecd4fe882fbd011cb0489f1334 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Fri, 22 Sep 2023 13:49:05 +0200 Subject: [PATCH] discard invalid H264 parameters (#431) (https://github.com/bluenviron/mediamtx/issues/2348) --- pkg/format/format_test.go | 19 +++++++++++++++++++ pkg/format/h264.go | 16 +++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/pkg/format/format_test.go b/pkg/format/format_test.go index 7f6ffe1c..d0d114ec 100644 --- a/pkg/format/format_test.go +++ b/pkg/format/format_test.go @@ -808,6 +808,25 @@ var casesFormat = []struct { "sprop-parameter-sets": "Z00AHo2NQFoe0IAAA4QAAK/IAg==,aO44gA==", }, }, + { + "video h264 with unparsable parameters (mediamtx/2348)", + "video", + 96, + "H264/90000", + map[string]string{ + "sprop-parameter-sets": "QgEBAWAAAAMAAAMAAAMAAAMAlqADwIAQ5Y2uSTJrlnAIAAADAAgAAAMAyEA=,RAHgdrAmQA==", + "packetization-mode": "1", + "profile-level-id": "010101", + }, + &H264{ + PayloadTyp: 96, + PacketizationMode: 1, + }, + "H264/90000", + map[string]string{ + "packetization-mode": "1", + }, + }, { "video h265", "video", diff --git a/pkg/format/h264.go b/pkg/format/h264.go index b9813504..44028ea3 100644 --- a/pkg/format/h264.go +++ b/pkg/format/h264.go @@ -34,28 +34,30 @@ func (f *H264) unmarshal(ctx *unmarshalContext) error { case "sprop-parameter-sets": tmp := strings.Split(val, ",") if len(tmp) >= 2 { - var err error - f.SPS, err = base64.StdEncoding.DecodeString(tmp[0]) + sps, err := base64.StdEncoding.DecodeString(tmp[0]) if err != nil { return fmt.Errorf("invalid sprop-parameter-sets (%v)", val) } // some cameras ship parameters with Annex-B prefix - f.SPS = bytes.TrimPrefix(f.SPS, []byte{0, 0, 0, 1}) + sps = bytes.TrimPrefix(sps, []byte{0, 0, 0, 1}) - f.PPS, err = base64.StdEncoding.DecodeString(tmp[1]) + pps, err := base64.StdEncoding.DecodeString(tmp[1]) if err != nil { return fmt.Errorf("invalid sprop-parameter-sets (%v)", val) } // some cameras ship parameters with Annex-B prefix - f.PPS = bytes.TrimPrefix(f.PPS, []byte{0, 0, 0, 1}) + pps = bytes.TrimPrefix(pps, []byte{0, 0, 0, 1}) var spsp h264.SPS - err = spsp.Unmarshal(f.SPS) + err = spsp.Unmarshal(sps) if err != nil { - return fmt.Errorf("invalid SPS: %v", err) + continue } + + f.SPS = sps + f.PPS = pps } case "packetization-mode":