From 5078e709c0858f066e716cceb5a4a73b4ba18ad6 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Tue, 17 Nov 2026 09:55:48 +0100 Subject: [PATCH] fix: keep the last publisher transceiver of each kind After a stop of all media transceivers, some browsers give the next transceiver new header extension ids. The server keeps the first ids of the connection, and the browser rejects its answer. Co-Authored-By: Claude Fable 5.2 --- .changeset/keep-transceiver-per-kind.md | 5 +++++ src/room/PCTransport.e2e.test.ts | 18 ++++++++++++++++++ src/room/PCTransport.ts | 20 +++++++++++--------- 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 .changeset/keep-transceiver-per-kind.md diff --git a/.changeset/keep-transceiver-per-kind.md b/.changeset/keep-transceiver-per-kind.md new file mode 100644 index 0000000000..00ed010d16 --- /dev/null +++ b/.changeset/keep-transceiver-per-kind.md @@ -0,0 +1,5 @@ +--- +'livekit-client': patch +--- + +Keep the last audio transceiver and the last video transceiver of the publisher connection when you unpublish their tracks. After a stop of all transceivers, some browsers gave the next track new header extension ids, and the publish failed. diff --git a/src/room/PCTransport.e2e.test.ts b/src/room/PCTransport.e2e.test.ts index ee252863d7..d2c309edfc 100644 --- a/src/room/PCTransport.e2e.test.ts +++ b/src/room/PCTransport.e2e.test.ts @@ -63,6 +63,24 @@ describe('PCTransport.stopTransceivers', () => { expect(pc.localDescription!.sdp).not.toContain('m=video 0 '); }); + it('keeps the last transceiver of each kind', async () => { + publisher = new PCTransport(); + remote = new RTCPeerConnection(); + const video = publisher.addTransceiver(videoTrack(), { direction: 'sendonly' }); + const audio = publisher.addTransceiverOfKind('audio', { direction: 'sendonly' }); + const secondVideo = publisher.addTransceiver(videoTrack(), { direction: 'sendonly' }); + await negotiate(publisher, remote); + + publisher.removeTrack(video.sender, true); + publisher.removeTrack(audio.sender, true); + publisher.removeTrack(secondVideo.sender, true); + await negotiate(publisher, remote); + + expect(video.direction).toBe('stopped'); + expect(audio.direction).not.toBe('stopped'); + expect(secondVideo.direction).not.toBe('stopped'); + }); + it('returns false when no transceiver has the sender', async () => { publisher = new PCTransport(); remote = new RTCPeerConnection(); diff --git a/src/room/PCTransport.ts b/src/room/PCTransport.ts index 5537b5dcad..16896e4082 100644 --- a/src/room/PCTransport.ts +++ b/src/room/PCTransport.ts @@ -865,29 +865,31 @@ export default class PCTransport extends (EventEmitter as new () => TypedEmitter * connection closes. Call this after `removeTrack`, as `removeTrack(sender, true)` does. * Returns true if a transceiver stopped. * - * The last live m-section stays. The browser drops the ICE and DTLS transport of a connection - * when all its m-sections are rejected, and the next publish then needs a new transport, which - * the server cannot set up on the same connection. + * The last live transceiver of each kind stays. The server cannot follow two changes that some + * browsers make after all transceivers of a kind stop. The browser drops the ICE and DTLS + * transport when all m-sections are rejected. Some browsers also give the next transceiver new + * header extension ids. */ stopTransceivers(senders: RTCRtpSender[]): boolean { let stopped = false; const transceivers = this.getTransceivers(); - // a negotiated data channel section also keeps the transport - let liveSections = - transceivers.filter((t) => t.direction !== 'stopped' && t.currentDirection !== 'stopped') - .length + (this._pc?.sctp ? 1 : 0); + const isLive = (t: RTCRtpTransceiver) => + t.direction !== 'stopped' && t.currentDirection !== 'stopped'; for (const transceiver of transceivers) { // RTCRtpTransceiver.stop() is not available before Chrome 88 if (!senders.includes(transceiver.sender) || typeof transceiver.stop !== 'function') { continue; } - if (liveSections <= 1) { + // the sender has no track after removeTrack, the receiver always has one + const { kind } = transceiver.receiver.track; + if ( + !transceivers.some((t) => t !== transceiver && isLive(t) && t.receiver.track.kind === kind) + ) { continue; } try { transceiver.stop(); stopped = true; - liveSections -= 1; } catch (e) { this.log.warn('could not stop transceiver', { error: e }); }