Skip to content

Commit

Permalink
Merge pull request #83 from cloudflare/fix-calls-api-v2-logic
Browse files Browse the repository at this point in the history
Fix calls api v2 logic
  • Loading branch information
third774 authored Aug 14, 2024
2 parents 5d82baa + 5d6ccc2 commit daa921d
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions app/utils/rxjs/RxjsPeer.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
Expand Down Expand Up @@ -247,7 +248,7 @@ export class RxjsPeer {
await peerConnection.setRemoteDescription(
new RTCSessionDescription(response.sessionDescription)
)
await connected(peerConnection)
await peerConnectionIsConnected(peerConnection)
}

return {
Expand Down Expand Up @@ -279,7 +280,7 @@ export class RxjsPeer {
})
})
}
})
}).pipe(retry(2))
}

pushTrack(
Expand Down Expand Up @@ -424,7 +425,7 @@ export class RxjsPeer {
if (renegotiationResponse.errorCode) {
throw new Error(renegotiationResponse.errorDescription)
} else {
await connected(peerConnection)
await peerConnectionIsConnected(peerConnection)
}
}

Expand Down Expand Up @@ -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
)
})

Expand Down

0 comments on commit daa921d

Please sign in to comment.