mirror of
https://github.com/balzack/databag.git
synced 2025-05-04 23:45:21 +00:00
serializing methods on peer in hopes to improve webrtc stability
This commit is contained in:
parent
ff3ad5d396
commit
a8a579dbda
@ -174,8 +174,8 @@ export function Calling({ callCard }: { callCard: string }) {
|
|||||||
{ state.calling && state.loaded && (
|
{ state.calling && state.loaded && (
|
||||||
<View style={{ ...styles.overlap, bottom: frameOffset }}>
|
<View style={{ ...styles.overlap, bottom: frameOffset }}>
|
||||||
<View style={{ paddingTop: 8, paddingBottom: 8, paddingLeft: 16, paddingRight: 16, gap: 16, display: 'flex', flexDirection: 'row', borderRadius: 16, backgroundColor: 'rgba(128,128,128,0.5)' }}>
|
<View style={{ paddingTop: 8, paddingBottom: 8, paddingLeft: 16, paddingRight: 16, gap: 16, display: 'flex', flexDirection: 'row', borderRadius: 16, backgroundColor: 'rgba(128,128,128,0.5)' }}>
|
||||||
<IconButton style={styles.closeIcon} iconColor="white" containerColor={Colors.primary} icon={state.audioEnabled ? 'microphone-off' : 'microphone'} loading={applyingAudio} disabled={!state.audio} compact="true" mode="contained" size={32} onPress={toggleAudio} />
|
<IconButton style={styles.closeIcon} iconColor="white" containerColor={Colors.primary} icon={state.audioEnabled ? 'microphone' : 'microphone-off'} loading={applyingAudio} disabled={!state.audio} compact="true" mode="contained" size={32} onPress={toggleAudio} />
|
||||||
<IconButton style={styles.closeIcon} iconColor="white" containerColor={Colors.primary} icon={state.videoEnabled ? 'video-off-outline' : 'video-outline'} loading={applyingVideo} disabled={!state.video} compact="true" mode="contained" size={32} onPress={toggleVideo} />
|
<IconButton style={styles.closeIcon} iconColor="white" containerColor={Colors.primary} icon={state.videoEnabled ? 'video-outline' : 'video-off-outline'} loading={applyingVideo} disabled={!state.video} compact="true" mode="contained" size={32} onPress={toggleVideo} />
|
||||||
<IconButton style={styles.closeIcon} iconColor="white" containerColor={Colors.danger} icon="phone-hangup-outline" compact="true" mode="contained" size={32} onPress={end} />
|
<IconButton style={styles.closeIcon} iconColor="white" containerColor={Colors.danger} icon="phone-hangup-outline" compact="true" mode="contained" size={32} onPress={end} />
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
|
@ -22,6 +22,9 @@ export function useCalling() {
|
|||||||
const call = useRef(null as { policy: string, peer: RTCPeerConnection, link: Link, candidates: RTCIceCandidate[] } | null);
|
const call = useRef(null as { policy: string, peer: RTCPeerConnection, link: Link, candidates: RTCIceCandidate[] } | null);
|
||||||
const localStream = useRef(null);
|
const localStream = useRef(null);
|
||||||
const remoteStream = useRef(null);
|
const remoteStream = useRef(null);
|
||||||
|
const updatingPeer = useRef(false);
|
||||||
|
const peerUpdate = useRef([]);
|
||||||
|
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
strings: {},
|
strings: {},
|
||||||
ringing: [],
|
ringing: [],
|
||||||
@ -82,7 +85,7 @@ export function useCalling() {
|
|||||||
const video = localStream.current.getTracks().find(track => track.kind === 'video');
|
const video = localStream.current.getTracks().find(track => track.kind === 'video');
|
||||||
if (audio) {
|
if (audio) {
|
||||||
audio.enabled = true;
|
audio.enabled = true;
|
||||||
peer.addTrack(audio, localStream.current);
|
await updatePeer('local_track', audio);
|
||||||
}
|
}
|
||||||
if (video) {
|
if (video) {
|
||||||
video.enabled = false;
|
video.enabled = false;
|
||||||
@ -93,13 +96,13 @@ export function useCalling() {
|
|||||||
updateState({ failed: true });
|
updateState({ failed: true });
|
||||||
}
|
}
|
||||||
} else if (status === 'closed') {
|
} else if (status === 'closed') {
|
||||||
|
call.current = null;
|
||||||
try {
|
try {
|
||||||
peer.close();
|
peer.close();
|
||||||
link.close();
|
link.close();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
call.current = null;
|
|
||||||
localStream.current = null;
|
localStream.current = null;
|
||||||
remoteStream.current = null,
|
remoteStream.current = null,
|
||||||
updateState({ calling: null, failed: false, audio: null, video: null, local: null, remote: null });
|
updateState({ calling: null, failed: false, audio: null, video: null, local: null, remote: null });
|
||||||
@ -108,16 +111,13 @@ export function useCalling() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const linkMessage = async (message: any) => {
|
const linkMessage = async (message: any) => {
|
||||||
console.log("LINK MSG", message);
|
|
||||||
if (call.current) {
|
if (call.current) {
|
||||||
const { peer, link, candidates, policy } = call.current;
|
const { peer, link, candidates, policy } = call.current;
|
||||||
try {
|
try {
|
||||||
if (message.description) {
|
if (message.description) {
|
||||||
const offer = new RTCSessionDescription(message.description);
|
const offer = new RTCSessionDescription(message.description);
|
||||||
await peer.setRemoteDescription(offer);
|
await peer.setRemoteDescription(offer);
|
||||||
console.log("SET REMOTE!!!");
|
|
||||||
if (message.description.type === 'offer') {
|
if (message.description.type === 'offer') {
|
||||||
console.log("RESPOND OFFER");
|
|
||||||
const description = await peer.createAnswer();
|
const description = await peer.createAnswer();
|
||||||
await peer.setLocalDescription(description);
|
await peer.setLocalDescription(description);
|
||||||
link.sendMessage({ description });
|
link.sendMessage({ description });
|
||||||
@ -144,7 +144,6 @@ console.log("RESPOND OFFER");
|
|||||||
}
|
}
|
||||||
|
|
||||||
const peerCandidate = async (candidate) => {
|
const peerCandidate = async (candidate) => {
|
||||||
console.log("PEER CANDIDATE");
|
|
||||||
if (call.current && candidate) {
|
if (call.current && candidate) {
|
||||||
const { link } = call.current;
|
const { link } = call.current;
|
||||||
await link.sendMessage({ candidate });
|
await link.sendMessage({ candidate });
|
||||||
@ -152,7 +151,6 @@ console.log("RESPOND OFFER");
|
|||||||
}
|
}
|
||||||
|
|
||||||
const peerNegotiate = async () => {
|
const peerNegotiate = async () => {
|
||||||
console.log("PEER NEGOTIATE");
|
|
||||||
if (call.current) {
|
if (call.current) {
|
||||||
try {
|
try {
|
||||||
const { peer, link } = call.current;
|
const { peer, link } = call.current;
|
||||||
@ -165,13 +163,47 @@ console.log("RESPOND OFFER");
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const peerTrack = async (track) => {
|
||||||
|
if (call.current) {
|
||||||
|
try {
|
||||||
|
const { peer } = call.current;
|
||||||
|
await peer.addTrack(track, localStream.current);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatePeer = async (type: string, data: any) => {
|
||||||
|
peerUpdate.current.push({ type, data });
|
||||||
|
|
||||||
|
if (!updatingPeer.current) {
|
||||||
|
updatingPeer.current = true;
|
||||||
|
while (peerUpdate.current.length > 0) {
|
||||||
|
const { type, data } = peerUpdate.current.shift();
|
||||||
|
if (type === 'negotiate') {
|
||||||
|
await peerNegotiate();
|
||||||
|
} else if (type === 'candidate') {
|
||||||
|
await peerCandidate(data);
|
||||||
|
} else if (type === 'message') {
|
||||||
|
await linkMessage(data);
|
||||||
|
} else if (type === 'remote_track') {
|
||||||
|
await remoteStream.current.addTrack(data, remoteStream.current);
|
||||||
|
} else if (type === 'local_track') {
|
||||||
|
await peerTrack(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updatingPeer.current = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const transmit = (ice: { urls: string; username: string; credential: string }[]) => {
|
const transmit = (ice: { urls: string; username: string; credential: string }[]) => {
|
||||||
const peerConnection = new RTCPeerConnection({ iceServers: ice });
|
const peerConnection = new RTCPeerConnection({ iceServers: ice });
|
||||||
peerConnection.addEventListener( 'connectionstatechange', event => {
|
peerConnection.addEventListener( 'connectionstatechange', event => {
|
||||||
console.log("CONNECTION STATE", event);
|
console.log("CONNECTION STATE", event);
|
||||||
});
|
});
|
||||||
peerConnection.addEventListener( 'icecandidate', event => {
|
peerConnection.addEventListener( 'icecandidate', event => {
|
||||||
peerCandidate(event.candidate);
|
updatePeer('candidate', event.candidate);
|
||||||
});
|
});
|
||||||
peerConnection.addEventListener( 'icecandidateerror', event => {
|
peerConnection.addEventListener( 'icecandidateerror', event => {
|
||||||
console.log("ICE ERROR");
|
console.log("ICE ERROR");
|
||||||
@ -180,13 +212,14 @@ console.log("RESPOND OFFER");
|
|||||||
console.log("ICE STATE CHANGE", event);
|
console.log("ICE STATE CHANGE", event);
|
||||||
});
|
});
|
||||||
peerConnection.addEventListener( 'negotiationneeded', event => {
|
peerConnection.addEventListener( 'negotiationneeded', event => {
|
||||||
peerNegotiate();
|
updatePeer('negotiate');
|
||||||
});
|
});
|
||||||
peerConnection.addEventListener( 'signalingstatechange', event => {
|
peerConnection.addEventListener( 'signalingstatechange', event => {
|
||||||
console.log("ICE SIGNALING", event);
|
console.log("ICE SIGNALING", event);
|
||||||
});
|
});
|
||||||
peerConnection.addEventListener( 'track', event => {
|
peerConnection.addEventListener( 'track', event => {
|
||||||
remoteStream.current.addTrack(event.track, remoteStream.current);
|
updatePeer('remote_track', event.track);
|
||||||
|
|
||||||
if (event.track.kind === 'video') {
|
if (event.track.kind === 'video') {
|
||||||
updateState({ remote: remoteStream.current });
|
updateState({ remote: remoteStream.current });
|
||||||
}
|
}
|
||||||
@ -243,7 +276,7 @@ console.log("RESPOND OFFER");
|
|||||||
const candidates = [];
|
const candidates = [];
|
||||||
call.current = { policy, peer, link, candidates };
|
call.current = { policy, peer, link, candidates };
|
||||||
link.setStatusListener(linkStatus);
|
link.setStatusListener(linkStatus);
|
||||||
link.setMessageListener(linkMessage);
|
link.setMessageListener((msg) => updatePeer('message', msg));
|
||||||
updateState({ calling: call.card });
|
updateState({ calling: call.card });
|
||||||
},
|
},
|
||||||
call: async (cardId: string) => {
|
call: async (cardId: string) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user