fix a few race conditions connecting webrtc

This commit is contained in:
balzack 2025-02-06 22:37:49 -08:00
parent 1b73422ab0
commit d85f8dee1d
5 changed files with 11891 additions and 8389 deletions

View File

@ -2250,7 +2250,7 @@ SPEC CHECKSUMS:
SDWebImageWebPCoder: 908b83b6adda48effe7667cd2b7f78c897e5111d
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
TOCropViewController: 80b8985ad794298fb69d3341de183f33d1853654
Yoga: a9ef4f5c2cd79ad812110525ef61048be6a582a4
Yoga: b05994d1933f507b0a28ceaa4fdb968dc18da178
PODFILE CHECKSUM: 9cf7373afef7b881c911fda82ff1f94eacee3e98

View File

@ -75,14 +75,18 @@ export function useRingContext() {
const linkStatus = async (status: string) => {
if (call.current) {
const { peer, link } = call.current;
if (status === 'connected') {
const now = new Date();
const connectedTime = Math.floor(now.getTime() / 1000);
updateState({ connected: true, connectedTime });
await actions.enableAudio();
} else if (status === 'closed') {
await cleanup();
try {
const { peer, link } = call.current;
if (status === 'connected') {
const now = new Date();
const connectedTime = Math.floor(now.getTime() / 1000);
updateState({ connected: true, connectedTime });
await actions.enableAudio();
} else if (status === 'closed') {
await cleanup();
}
} catch (err) {
console.log(err);
}
}
}
@ -143,7 +147,7 @@ export function useRingContext() {
break;
case 'local_track':
if (passive.current) {
passiveTracks.push(data);
passiveTracks.current.push(data);
} else {
peer.addTrack(data, sourceStream.current);
}
@ -192,11 +196,11 @@ export function useRingContext() {
const peer = transmit(ice);
const candidates = [] as RTCIceCandidate[];
call.current = { peer, link, candidates };
link.setStatusListener(linkStatus);
link.setMessageListener((msg: any) => updatePeer('message', msg));
updateState({ calling: card, failed: false, connected: false, connectedTime: 0,
audioEnabled: false, videoEnabled: false, localVideo: false, remoteVideo: false,
localStream: localStream.current, remoteStream: remoteStream.current });
link.setStatusListener(linkStatus);
link.setMessageListener((msg: any) => updatePeer('message', msg));
}
const cleanup = async () => {
@ -323,8 +327,8 @@ export function useRingContext() {
}
},
enableAudio: async () => {
if (connecting.current || closing.current || !call.current) {
throw new Error('cannot unmute audio');
if (closing.current || !call.current) {
throw new Error('cannot unmute audio')
}
if (!localAudio.current) {
throw new Error('audio not available');
@ -347,7 +351,7 @@ export function useRingContext() {
updateState({ audioEnabled: false });
},
enableVideo: async () => {
if (connecting.current || closing.current || !call.current) {
if (closing.current || !call.current) {
throw new Error('cannot start video');
}
if (!localVideo.current) {

File diff suppressed because it is too large Load Diff

View File

@ -186,7 +186,7 @@ export function useRingContext() {
link.setMessageListener((msg: any) => updatePeer('message', msg));
updateState({ calling: card, failed: false, connected: false, connectedTime: 0,
audioEnabled: false, videoEnabled: false, localVideo: false, remoteVideo: false,
localStream: localStream.current, remoteStream: remoteStream.current });
localStream: localStream.current, remoteStream: remoteStream.current, fullscreen: false });
}
const cleanup = async () => {
@ -211,7 +211,8 @@ export function useRingContext() {
localStream.current = null;
remoteStream.current = null,
peerUpdate.current = [];
updateState({ calling: null, connected: false, connectedTime: 0, failed: false, localStream: null, remoteStream: null, localVideo: false, remoteVideo: false });
updateState({ calling: null, connected: false, connectedTime: 0, fullscreen: false,
failed: false, localStream: null, remoteStream: null, localVideo: false, remoteVideo: false });
closing.current = false;
}

View File

@ -177,14 +177,22 @@ export class LinkModule implements Link {
try {
this.status = status;
if (this.statusListener) {
await this.statusListener(this.connected && status === 'connected' ? 'reconnected' : status);
if (status === 'connected') {
if (this.connected) {
await this.statusListener('reconnected');
} else {
await this.statusListener('connected');
this.connected = true;
}
} else {
await this.statusListener(status);
}
}
} catch (err) {
this.log.error('status notification failed');
}
if (status === 'connected') {
this.connected = true;
if (this.ringInterval) {
clearInterval(this.ringInterval);
this.ringInterval = null;