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-last-publisher-transceiver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Keep the last transceiver of the publisher connection when you unpublish its track. A stop of the only transceiver made the browser drop the transport of the connection, and the next publish sent no media.
19 changes: 19 additions & 0 deletions src/room/PCTransport.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ describe('PCTransport.stopTransceivers', () => {
expect(kept.currentDirection).toBe('sendonly');
});

it('keeps the only transceiver, so the connection keeps its transport', async () => {
publisher = new PCTransport();
remote = new RTCPeerConnection();
const pc = (publisher as unknown as { pc: RTCPeerConnection }).pc;
const { sender } = publisher.addTransceiver(videoTrack(), { direction: 'sendonly' });
await negotiate(publisher, remote);
const transport = sender.transport;

publisher.removeTrack(sender, true);
await negotiate(publisher, remote);
publisher.addTransceiver(videoTrack(), { direction: 'sendonly' });
await negotiate(publisher, remote);

expect(transport).not.toBeNull();
expect(publisher.getTransceivers()).toHaveLength(2);
expect(publisher.getTransceivers()[1].sender.transport).toBe(transport);
expect(pc.localDescription!.sdp).not.toContain('m=video 0 ');
});

it('returns false when no transceiver has the sender', async () => {
publisher = new PCTransport();
remote = new RTCPeerConnection();
Expand Down
15 changes: 14 additions & 1 deletion src/room/PCTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,17 +864,30 @@ export default class PCTransport extends (EventEmitter as new () => TypedEmitter
* and each publish adds a new transceiver. A transceiver that is not stopped stays until the
* 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.
*/
stopTransceivers(senders: RTCRtpSender[]): boolean {
let stopped = false;
for (const transceiver of this.getTransceivers()) {
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);
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) {
continue;
}
try {
transceiver.stop();
stopped = true;
liveSections -= 1;
} catch (e) {
this.log.warn('could not stop transceiver', { error: e });
}
Expand Down
Loading