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?
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 Opus and RED objects use the existing audio options, and omittingaudioCodeckeeps 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, andpnpm buildalso passed.Fixes #2281
Checklist