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

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 Opus and RED objects use the existing audio options, and omitting audioCodec keeps the existing negotiation.

Chromium's WebRTC logs confirm the first RTP payload after publishing, republishing, and full reconnect. Firefox passed the send and receive codec checks for the same stages. Publication failure cleanup, pnpm test, pnpm test:public, and pnpm build also passed.

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.

✅ 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
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?

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