From 50145028dd7c67f297fd56603eaa28d88344fec4 Mon Sep 17 00:00:00 2001 From: Alex Snyatkov Date: Thu, 26 Mar 2026 22:28:45 +0000 Subject: [PATCH] Export GetCodecMap for payload lookups Rename buildCodecMap to GetCodecMap so callers can build the codec map once and reuse it for multiple payload type lookups. This is needed by pion/webrtc's codecsFromMediaDescription (sdp.go:1101), which calls GetCodecForPayloadType in a loop (sdp.go:1112), rebuilding the full codec map on every iteration. With GetCodecMap exported, webrtc can build the map once and do direct lookups. --- util.go | 11 +++++++---- util_test.go | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/util.go b/util.go index b6f6a98..c68fb50 100644 --- a/util.go +++ b/util.go @@ -226,7 +226,10 @@ func mergeCodecs(codec Codec, codecs map[uint8]Codec) { codecs[savedCodec.PayloadType] = savedCodec } -func (s *SessionDescription) buildCodecMap() map[uint8]Codec { //nolint:cyclop +// GetCodecMap parses the SessionDescription and returns a map of payload +// type to Codec. This allows callers to build the map once and look up +// multiple payload types without rebuilding it each time. +func (s *SessionDescription) GetCodecMap() map[uint8]Codec { //nolint:cyclop codecs := map[uint8]Codec{ // static codecs that do not require a rtpmap 0: { @@ -326,7 +329,7 @@ func codecsMatch(wanted, got Codec) bool { // GetCodecForPayloadType scans the SessionDescription for the given payload type and returns the codec. func (s *SessionDescription) GetCodecForPayloadType(payloadType uint8) (Codec, error) { - codecs := s.buildCodecMap() + codecs := s.GetCodecMap() codec, ok := codecs[payloadType] if ok { @@ -337,7 +340,7 @@ func (s *SessionDescription) GetCodecForPayloadType(payloadType uint8) (Codec, e } func (s *SessionDescription) GetCodecsForPayloadTypes(payloadTypes []uint8) ([]Codec, error) { - codecs := s.buildCodecMap() + codecs := s.GetCodecMap() result := make([]Codec, 0, len(payloadTypes)) for _, payloadType := range payloadTypes { @@ -353,7 +356,7 @@ func (s *SessionDescription) GetCodecsForPayloadTypes(payloadTypes []uint8) ([]C // GetPayloadTypeForCodec scans the SessionDescription for a codec that matches the provided codec // as closely as possible and returns its payload type. func (s *SessionDescription) GetPayloadTypeForCodec(wanted Codec) (uint8, error) { - codecs := s.buildCodecMap() + codecs := s.GetCodecMap() for payloadType, codec := range codecs { if codecsMatch(wanted, codec) { diff --git a/util_test.go b/util_test.go index 1f1d82c..cf53027 100644 --- a/util_test.go +++ b/util_test.go @@ -437,7 +437,7 @@ func TestBuildCodecMap_RtcpFbError(t *testing.T) { }, } - codecs := sd.buildCodecMap() + codecs := sd.GetCodecMap() // the three static codecs should be present, unchanged. if assert.Len(t, codecs, 3) {