Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/keep-transceiver-per-kind.md
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions src/room/PCTransport.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 11 additions & 9 deletions src/room/PCTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
Loading