Skip to content

Commit

Permalink
refactor format parser (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Jan 18, 2025
1 parent b7d86f6 commit 4c8670a
Showing 1 changed file with 56 additions and 63 deletions.
119 changes: 56 additions & 63 deletions pkg/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,102 +124,95 @@ func Unmarshal(md *psdp.MediaDescription, payloadTypeStr string) (Format, error)

rtpMap := getFormatAttribute(md.Attributes, payloadType, "rtpmap")
fmtp := decodeFMTP(getFormatAttribute(md.Attributes, payloadType, "fmtp"))

codec, clock := getCodecAndClock(rtpMap)

format := func() Format {
switch {
/*
* static payload types
* dynamic payload types
**/

// video

case payloadType == 32:
return &MPEG1Video{}
case codec == "av1" && clock == "90000" && payloadType >= 96 && payloadType <= 127:
return &AV1{}

case payloadType == 26:
return &MJPEG{}
case codec == "vp9" && clock == "90000" && payloadType >= 96 && payloadType <= 127:
return &VP9{}

case payloadType == 33:
return &MPEGTS{}
case codec == "vp8" && clock == "90000" && payloadType >= 96 && payloadType <= 127:
return &VP8{}

case payloadType == 35 && codec == "h264" && clock == "90000": // Bosch cameras
return &H264{}
case codec == "h265" && clock == "90000" && payloadType >= 96 && payloadType <= 127:
return &H265{}

// audio
case codec == "h264" && clock == "90000" && ((payloadType >= 96 && payloadType <= 127) || payloadType == 35):
return &H264{}

case payloadType == 14:
return &MPEG1Audio{}
case codec == "mp4v-es" && clock == "90000" && payloadType >= 96 && payloadType <= 127:
return &MPEG4Video{}

case payloadType == 9:
return &G722{}
// audio

case payloadType == 0, payloadType == 8:
return &G711{}
case codec == "opus", codec == "multiopus" && payloadType >= 96 && payloadType <= 127:
return &Opus{}

case payloadType == 10, payloadType == 11:
return &LPCM{}
case codec == "vorbis" && payloadType >= 96 && payloadType <= 127:
return &Vorbis{}

/*
* dynamic payload types
**/
case (codec == "mpeg4-generic" || codec == "mp4a-latm") && payloadType >= 96 && payloadType <= 127:
return &MPEG4Audio{}

case payloadType >= 96 && payloadType <= 127:
switch {
// video
case codec == "ac3" && payloadType >= 96 && payloadType <= 127:
return &AC3{}

case codec == "av1" && clock == "90000":
return &AV1{}
case codec == "speex" && payloadType >= 96 && payloadType <= 127:
return &Speex{}

case codec == "vp9" && clock == "90000":
return &VP9{}
case (codec == "g726-16" ||
codec == "g726-24" ||
codec == "g726-32" ||
codec == "g726-40" ||
codec == "aal2-g726-16" ||
codec == "aal2-g726-24" ||
codec == "aal2-g726-32" ||
codec == "aal2-g726-40") && clock == "8000" && payloadType >= 96 && payloadType <= 127:
return &G726{}

case codec == "vp8" && clock == "90000":
return &VP8{}

case codec == "h265" && clock == "90000":
return &H265{}
case codec == "pcma", codec == "pcmu" && payloadType >= 96 && payloadType <= 127:
return &G711{}

case codec == "h264" && clock == "90000":
return &H264{}
case codec == "l8", codec == "l16", codec == "l24" && payloadType >= 96 && payloadType <= 127:
return &LPCM{}

case codec == "mp4v-es" && clock == "90000":
return &MPEG4Video{}
/*
* static payload types
**/

// audio
// video

case codec == "opus", codec == "multiopus":
return &Opus{}
case payloadType == 32:
return &MPEG1Video{}

case codec == "vorbis":
return &Vorbis{}
case payloadType == 26:
return &MJPEG{}

case codec == "mpeg4-generic", codec == "mp4a-latm":
return &MPEG4Audio{}
case payloadType == 33:
return &MPEGTS{}

case codec == "ac3":
return &AC3{}
// audio

case codec == "speex":
return &Speex{}
case payloadType == 14:
return &MPEG1Audio{}

case (codec == "g726-16" ||
codec == "g726-24" ||
codec == "g726-32" ||
codec == "g726-40" ||
codec == "aal2-g726-16" ||
codec == "aal2-g726-24" ||
codec == "aal2-g726-32" ||
codec == "aal2-g726-40") && clock == "8000":
return &G726{}
case payloadType == 9:
return &G722{}

case codec == "pcma", codec == "pcmu":
return &G711{}
case payloadType == 0, payloadType == 8:
return &G711{}

case codec == "l8", codec == "l16", codec == "l24":
return &LPCM{}
}
case payloadType == 10, payloadType == 11:
return &LPCM{}
}

return &Generic{}
Expand Down

0 comments on commit 4c8670a

Please sign in to comment.