Skip to content

Commit

Permalink
Make peerconnection private on PCTransport (#903)
Browse files Browse the repository at this point in the history
* Make peerconnection private on PCTransport

* revert enablesimulcast for this branch

* Create moody-nails-flash.md

* fix connection check
  • Loading branch information
lukasIO authored Oct 26, 2023
1 parent 6bb1a9d commit 7631712
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-nails-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Make peerconnection private on PCTransport
2 changes: 1 addition & 1 deletion src/connectionHelper/checks/webrtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class WebRTCCheck extends Checker {
};

if (this.room.engine.subscriber) {
this.room.engine.subscriber.pc.onicecandidateerror = (ev) => {
this.room.engine.subscriber.onIceCandidateError = (ev) => {
if (ev instanceof RTCPeerConnectionIceErrorEvent) {
this.appendWarning(
`error with ICE candidate: ${ev.errorCode} ${ev.errorText} ${ev.url}`,
Expand Down
117 changes: 116 additions & 1 deletion src/room/PCTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const PCEvents = {
export default class PCTransport extends EventEmitter {
private _pc: RTCPeerConnection | null;

public get pc() {
private get pc() {
if (this._pc) return this._pc;
throw new UnexpectedConnectionState('Expected peer connection to be available');
}
Expand All @@ -51,12 +51,38 @@ export default class PCTransport extends EventEmitter {

onOffer?: (offer: RTCSessionDescriptionInit) => void;

onIceCandidate?: (candidate: RTCIceCandidate) => void;

onIceCandidateError?: (ev: Event) => void;

onConnectionStateChange?: (state: RTCPeerConnectionState) => void;

onDataChannel?: (ev: RTCDataChannelEvent) => void;

onTrack?: (ev: RTCTrackEvent) => void;

constructor(config?: RTCConfiguration, mediaConstraints: Record<string, unknown> = {}) {
super();
this._pc = isChromiumBased()
? // @ts-expect-error chrome allows additional media constraints to be passed into the RTCPeerConnection constructor
new RTCPeerConnection(config, mediaConstraints)
: new RTCPeerConnection(config);
this._pc.onicecandidate = (ev) => {
if (!ev.candidate) return;
this.onIceCandidate?.(ev.candidate);
};
this._pc.onicecandidateerror = (ev) => {
this.onIceCandidateError?.(ev);
};
this._pc.onconnectionstatechange = () => {
this.onConnectionStateChange?.(this._pc?.connectionState ?? 'closed');
};
this._pc.ondatachannel = (ev) => {
this.onDataChannel?.(ev);
};
this._pc.ontrack = (ev) => {
this.onTrack?.(ev);
};
}

get isICEConnected(): boolean {
Expand Down Expand Up @@ -270,10 +296,99 @@ export default class PCTransport extends EventEmitter {
return answer;
}

createDataChannel(label: string, dataChannelDict: RTCDataChannelInit) {
return this.pc.createDataChannel(label, dataChannelDict);
}

addTransceiver(mediaStreamTrack: MediaStreamTrack, transceiverInit: RTCRtpTransceiverInit) {
return this.pc.addTransceiver(mediaStreamTrack, transceiverInit);
}

addTrack(track: MediaStreamTrack) {
return this.pc.addTrack(track);
}

setTrackCodecBitrate(info: TrackBitrateInfo) {
this.trackBitrates.push(info);
}

setConfiguration(rtcConfig: RTCConfiguration) {
return this.pc.setConfiguration(rtcConfig);
}

canRemoveTrack(): boolean {
return !!this.pc.removeTrack;
}

removeTrack(sender: RTCRtpSender) {
return this.pc.removeTrack(sender);
}

getConnectionState() {
return this.pc.connectionState;
}

getICEConnectionState() {
return this.pc.iceConnectionState;
}

getSignallingState() {
return this.pc.signalingState;
}

getTransceivers() {
return this.pc.getTransceivers();
}

getSenders() {
return this.pc.getSenders();
}

getLocalDescription() {
return this.pc.localDescription;
}

getRemoteDescription() {
return this.pc.remoteDescription;
}

async getConnectedAddress(): Promise<string | undefined> {
if (!this._pc) {
return;
}
let selectedCandidatePairId = '';
const candidatePairs = new Map<string, RTCIceCandidatePairStats>();
// id -> candidate ip
const candidates = new Map<string, string>();
const stats: RTCStatsReport = await this._pc.getStats();
stats.forEach((v) => {
switch (v.type) {
case 'transport':
selectedCandidatePairId = v.selectedCandidatePairId;
break;
case 'candidate-pair':
if (selectedCandidatePairId === '' && v.selected) {
selectedCandidatePairId = v.id;
}
candidatePairs.set(v.id, v);
break;
case 'remote-candidate':
candidates.set(v.id, `${v.address}:${v.port}`);
break;
default:
}
});

if (selectedCandidatePairId === '') {
return undefined;
}
const selectedID = candidatePairs.get(selectedCandidatePairId)?.remoteCandidateId;
if (selectedID === undefined) {
return undefined;
}
return candidates.get(selectedID);
}

close() {
if (!this._pc) {
return;
Expand Down
Loading

0 comments on commit 7631712

Please sign in to comment.