diff --git a/packages/js/src/Modules/Verto/webrtc/BaseCall.ts b/packages/js/src/Modules/Verto/webrtc/BaseCall.ts index 1c3abfe3..e0924114 100644 --- a/packages/js/src/Modules/Verto/webrtc/BaseCall.ts +++ b/packages/js/src/Modules/Verto/webrtc/BaseCall.ts @@ -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(); } /** @@ -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; @@ -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() { @@ -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 || diff --git a/packages/js/src/Modules/Verto/webrtc/Peer.ts b/packages/js/src/Modules/Verto/webrtc/Peer.ts index 81d73839..fecaa320 100644 --- a/packages/js/src/Modules/Verto/webrtc/Peer.ts +++ b/packages/js/src/Modules/Verto/webrtc/Peer.ts @@ -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, @@ -30,7 +30,6 @@ import { IVertoCallOptions } from './interfaces'; */ export default class Peer { public instance: RTCPeerConnection; - public iceGatheringComplete: DeferredPromise; public onSdpReadyTwice: Function = null; private _constraints: { offerToReceiveAudio: boolean; @@ -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; @@ -172,16 +170,6 @@ 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( @@ -189,10 +177,6 @@ export default class Peer { connectionState ); - if (connectionState === 'connected') { - return this.iceGatheringComplete.resolve(true); - } - if (connectionState === 'failed' || connectionState === 'disconnected') { const onConnectionOnline = () => { this.instance.restartIce(); @@ -220,7 +204,6 @@ export default class Peer { 'connectionstatechange', this.handleConnectionStateChange ); - this.instance.addEventListener('icecandidate', this.handleIceCandidate); this.instance.addEventListener( 'iceconnectionstatechange', this._handleIceConnectionStateChange @@ -483,7 +466,6 @@ export default class Peer { const { iceServers = [] } = this.options; const config: RTCConfiguration = { - iceCandidatePoolSize: 255, bundlePolicy: 'max-compat', iceServers, };