From f508c7bf9996c503fd65ba485c1785ca437f5d0f Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Thu, 30 Mar 2023 14:27:25 -0700 Subject: [PATCH] stabilzing webrtc implementation --- app/mobile/src/api/removeCall.js | 2 +- app/mobile/src/context/useRingContext.hook.js | 94 +++++++++++-------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/app/mobile/src/api/removeCall.js b/app/mobile/src/api/removeCall.js index 602d1d59..78bbe14d 100644 --- a/app/mobile/src/api/removeCall.js +++ b/app/mobile/src/api/removeCall.js @@ -1,7 +1,7 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; export async function removeCall(server, token, callId) { - let call = await fetchWithTimeout(`https://${server}/talk/calls/${callId}?agent=${token}` + param, { method: 'DELETE' }); + let call = await fetchWithTimeout(`https://${server}/talk/calls/${callId}?agent=${token}`, { method: 'DELETE' }); checkResponse(call) } diff --git a/app/mobile/src/context/useRingContext.hook.js b/app/mobile/src/context/useRingContext.hook.js index bfd774d1..402b9e94 100644 --- a/app/mobile/src/context/useRingContext.hook.js +++ b/app/mobile/src/context/useRingContext.hook.js @@ -1,4 +1,5 @@ import { useEffect, useContext, useState, useRef } from 'react'; +import { Alert } from 'react-native'; import { createWebsocket } from 'api/fetchUtil'; import { addContactRing } from 'api/addContactRing'; import { addCall } from 'api/addCall'; @@ -46,6 +47,7 @@ export function useRingContext() { const candidates = useRef([]); const offers = useRef([]); const processing = useRef(false); + const connected = useRef(false); const iceServers = [ { @@ -133,7 +135,7 @@ export function useRingContext() { updateState({ ringing: ringing.current }); const impolite = async () => { - if (processing.current) { + if (processing.current || !connected.current) { return; } @@ -146,24 +148,29 @@ export function useRingContext() { const description = descriptions[i]; stream.current = null; - if (description == null) { - const offer = await pc.current.createOffer(constraints); - await pc.current.setLocalDescription(offer); - ws.current.send(JSON.stringify({ description: offer })); + try { + if (description == null) { + const offer = await pc.current.createOffer(constraints); + await pc.current.setLocalDescription(offer); + ws.current.send(JSON.stringify({ description: offer })); + } + else { + if (description.type === 'offer' && pc.current.signalingState !== 'stable') { + continue; + } + + const offer = new RTCSessionDescription(description); + await pc.current.setRemoteDescription(offer); + + if (description.type === 'offer') { + const answer = await pc.current.createAnswer(); + await pc.current.setLocalDescription(answer); + ws.current.send(JSON.stringify({ description: answer })); + } + } } - else { - if (description.type === 'offer' && pc.current.signalingState !== 'stable') { - continue; - } - - const offer = new RTCSessionDescription(description); - await pc.current.setRemoteDescription(offer); - - if (description.type === 'offer') { - const answer = await pc.current.createAnswer(); - await pc.current.setLocalDescription(answer); - ws.current.send(JSON.stringify({ description: answer })); - } + catch (err) { + Alert.alert('webrtc error', err.toString()); } } } @@ -172,6 +179,7 @@ export function useRingContext() { // connect signal socket candidates.current = []; + connected.current = false; calling.current = { state: "connecting", callId, contactNode, contactToken, host: false }; updateState({ callStatus: "connecting", cardId, remoteVideo: false, remoteAudio: false }); @@ -274,8 +282,7 @@ export function useRingContext() { calling.current.state = "connected" updateState({ callStatus: "connected" }); ws.current.send(JSON.stringify({ AppToken: calleeToken })) - - offers.current.push(null); + connected.current = true; impolite(); } ws.current.error = (e) => { @@ -317,7 +324,7 @@ export function useRingContext() { } const polite = async () => { - if (processing.current) { + if (processing.current || !connected.current) { return; } @@ -327,29 +334,34 @@ export function useRingContext() { descriptions = offers.current; offers.current = []; - for (let i = 0; i < descriptions.length; i++) { - const description = descriptions[i]; - stream.current = null; + try { + for (let i = 0; i < descriptions.length; i++) { + const description = descriptions[i]; + stream.current = null; - if (description == null) { - const offer = await pc.current.createOffer(constraints); - await pc.current.setLocalDescription(offer); - ws.current.send(JSON.stringify({ description: offer })); - } - else { - if (description.type === 'offer' && pc.current.signalingState !== 'stable') { - const rollback = new RTCSessionDescription({ type: "rollback" }); - await pc.current.setLocalDescription(rollback); + if (description == null) { + const offer = await pc.current.createOffer(constraints); + await pc.current.setLocalDescription(offer); + ws.current.send(JSON.stringify({ description: offer })); } - const offer = new RTCSessionDescription(description); - await pc.current.setRemoteDescription(offer); - if (description.type === 'offer') { - const answer = await pc.current.createAnswer(); - await pc.current.setLocalDescription(answer); - ws.current.send(JSON.stringify({ description: answer })); + else { + if (description.type === 'offer' && pc.current.signalingState !== 'stable') { + const rollback = new RTCSessionDescription({ type: "rollback" }); + await pc.current.setLocalDescription(rollback); + } + const offer = new RTCSessionDescription(description); + await pc.current.setRemoteDescription(offer); + if (description.type === 'offer') { + const answer = await pc.current.createAnswer(); + await pc.current.setLocalDescription(answer); + ws.current.send(JSON.stringify({ description: answer })); + } } } } + catch (err) { + Alert.alert('webrtc error', err.toString()); + } } processing.current = false; @@ -384,6 +396,8 @@ export function useRingContext() { } }, RING); + candidates.current = []; + connected.current = false; calling.current = { state: "connecting", callId: id, host: true }; updateState({ callStatus: "connecting", cardId, remoteVideo: false, remoteAudio: false }); @@ -456,6 +470,8 @@ export function useRingContext() { clearInterval(ringInterval); calling.current.state = 'connected'; updateState({ callStatus: "connected" }); + connected.current = true; + polite(); } if (signal.status === 'closed') { ws.current.close();