Skip to content

Commit

Permalink
fix: revert ice gathering process changes (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
farhat-ha authored Nov 1, 2024
1 parent 4b1c497 commit 2d9fbf4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
34 changes: 28 additions & 6 deletions packages/js/src/Modules/Verto/webrtc/BaseCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@ export default abstract class BaseCall implements IWebRTCCall {
return `conference-member.${this.id}`;
}

async invite() {
invite() {
this.direction = Direction.Outbound;
this.peer = new Peer(PeerType.Offer, this.options, this.session);
await this.peer.iceGatheringComplete.promise;
this._onIceSdp(this.peer.instance.localDescription);
this._registerPeerEvents();
}

/**
Expand All @@ -263,7 +262,7 @@ export default abstract class BaseCall implements IWebRTCCall {
* call.answer()
* ```
*/
async answer(params: AnswerParams = {}) {
answer(params: AnswerParams = {}) {
this.stopRingtone();

this.direction = Direction.Inbound;
Expand All @@ -279,8 +278,7 @@ export default abstract class BaseCall implements IWebRTCCall {
}

this.peer = new Peer(PeerType.Answer, this.options, this.session);
await this.peer.iceGatheringComplete.promise;
this._onIceSdp(this.peer.instance.localDescription);
this._registerPeerEvents();
}

playRingtone() {
Expand Down Expand Up @@ -1436,6 +1434,30 @@ export default abstract class BaseCall implements IWebRTCCall {
}
}

private _registerPeerEvents() {
const { instance } = this.peer;
this._iceDone = false;
instance.onicecandidate = (event) => {
if (this._iceDone) {
return;
}
this._onIce(event);
};

//@ts-ignore
instance.addEventListener('addstream', (event: MediaStreamEvent) => {
this.options.remoteStream = event.stream;
});

instance.addEventListener('track', (event: RTCTrackEvent) => {
this.options.remoteStream = event.streams[0];
const { remoteElement, remoteStream, screenShare } = this.options;
if (screenShare === false) {
attachMediaStream(remoteElement, remoteStream);
}
});
}

private _checkConferenceSerno = (serno: number) => {
const check =
serno < 0 ||
Expand Down
20 changes: 1 addition & 19 deletions packages/js/src/Modules/Verto/webrtc/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
saveToFile,
webRTCStatsReporter,
} from '../util/debug';
import { DeferredPromise, deferredPromise, isFunction } from '../util/helpers';
import { isFunction } from '../util/helpers';
import logger from '../util/logger';
import {
RTCPeerConnection,
Expand All @@ -30,7 +30,6 @@ import { IVertoCallOptions } from './interfaces';
*/
export default class Peer {
public instance: RTCPeerConnection;
public iceGatheringComplete: DeferredPromise<boolean>;
public onSdpReadyTwice: Function = null;
private _constraints: {
offerToReceiveAudio: boolean;
Expand Down Expand Up @@ -60,7 +59,6 @@ export default class Peer {
this.handleNegotiationNeededEvent.bind(this);
this.handleTrackEvent = this.handleTrackEvent.bind(this);
this.createPeerConnection = this.createPeerConnection.bind(this);
this.iceGatheringComplete = deferredPromise({ debounceTime: 100 });

this._session = session;

Expand Down Expand Up @@ -172,27 +170,13 @@ export default class Peer {
}
}

private handleIceCandidate = (event: RTCPeerConnectionIceEvent) => {
if (
event.candidate &&
['relay', 'srflx', 'prflx'].includes(event.candidate.type)
) {
// Found enough candidates to establish a connection
// This is a workaround for the issue where iceGatheringState is always 'gathering'
this.iceGatheringComplete.resolve(true);
}
};
private handleConnectionStateChange = async (event: Event) => {
const { connectionState } = this.instance;
console.log(
`[${new Date().toISOString()}] Connection State`,
connectionState
);

if (connectionState === 'connected') {
return this.iceGatheringComplete.resolve(true);
}

if (connectionState === 'failed' || connectionState === 'disconnected') {
const onConnectionOnline = () => {
this.instance.restartIce();
Expand Down Expand Up @@ -220,7 +204,6 @@ export default class Peer {
'connectionstatechange',
this.handleConnectionStateChange
);
this.instance.addEventListener('icecandidate', this.handleIceCandidate);
this.instance.addEventListener(
'iceconnectionstatechange',
this._handleIceConnectionStateChange
Expand Down Expand Up @@ -483,7 +466,6 @@ export default class Peer {
const { iceServers = [] } = this.options;

const config: RTCConfiguration = {
iceCandidatePoolSize: 255,
bundlePolicy: 'max-compat',
iceServers,
};
Expand Down

0 comments on commit 2d9fbf4

Please sign in to comment.