diff --git a/app/utils/rxjs/RxjsPeer.client.ts b/app/utils/rxjs/RxjsPeer.client.ts index f7b86d7f..b03e4b40 100644 --- a/app/utils/rxjs/RxjsPeer.client.ts +++ b/app/utils/rxjs/RxjsPeer.client.ts @@ -92,6 +92,7 @@ export class RxjsPeer { // for the best like we do here for now) const timeoutSeconds = 7 iceTimeout = window.setTimeout(() => { + if (peerConnection.iceConnectionState === 'connected') return console.debug( `💥 Peer iceConnectionState was ${peerConnection.iceConnectionState} for more than ${timeoutSeconds} seconds` ) @@ -247,7 +248,7 @@ export class RxjsPeer { await peerConnection.setRemoteDescription( new RTCSessionDescription(response.sessionDescription) ) - await connected(peerConnection) + await peerConnectionIsConnected(peerConnection) } return { @@ -279,7 +280,7 @@ export class RxjsPeer { }) }) } - }) + }).pipe(retry(2)) } pushTrack( @@ -424,7 +425,7 @@ export class RxjsPeer { if (renegotiationResponse.errorCode) { throw new Error(renegotiationResponse.errorDescription) } else { - await connected(peerConnection) + await peerConnectionIsConnected(peerConnection) } } @@ -559,26 +560,30 @@ async function resolveTrack( }) } -async function connected(peerConnection: RTCPeerConnection) { +async function peerConnectionIsConnected(peerConnection: RTCPeerConnection) { if (peerConnection.connectionState !== 'connected') { const connected = new Promise((res, rej) => { // timeout after 5s - setTimeout(rej, 5000) + const timeout = setTimeout(() => { + peerConnection.removeEventListener( + 'connectionstatechange', + connectionStateChangeHandler + ) + rej() + }, 5000) const connectionStateChangeHandler = () => { if (peerConnection.connectionState === 'connected') { peerConnection.removeEventListener( 'connectionstatechange', connectionStateChangeHandler ) + clearTimeout(timeout) res(undefined) } } peerConnection.addEventListener( 'connectionstatechange', - connectionStateChangeHandler, - { - once: true, - } + connectionStateChangeHandler ) })