Skip to content

Export GetCodecMap for efficient payload type lookups#248

Open
asnyatkov wants to merge 1 commit intopion:mainfrom
asnyatkov:feat/export-codec-map
Open

Export GetCodecMap for efficient payload type lookups#248
asnyatkov wants to merge 1 commit intopion:mainfrom
asnyatkov:feat/export-codec-map

Conversation

@asnyatkov
Copy link
Copy Markdown

Description

Rename buildCodecMap to GetCodecMap so callers can build the codec map once and reuse it for multiple payload type lookups. CPU overhead manifests itself in thundering herd scenario when we're creating new peer connections at a very high TPS rate.

This is needed by pion/webrtc's codecsFromMediaDescription (sdp.go:1101), which currently 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 instead.

There will be webrtc integration patch in webrtc. We should bump minor SDP version to avoid compatibility issues.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.43%. Comparing base (f66f2eb) to head (5014502).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #248   +/-   ##
=======================================
  Coverage   97.43%   97.43%           
=======================================
  Files          12       12           
  Lines        1405     1405           
=======================================
  Hits         1369     1369           
  Misses         19       19           
  Partials       17       17           
Flag Coverage Δ
go 97.43% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.
@asnyatkov asnyatkov force-pushed the feat/export-codec-map branch from 5e27285 to 5014502 Compare March 30, 2026 23:16
@JoTurk
Copy link
Copy Markdown
Member

JoTurk commented Mar 31, 2026

Can we cache codec map inside the library instead, I think this was the original plan when it got merged, I'm down for full api change if we have to and a major release but sdp users shouldn't have to implement the helpers themself.
I think it's a failure if they have to use a codec map api themself.

@asnyatkov
Copy link
Copy Markdown
Author

With the way the code is structured right now, the only way to implement your suggestion is to add lazy caching to buildCodecMap. I was thinking that it can be done when SDP is parsed, but turns out webrtc doesn't do any parsing:

func codecsFromMediaDescription(mediaDescr *sdp.MediaDescription) (out []RTPCodecParameters, err error) {
        // vvvvv The descriptor is not parsed but constructed just to invoke GetCodecForPayloadType
	s := &sdp.SessionDescription{
		MediaDescriptions: []*sdp.MediaDescription{mediaDescr},
	}
        .......

Is this approach ok? That would also introduce private fields in sdp.SessionDescription.

@asnyatkov
Copy link
Copy Markdown
Author

@JoTurk small nudge. This is a benign change, but it does make a difference in thundering herd scenarios.

@JoTurk
Copy link
Copy Markdown
Member

JoTurk commented May 1, 2026

What if we merge this now, and plan for a next major release where we control the constructors and the mutation calls so we can have internal map?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants