Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ func mergeCodecs(codec Codec, codecs map[uint8]Codec) {
codecs[savedCodec.PayloadType] = savedCodec
}

func (s *SessionDescription) buildCodecMap() map[uint8]Codec { //nolint:cyclop
// GetCodecMap builds the codec map from the session description.
func (s *SessionDescription) GetCodecMap() map[uint8]Codec { //nolint:cyclop
codecs := map[uint8]Codec{
// static codecs that do not require a rtpmap
0: {
Expand Down Expand Up @@ -326,7 +327,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 {
Expand All @@ -337,7 +338,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 {
Expand All @@ -353,7 +354,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) {
Expand Down
22 changes: 22 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@
},
}

codecs := sd.buildCodecMap()

Check failure on line 440 in util_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

sd.buildCodecMap undefined (type SessionDescription has no field or method buildCodecMap) (typecheck)

Check failure on line 440 in util_test.go

View workflow job for this annotation

GitHub Actions / test-macos (1.25) / Go macOS 1.25

sd.buildCodecMap undefined (type SessionDescription has no field or method buildCodecMap)

Check failure on line 440 in util_test.go

View workflow job for this annotation

GitHub Actions / test (1.25) / Go 1.25

sd.buildCodecMap undefined (type SessionDescription has no field or method buildCodecMap)

Check failure on line 440 in util_test.go

View workflow job for this annotation

GitHub Actions / test (1.24) / Go 1.24

sd.buildCodecMap undefined (type SessionDescription has no field or method buildCodecMap)

Check failure on line 440 in util_test.go

View workflow job for this annotation

GitHub Actions / test-macos (1.24) / Go macOS 1.24

sd.buildCodecMap undefined (type SessionDescription has no field or method buildCodecMap)

// the three static codecs should be present, unchanged.
if assert.Len(t, codecs, 3) {
Expand Down Expand Up @@ -521,3 +521,25 @@
var se syntaxError
assert.ErrorAs(t, err, &se)
}

func BenchmarkGetCodecForPayloadType(b *testing.B) {
sd := getTestSessionDescription()
payloadTypes := []uint8{120, 121, 126, 97, 98}

b.Run("WithoutCaching", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, pt := range payloadTypes {
_, _ = sd.GetCodecForPayloadType(pt)
}
}
})

b.Run("WithCaching", func(b *testing.B) {
for i := 0; i < b.N; i++ {
codecMap := sd.GetCodecMap()
for _, pt := range payloadTypes {
_ = codecMap[pt]
}
}
})
}
Loading