Repository navigation
feat: support PCMA and PCMU audio publishing - #2287
huangyouyi wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 2d7469a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
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 theaudio/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 wayvideoCodecworks today. - Besides adding extra complexity, because your solution today calls
setCodecPreferenceswith 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.AddTrackalready 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.
| private async publish( | ||
| track: LocalTrack, | ||
| opts: TrackPublishOptions, | ||
| isStereo: boolean, | ||
| g711Codec?: 'pcma' | 'pcmu', | ||
| ) { |
There was a problem hiding this comment.
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.
| export type AudioCodecOptions = | ||
| | ({ codec: 'opus' } & OpusParams) | ||
| | ({ codec: 'red' } & OpusParams) | ||
| | { codec: 'pcma' } | ||
| | { codec: 'pcmu' }; |
There was a problem hiding this comment.
nitpick: Consider breaking out each union item into its own type - ie, AudioCodecOptionsOpus, AudioCodecOptionsPcma, etc.
| 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 { |
There was a problem hiding this comment.
question: you dropped negotiationNeeded = true in the refactored out deactivateTransceiver function, can you explain the rationale here?
2d7469a to
a39e332
Compare
There was a problem hiding this comment.
Devin Review found 1 new potential issue.
2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
| 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; |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
|
@1egoman Yes. |
a39e332 to
bdcec9c
Compare
bdcec9c to
67b9d27
Compare
What and why
Apps that interoperate with G.711 endpoints can request
pcmaorpcmuwhen publishing browser audio.audioCodecaccepts a codec name or an object discriminated bycodec.The SDK signals the preference through
AddTrackRequest.simulcastCodecsand uses the existing publish and negotiation flow. The SFU handles codec fallback. OmittingaudioCodeckeeps default negotiation.Fixes #2281
Checklist