Skip to content

Commit

Permalink
Reuse transceivers in subscribe path (#51)
Browse files Browse the repository at this point in the history
* Reuse transceivers in subscribe path

LK-22 (https://linear.app/livekit/issue/LK-22/feature-pooled-transceivers-to-avoid-re-negotiation)

Do not stop remote tracks. That would change `readyState` of track
to `ended` and the corresponding transceiver cannot be reused.
Instead use the `enabled` attribute of track to enable/disable
remote tracks.

Bumping up the protocol version that informs the server that
client is capable of reusing transceivers.

Testing:
--------
Connect from Chrome. For remote side, connect/disconnect
from Firefox several times and ensure the following
- SDP does not grow (it has 4 sections as reported by
chrome://webrtc-internals (version, data, audio, video) each
time the remote side connects.
- Media flows from remote side on every connection.

Oddities:
---------
- Chrome callback `ontrack` sends a track. When the remote side
reconnects and the same transceiver is used, the callback does
not contain the new track id. It retains what was returned in the
first callback. Don't think it is an issue as we do not use that
track id anywhere.

* Apply suggestions from code review

Co-authored-by: David Zhao <[email protected]>

Co-authored-by: David Zhao <[email protected]>
  • Loading branch information
boks1971 and davidzhao authored Oct 14, 2021
1 parent f4c26e2 commit 6817386
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/room/participant/RemoteParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default class RemoteParticipant extends Participant {
} else {
track = new RemoteAudioTrack(mediaTrack, sid, receiver);
}
track.start();

publication.setTrack(track);
// set track name etc
Expand Down
10 changes: 10 additions & 0 deletions src/room/track/RemoteAudioTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ export default class RemoteAudioTrack extends Track {
this.emit(muted ? TrackEvent.Muted : TrackEvent.Unmuted, this);
}
}

start() {
// use `enabled` of track to enable re-use of transceiver
super.enable();
}

stop() {
// use `enabled` of track to enable re-use of transceiver
super.disable();
}
}
8 changes: 7 additions & 1 deletion src/room/track/RemoteVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ export default class RemoteVideoTrack extends Track {
});
}

start() {
// use `enabled` of track to enable re-use of transceiver
super.enable();
}

stop() {
super.stop();
// use `enabled` of track to enable re-use of transceiver
super.disable();
}
}
8 changes: 8 additions & 0 deletions src/room/track/Track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export class Track extends EventEmitter {
this.mediaStreamTrack.stop();
}

protected enable() {
this.mediaStreamTrack.enabled = true;
}

protected disable() {
this.mediaStreamTrack.enabled = false;
}

private recycleElement(element: HTMLMediaElement) {
if (element instanceof HTMLAudioElement) {
// we only need to re-use a single element
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const version = '0.12.2';
export const protocolVersion = 3;
export const protocolVersion = 4;

0 comments on commit 6817386

Please sign in to comment.