WIP: Adding checks for perfect negotiation webrtc

pull/1947/head
Warrick Corfe-Tan 4 years ago
parent a1601b039e
commit c777a27d5b

@ -120,6 +120,7 @@ export const CallContainer = () => {
// call method to end call connection // call method to end call connection
console.warn('ending the call'); console.warn('ending the call');
await CallManager.USER_rejectIncomingCallRequest(fakeCaller); await CallManager.USER_rejectIncomingCallRequest(fakeCaller);
setConnectionState(null);
}; };
const handleMouseDown = () => { const handleMouseDown = () => {
@ -127,6 +128,10 @@ export const CallContainer = () => {
}; };
//#endregion //#endregion
if (connectionState === null) {
return null;
}
return ( return (
<> <>
{connectionState === 'connecting' ? 'connecting...' : null} {connectionState === 'connecting' ? 'connecting...' : null}
@ -139,7 +144,6 @@ export const CallContainer = () => {
<VideoContainer /> <VideoContainer />
<CallWindowControls> <CallWindowControls>
<SessionButton text={'end call'} onClick={handleEndCall} /> <SessionButton text={'end call'} onClick={handleEndCall} />
<SessionButton text={'end call'} onClick={handleEndCall} />
</CallWindowControls> </CallWindowControls>
</CallWindowInner> </CallWindowInner>
</CallWindow> </CallWindow>

@ -30,6 +30,10 @@ let peerConnection: RTCPeerConnection | null;
const ENABLE_VIDEO = true; const ENABLE_VIDEO = true;
let makingOffer = false;
let ignoreOffer = false;
let isSettingRemoteAnswerPending = false;
const configuration = { const configuration = {
configuration: { configuration: {
offerToReceiveAudio: true, offerToReceiveAudio: true,
@ -187,8 +191,23 @@ export async function USER_acceptIncomingCallRequest(fromSender: string) {
console.warn('icecandidateerror:', event); console.warn('icecandidateerror:', event);
}); });
peerConnection.addEventListener('icecandidate', event => {
console.warn('icecandidateerror:', event);
// signaler.send({candidate}); // probably event.candidate
});
peerConnection.addEventListener('negotiationneeded', async event => { peerConnection.addEventListener('negotiationneeded', async event => {
console.warn('negotiationneeded:', event); console.warn('negotiationneeded:', event);
try {
makingOffer = true;
await peerConnection?.setLocalDescription();
// SignalService.CallMessage.Type.OFFER
// signaler.send({ description: pc.localDescription });
} catch (err) {
console.error(err);
} finally {
makingOffer = false;
}
}); });
peerConnection.addEventListener('signalingstatechange', event => { peerConnection.addEventListener('signalingstatechange', event => {
console.warn('signalingstatechange:', event); console.warn('signalingstatechange:', event);
@ -211,9 +230,13 @@ export async function USER_acceptIncomingCallRequest(fromSender: string) {
); );
return; return;
} }
try {
await peerConnection.setRemoteDescription( await peerConnection.setRemoteDescription(
new RTCSessionDescription({ sdp: sdps[0], type: 'offer' }) new RTCSessionDescription({ sdp: sdps[0], type: 'offer' })
); );
} catch (e) {
window.log?.error(`Error setting RTC Session Description ${e}`);
}
const answer = await peerConnection.createAnswer({ const answer = await peerConnection.createAnswer({
offerToReceiveAudio: true, offerToReceiveAudio: true,
@ -278,6 +301,29 @@ export async function handleOfferCallMessage(
sender: string, sender: string,
callMessage: SignalService.CallMessage callMessage: SignalService.CallMessage
) { ) {
try {
console.warn({ callMessage });
const readyForOffer =
!makingOffer && (peerConnection?.signalingState == 'stable' || isSettingRemoteAnswerPending);
// const offerCollision =
// callMessage.type === SignalService.CallMessage.Type.OFFER ||
// makingOffer ||
// peerConnection?.signalingState != 'stable';
// TODO: How should politeness be decided between client / recipient?
// ignoreOffer = !polite && offerCollision;
ignoreOffer = !true && !readyForOffer;
if (ignoreOffer) {
window.log?.warn('Received offer when unready for offer; Ignoring offer.');
return;
}
await peerConnection?.setLocalDescription();
// send via our signalling with the sdp of our pc.localDescription
} catch (err) {
window.log?.error(`Error handling offer message ${err}`);
}
if (!callCache.has(sender)) { if (!callCache.has(sender)) {
callCache.set(sender, new Array()); callCache.set(sender, new Array());
} }
@ -290,7 +336,7 @@ export async function handleCallAnsweredMessage(
callMessage: SignalService.CallMessage callMessage: SignalService.CallMessage
) { ) {
if (!callMessage.sdps || callMessage.sdps.length === 0) { if (!callMessage.sdps || callMessage.sdps.length === 0) {
window.log.warn('cannot handle answered message without sdps'); window.log.warn('cannot handle answered message without signal description protols');
return; return;
} }
if (!callCache.has(sender)) { if (!callCache.has(sender)) {
@ -301,7 +347,10 @@ export async function handleCallAnsweredMessage(
window.inboxStore?.dispatch(incomingCall({ sender })); window.inboxStore?.dispatch(incomingCall({ sender }));
const remoteDesc = new RTCSessionDescription({ type: 'answer', sdp: callMessage.sdps[0] }); const remoteDesc = new RTCSessionDescription({ type: 'answer', sdp: callMessage.sdps[0] });
if (peerConnection) { if (peerConnection) {
console.warn('Setting remote answer pending');
isSettingRemoteAnswerPending = true;
await peerConnection.setRemoteDescription(remoteDesc); await peerConnection.setRemoteDescription(remoteDesc);
isSettingRemoteAnswerPending = false;
} else { } else {
window.log.info('call answered by recipient but we do not have a peerconnection set'); window.log.info('call answered by recipient but we do not have a peerconnection set');
} }
@ -328,7 +377,12 @@ export async function handleIceCandidatesMessage(
const sdpMLineIndex = callMessage.sdpMLineIndexes[index]; const sdpMLineIndex = callMessage.sdpMLineIndexes[index];
const sdpMid = callMessage.sdpMids[index]; const sdpMid = callMessage.sdpMids[index];
const candicate = new RTCIceCandidate({ sdpMid, sdpMLineIndex, candidate: sdp }); const candicate = new RTCIceCandidate({ sdpMid, sdpMLineIndex, candidate: sdp });
try {
await peerConnection.addIceCandidate(candicate); await peerConnection.addIceCandidate(candicate);
} catch (err) {
if (!ignoreOffer) {
}
}
} }
} else { } else {
window.log.info('handleIceCandidatesMessage but we do not have a peerconnection set'); window.log.info('handleIceCandidatesMessage but we do not have a peerconnection set');

Loading…
Cancel
Save