End call when connection is lost
- Automatically end call when WebRTC connection is lost
- Remove state when in call
GitLab: 182
Change-Id: I259eccda82db7304542bef863c429c9313b4145a
diff --git a/client/src/contexts/CallProvider.tsx b/client/src/contexts/CallProvider.tsx
index e99399d..f54b75f 100644
--- a/client/src/contexts/CallProvider.tsx
+++ b/client/src/contexts/CallProvider.tsx
@@ -110,11 +110,8 @@
webSocket: IWebSocketContext;
webRtcConnection: RTCPeerConnection;
}) => {
- const {
- queryParams: { role: callRole },
- state: routeState,
- } = useUrlParams<CallRouteParams>();
- const { remoteStreams, sendWebRtcOffer, isConnected } = useContext(WebRtcContext);
+ const { state: routeState } = useUrlParams<CallRouteParams>();
+ const { remoteStreams, sendWebRtcOffer, iceConnectionState } = useContext(WebRtcContext);
const { conversationId, conversation } = useContext(ConversationContext);
const navigate = useNavigate();
@@ -128,6 +125,7 @@
const [isChatShown, setIsChatShown] = useState(false);
const [isFullscreen, setIsFullscreen] = useState(false);
const [callStatus, setCallStatus] = useState(routeState?.callStatus);
+ const [callRole] = useState(routeState?.role);
const [callStartTime, setCallStartTime] = useState<Date | undefined>(undefined);
// TODO: This logic will have to change to support multiple people in a call. Could we move this logic to the server?
@@ -298,13 +296,16 @@
}, [webSocket, navigate, conversationId, quitCall]);
useEffect(() => {
- if (callStatus === CallStatus.Connecting && isConnected) {
+ if (
+ callStatus === CallStatus.Connecting &&
+ (iceConnectionState === 'connected' || iceConnectionState === 'completed')
+ ) {
console.info('Changing call status to InCall');
setCallStatus(CallStatus.InCall);
setVideoStatus(isVideoOn);
setCallStartTime(new Date());
}
- }, [isConnected, callStatus, setVideoStatus, isVideoOn]);
+ }, [iceConnectionState, callStatus, setVideoStatus, isVideoOn]);
const acceptCall = useCallback(
(withVideoOn: boolean) => {
@@ -334,6 +335,13 @@
}, [webSocket, contactUri, conversationId, quitCall]);
useEffect(() => {
+ if (iceConnectionState === 'disconnected') {
+ console.info('ICE connection disconnected');
+ endCall();
+ }
+ }, [iceConnectionState, callStatus, setVideoStatus, isVideoOn, endCall]);
+
+ useEffect(() => {
const checkStatusTimeout = () => {
if (callStatus !== CallStatus.InCall) {
endCall();
@@ -346,6 +354,13 @@
};
}, [callStatus, endCall]);
+ useEffect(() => {
+ navigate('.', {
+ replace: true,
+ state: {},
+ });
+ }, [navigate]);
+
if (!callRole || callStatus === undefined) {
console.error('Invalid route. Redirecting...');
return <Navigate to={'/'} />;
diff --git a/client/src/contexts/WebRtcProvider.tsx b/client/src/contexts/WebRtcProvider.tsx
index 397bd83..63e2ca2 100644
--- a/client/src/contexts/WebRtcProvider.tsx
+++ b/client/src/contexts/WebRtcProvider.tsx
@@ -26,7 +26,7 @@
import { IWebSocketContext, WebSocketContext } from './WebSocketProvider';
interface IWebRtcContext {
- isConnected: boolean;
+ iceConnectionState: RTCIceConnectionState | undefined;
remoteStreams: readonly MediaStream[] | undefined;
webRtcConnection: RTCPeerConnection | undefined;
@@ -35,7 +35,7 @@
}
const defaultWebRtcContext: IWebRtcContext = {
- isConnected: false,
+ iceConnectionState: undefined,
remoteStreams: undefined,
webRtcConnection: undefined,
sendWebRtcOffer: async () => {},
@@ -91,7 +91,7 @@
}) => {
const { conversation, conversationId } = useContext(ConversationContext);
const [remoteStreams, setRemoteStreams] = useState<readonly MediaStream[]>();
- const [isConnected, setIsConnected] = useState(false);
+ const [iceConnectionState, setIceConnectionState] = useState<RTCIceConnectionState | undefined>();
// TODO: This logic will have to change to support multiple people in a call
const contactUri = useMemo(() => conversation.getFirstMember().contact.getUri(), [conversation]);
@@ -208,11 +208,9 @@
setRemoteStreams(event.streams);
};
- const iceConnectionStateChangeEventListener = (event: Event) => {
- console.info(`Received WebRTC event on iceconnectionstatechange: ${webRtcConnection.iceConnectionState}`, event);
- setIsConnected(
- webRtcConnection.iceConnectionState === 'connected' || webRtcConnection.iceConnectionState === 'completed'
- );
+ const iceConnectionStateChangeEventListener = () => {
+ console.info('ICE connection state changed:', webRtcConnection.iceConnectionState);
+ setIceConnectionState(webRtcConnection.iceConnectionState);
};
webRtcConnection.addEventListener('track', trackEventListener);
@@ -227,7 +225,7 @@
return (
<WebRtcContext.Provider
value={{
- isConnected,
+ iceConnectionState,
remoteStreams,
webRtcConnection,
sendWebRtcOffer,