Skip to content

feat: support PCMA and PCMU audio publishing - #2287

Open
huangyouyi wants to merge 1 commit into
livekit:mainfrom
huangyouyi:feat/2281-pcma-pcmu
Open

huangyouyi wants to merge 1 commit into
livekit:mainfrom
huangyouyi:feat/2281-pcma-pcmu

Conversation

@huangyouyi

@huangyouyi huangyouyi commented Nov 19, 2026 •

Copy link
Copy Markdown

What and why

Apps that interoperate with G.711 endpoints can request pcma or pcmu when publishing browser audio. audioCodec accepts a codec name or an object discriminated by codec.

The SDK signals the preference through AddTrackRequest.simulcastCodecs and uses the existing publish and negotiation flow. The SFU handles codec fallback. Omitting audioCodec keeps default negotiation.

Fixes #2281

Checklist

  • An AI tool wrote or helped write this change (OpenAI Codex).

@CLAassistant

CLAassistant commented Nov 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@changeset-bot

changeset-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2d7469a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
livekit-client Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@1egoman 1egoman left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together! I started going through a review but paused halfway through - I think the approach can be a lot simpler:

  • The server already handles audio codec selection today through AddTrackRequest.simulcastCodecs. It adds the audio/ prefix, checks the codec against enabledPublishCodecs, falls back to another codec if needed, and reorders the publisher answer so the requested codec comes first.
  • I think the core of this feature should be req.simulcastCodecs = [{ codec, cid }] for audio, using the existing addTrack + negotiate flow, the same way videoCodec works today.
  • Besides adding extra complexity, because your solution today calls setCodecPreferences with only PCMA/PCMU, it removes any audio fallback. That's what forces the server/browser pre-checks, the 415 errors, the custom teardown path, and bypassing engine.removeTrack.
  • The separate G.711 ordering (createSender -> addTrack -> negotiate) shouldn't be needed. AddTrack already goes out on the signal connection before the offer, which is why the parallel video path works. Dropping it also removes the need to defer the mute/unmute listeners, the manual onTrackMuted sync, and the G.711-only Ended cleanup.
  • Video falls back quietly to the codec the server picks (serverCodecChanged), while this PR throws in a similar case for G.711. I think it makes sense to match the current video behavior unless there's a strong reason you can think of to do it differently.
  • If a client-side preference turns out to be needed (e.g. a browser that ignores the codec order in the answer), please put the requested codec first and keep the others, rather than restricting to it alone. That keeps fallback working.

Did you try simulcastCodecs alone? If it didn't work in, can you share more context? Most if (g711Codec) / if (opts.audioCodec) branches should come down to normalizing the options once at the top of publishTrack (dtx = false, forceStereo = false, no maxBitrate), with everything after that just reading opts.

Comment on lines +898 to +903
private async publish(
track: LocalTrack,
opts: TrackPublishOptions,
isStereo: boolean,
g711Codec?: 'pcma' | 'pcmu',
) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Instead of g711Codec?: 'pcma' | 'pcmu', maybe it makes sense to pass through the audio codec param itself? I'm assuming right now the undefined effectively means opus which I think could be made more explicit with my suggestion. Since this method is private it should be ok to add new required params.

Comment thread src/room/track/options.ts Outdated
Comment on lines +447 to +451
export type AudioCodecOptions =
| ({ codec: 'opus' } & OpusParams)
| ({ codec: 'red' } & OpusParams)
| { codec: 'pcma' }
| { codec: 'pcmu' };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Consider breaking out each union item into its own type - ie, AudioCodecOptionsOpus, AudioCodecOptionsPcma, etc.

Comment on lines 1233 to 1244
try {
for (const transceiver of this.engine.pcManager.publisher.getTransceivers()) {
// if sender is not currently sending (after replaceTrack(null))
// removeTrack would have no effect.
// to ensure we end up successfully removing the track, manually set
// the transceiver to inactive
if (transceiver.sender === trackSender) {
transceiver.direction = 'inactive';
negotiationNeeded = true;
}
}
this.deactivateTransceiver(trackSender, this.engine.pcManager.publisher.getTransceivers());
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: you dropped negotiationNeeded = true in the refactored out deactivateTransceiver function, can you explain the rationale here?

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +776 to +780
const audioParams = isG711 ? { dtx: false, stereo: false } : selection;
if (audioParams) {
opts.red = codec === 'red';
opts.dtx = audioParams.dtx ?? opts.dtx;
opts.forceStereo = audioParams.stereo ?? opts.forceStereo;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Stereo capture overrides explicit mono publishing

When stereo input requests mono Opus or RED, publish still sends TF_STEREO from the capture settings. The SFU uses that flag to enable stereo in the Opus answer despite the mono request.

Learn more

Audio publishing separates capture settings from the requested encoding settings. The codec option sets forceStereo, which determines isStereo for the AddTrackRequest.stereo field. But the audio feature list independently adds TF_STEREO from the capture channel count. The LiveKit SFU's Opus answer configuration uses that feature to set the encoder's stereo parameter. getTrackFeatures also derives the feature from capture settings when sending a later audio feature update.

Example: Enable a microphone with channelCount: 2 and publish it with audioCodec: { codec: 'opus', stereo: false }. The request sends stereo: false and TF_STEREO, so the Opus answer still advertises stereo=1.

Recommended fix: For explicit codec selections, derive TF_STEREO from the same resolved isStereo value as AddTrackRequest.stereo. Preserve that resolved value for later audio feature updates. Add a test for two-channel input with stereo: false that checks both fields.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@huangyouyi

Copy link
Copy Markdown
Author

@1egoman Yes. simulcastCodecs alone worked for both PCMA and PCMU, as confirmed by the publisher and subscriber RTP stats.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support explicit audio codec selection before publishing an audio track

3 participants