Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Calls API V2 #81

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions app/utils/rxjs/RxjsPeer.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,41 +169,12 @@ export class RxjsPeer {
async createSession(peerConnection: RTCPeerConnection) {
console.debug('🆕 creating new session')
const { apiBase } = this.config
// create an offer
const offer = await peerConnection.createOffer()
// Turn on Opus DTX to save bandwidth
offer.sdp = offer.sdp?.replace('useinbandfec=1', 'usedtx=1;useinbandfec=1')
// And set the offer as the local description
await peerConnection.setLocalDescription(offer)
const { sessionId, sessionDescription } =
await this.fetchWithRecordedHistory(`${apiBase}/sessions/new?SESSION`, {
const { sessionId } = await this.fetchWithRecordedHistory(
`${apiBase}/sessions/new?SESSION`,
{
method: 'POST',
body: JSON.stringify({
sessionDescription: offer,
}),
}).then((res) => res.json() as any)
const connected = new Promise((res, rej) => {
// timeout after 5s
setTimeout(rej, 5000)
const connectionStateChangeHandler = () => {
if (peerConnection.connectionState === 'connected') {
peerConnection.removeEventListener(
'connectionstatechange',
connectionStateChangeHandler
)
res(undefined)
}
}
peerConnection.addEventListener(
'connectionstatechange',
connectionStateChangeHandler
)
})

// Once both local and remote descriptions are set, the ICE process begins
await peerConnection.setRemoteDescription(sessionDescription)
// Wait until the peer connection's iceConnectionState is "connected"
await connected
).then((res) => res.json() as any)
return { peerConnection, sessionId }
}

Expand Down Expand Up @@ -276,6 +247,7 @@ export class RxjsPeer {
await peerConnection.setRemoteDescription(
new RTCSessionDescription(response.sessionDescription)
)
await connected(peerConnection)
}

return {
Expand Down Expand Up @@ -449,8 +421,11 @@ export class RxjsPeer {
}),
}
).then((res) => res.json() as Promise<RenegotiationResponse>)
if (renegotiationResponse.errorCode)
if (renegotiationResponse.errorCode) {
throw new Error(renegotiationResponse.errorDescription)
} else {
await connected(peerConnection)
}
}

return { trackMap }
Expand Down Expand Up @@ -583,3 +558,30 @@ async function resolveTrack(
peerConnection.addEventListener('track', handler)
})
}

async function connected(peerConnection: RTCPeerConnection) {
if (peerConnection.connectionState !== 'connected') {
const connected = new Promise((res, rej) => {
// timeout after 5s
setTimeout(rej, 5000)
const connectionStateChangeHandler = () => {
if (peerConnection.connectionState === 'connected') {
peerConnection.removeEventListener(
'connectionstatechange',
connectionStateChangeHandler
)
res(undefined)
}
}
peerConnection.addEventListener(
'connectionstatechange',
connectionStateChangeHandler,
{
once: true,
}
)
})

await connected
}
}