From 59ae3add7e5debdb0ea4e55f1f1fafec2b953a25 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Thu, 1 Aug 2024 16:42:53 +0200 Subject: [PATCH] move codec-related constants into formatprocessor (#3595) --- internal/formatprocessor/av1.go | 7 ++++ internal/formatprocessor/h264.go | 11 ++++++ internal/formatprocessor/h265.go | 24 +++++++++++++ internal/formatprocessor/mpeg1_video.go | 9 +++++ internal/formatprocessor/mpeg4_video.go | 12 +++++++ internal/record/format_fmp4.go | 48 +++++-------------------- 6 files changed, 72 insertions(+), 39 deletions(-) diff --git a/internal/formatprocessor/av1.go b/internal/formatprocessor/av1.go index 05e96b97..a549dd10 100644 --- a/internal/formatprocessor/av1.go +++ b/internal/formatprocessor/av1.go @@ -13,6 +13,13 @@ import ( "github.com/bluenviron/mediamtx/internal/unit" ) +// AV1-related parameters +var ( + AV1DefaultSequenceHeader = []byte{ + 8, 0, 0, 0, 66, 167, 191, 228, 96, 13, 0, 64, + } +) + type formatProcessorAV1 struct { udpMaxPayloadSize int format *format.AV1 diff --git a/internal/formatprocessor/h264.go b/internal/formatprocessor/h264.go index 0d5cf289..55c2de78 100644 --- a/internal/formatprocessor/h264.go +++ b/internal/formatprocessor/h264.go @@ -14,6 +14,17 @@ import ( "github.com/bluenviron/mediamtx/internal/unit" ) +// H264-related parameters +var ( + H264DefaultSPS = []byte{ // 1920x1080 baseline + 0x67, 0x42, 0xc0, 0x28, 0xd9, 0x00, 0x78, 0x02, + 0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04, + 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9, 0x20, + } + + H264DefaultPPS = []byte{0x08, 0x06, 0x07, 0x08} +) + // extract SPS and PPS without decoding RTP packets func rtpH264ExtractParams(payload []byte) ([]byte, []byte) { if len(payload) < 1 { diff --git a/internal/formatprocessor/h265.go b/internal/formatprocessor/h265.go index eaa336d1..4e07b91b 100644 --- a/internal/formatprocessor/h265.go +++ b/internal/formatprocessor/h265.go @@ -14,6 +14,30 @@ import ( "github.com/bluenviron/mediamtx/internal/unit" ) +// H265-related parameters +var ( + H265DefaultVPS = []byte{ + 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x02, 0x20, + 0x00, 0x00, 0x03, 0x00, 0xb0, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x7b, 0x18, 0xb0, 0x24, + } + + H265DefaultSPS = []byte{ + 0x42, 0x01, 0x01, 0x02, 0x20, 0x00, 0x00, 0x03, + 0x00, 0xb0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, + 0x00, 0x7b, 0xa0, 0x07, 0x82, 0x00, 0x88, 0x7d, + 0xb6, 0x71, 0x8b, 0x92, 0x44, 0x80, 0x53, 0x88, + 0x88, 0x92, 0xcf, 0x24, 0xa6, 0x92, 0x72, 0xc9, + 0x12, 0x49, 0x22, 0xdc, 0x91, 0xaa, 0x48, 0xfc, + 0xa2, 0x23, 0xff, 0x00, 0x01, 0x00, 0x01, 0x6a, + 0x02, 0x02, 0x02, 0x01, + } + + H265DefaultPPS = []byte{ + 0x44, 0x01, 0xc0, 0x25, 0x2f, 0x05, 0x32, 0x40, + } +) + // extract VPS, SPS and PPS without decoding RTP packets func rtpH265ExtractParams(payload []byte) ([]byte, []byte, []byte) { if len(payload) < 2 { diff --git a/internal/formatprocessor/mpeg1_video.go b/internal/formatprocessor/mpeg1_video.go index 8083aeff..079049ff 100644 --- a/internal/formatprocessor/mpeg1_video.go +++ b/internal/formatprocessor/mpeg1_video.go @@ -13,6 +13,15 @@ import ( "github.com/bluenviron/mediamtx/internal/unit" ) +// MPEG-1 video related parameters +var ( + MPEG1VideoDefaultConfig = []byte{ + 0x00, 0x00, 0x01, 0xb3, 0x78, 0x04, 0x38, 0x35, + 0xff, 0xff, 0xe0, 0x18, 0x00, 0x00, 0x01, 0xb5, + 0x14, 0x4a, 0x00, 0x01, 0x00, 0x00, + } +) + type formatProcessorMPEG1Video struct { udpMaxPayloadSize int format *format.MPEG1Video diff --git a/internal/formatprocessor/mpeg4_video.go b/internal/formatprocessor/mpeg4_video.go index eac47fc1..63691231 100644 --- a/internal/formatprocessor/mpeg4_video.go +++ b/internal/formatprocessor/mpeg4_video.go @@ -15,6 +15,18 @@ import ( "github.com/bluenviron/mediamtx/internal/unit" ) +// MPEG-4 video related parameters +var ( + MPEG4VideoDefaultConfig = []byte{ + 0x00, 0x00, 0x01, 0xb0, 0x01, 0x00, 0x00, 0x01, + 0xb5, 0x89, 0x13, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x01, 0x20, 0x00, 0xc4, 0x8d, 0x88, 0x00, + 0xf5, 0x3c, 0x04, 0x87, 0x14, 0x63, 0x00, 0x00, + 0x01, 0xb2, 0x4c, 0x61, 0x76, 0x63, 0x35, 0x38, + 0x2e, 0x31, 0x33, 0x34, 0x2e, 0x31, 0x30, 0x30, + } +) + type formatProcessorMPEG4Video struct { udpMaxPayloadSize int format *format.MPEG4Video diff --git a/internal/record/format_fmp4.go b/internal/record/format_fmp4.go index 6041b34f..462f24fd 100644 --- a/internal/record/format_fmp4.go +++ b/internal/record/format_fmp4.go @@ -20,8 +20,8 @@ import ( "github.com/bluenviron/mediacommon/pkg/formats/fmp4" "github.com/bluenviron/mediamtx/internal/defs" + "github.com/bluenviron/mediamtx/internal/formatprocessor" "github.com/bluenviron/mediamtx/internal/logger" - "github.com/bluenviron/mediamtx/internal/test" "github.com/bluenviron/mediamtx/internal/unit" ) @@ -145,9 +145,7 @@ func (f *formatFMP4) initialize() { switch forma := forma.(type) { case *rtspformat.AV1: codec := &fmp4.CodecAV1{ - SequenceHeader: []byte{ - 8, 0, 0, 0, 66, 167, 191, 228, 96, 13, 0, 64, - }, + SequenceHeader: formatprocessor.AV1DefaultSequenceHeader, } track := addTrack(forma, codec) @@ -278,26 +276,9 @@ func (f *formatFMP4) initialize() { vps, sps, pps := forma.SafeParams() if vps == nil || sps == nil || pps == nil { - vps = []byte{ - 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x02, 0x20, - 0x00, 0x00, 0x03, 0x00, 0xb0, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x7b, 0x18, 0xb0, 0x24, - } - - sps = []byte{ - 0x42, 0x01, 0x01, 0x02, 0x20, 0x00, 0x00, 0x03, - 0x00, 0xb0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, - 0x00, 0x7b, 0xa0, 0x07, 0x82, 0x00, 0x88, 0x7d, - 0xb6, 0x71, 0x8b, 0x92, 0x44, 0x80, 0x53, 0x88, - 0x88, 0x92, 0xcf, 0x24, 0xa6, 0x92, 0x72, 0xc9, - 0x12, 0x49, 0x22, 0xdc, 0x91, 0xaa, 0x48, 0xfc, - 0xa2, 0x23, 0xff, 0x00, 0x01, 0x00, 0x01, 0x6a, - 0x02, 0x02, 0x02, 0x01, - } - - pps = []byte{ - 0x44, 0x01, 0xc0, 0x25, 0x2f, 0x05, 0x32, 0x40, - } + vps = formatprocessor.H265DefaultVPS + sps = formatprocessor.H265DefaultSPS + pps = formatprocessor.H265DefaultPPS } codec := &fmp4.CodecH265{ @@ -375,8 +356,8 @@ func (f *formatFMP4) initialize() { sps, pps := forma.SafeParams() if sps == nil || pps == nil { - sps = test.FormatH264.SPS - pps = test.FormatH264.PPS + sps = formatprocessor.H264DefaultSPS + pps = formatprocessor.H264DefaultPPS } codec := &fmp4.CodecH264{ @@ -446,14 +427,7 @@ func (f *formatFMP4) initialize() { config := forma.SafeParams() if config == nil { - config = []byte{ - 0x00, 0x00, 0x01, 0xb0, 0x01, 0x00, 0x00, 0x01, - 0xb5, 0x89, 0x13, 0x00, 0x00, 0x01, 0x00, 0x00, - 0x00, 0x01, 0x20, 0x00, 0xc4, 0x8d, 0x88, 0x00, - 0xf5, 0x3c, 0x04, 0x87, 0x14, 0x63, 0x00, 0x00, - 0x01, 0xb2, 0x4c, 0x61, 0x76, 0x63, 0x35, 0x38, - 0x2e, 0x31, 0x33, 0x34, 0x2e, 0x31, 0x30, 0x30, - } + config = formatprocessor.MPEG4VideoDefaultConfig } codec := &fmp4.CodecMPEG4Video{ @@ -506,11 +480,7 @@ func (f *formatFMP4) initialize() { case *rtspformat.MPEG1Video: codec := &fmp4.CodecMPEG1Video{ - Config: []byte{ - 0x00, 0x00, 0x01, 0xb3, 0x78, 0x04, 0x38, 0x35, - 0xff, 0xff, 0xe0, 0x18, 0x00, 0x00, 0x01, 0xb5, - 0x14, 0x4a, 0x00, 0x01, 0x00, 0x00, - }, + Config: formatprocessor.MPEG1VideoDefaultConfig, } track := addTrack(forma, codec)