simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 18 | import { CallAction, CallBegin, WebSocketMessageType } from 'jami-web-common'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 19 | import { useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 20 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 21 | import { createOptionalContext } from '../hooks/createOptionalContext'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 22 | import { ConversationMember } from '../models/conversation-member'; |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 23 | import { callTimeoutMs } from '../utils/constants'; |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 24 | import { AsyncSetState, SetState, WithChildren } from '../utils/utils'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 25 | import { CallData, CallManagerContext } from './CallManagerProvider'; |
| 26 | import ConditionalContextProvider from './ConditionalContextProvider'; |
| 27 | import { IWebRtcContext, MediaDevicesInfo, MediaInputKind, useWebRtcContext } from './WebRtcProvider'; |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 28 | import { IWebSocketContext, WebSocketContext } from './WebSocketProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 29 | |
| 30 | export type CallRole = 'caller' | 'receiver'; |
| 31 | |
| 32 | export enum CallStatus { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 33 | Default, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 34 | Loading, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 35 | Ringing, |
| 36 | Connecting, |
| 37 | InCall, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 38 | PermissionsDenied, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 39 | } |
| 40 | |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 41 | export enum VideoStatus { |
| 42 | Off, |
| 43 | Camera, |
| 44 | ScreenShare, |
| 45 | } |
| 46 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 47 | type MediaDeviceIdState = { |
| 48 | id: string | undefined; |
| 49 | setId: (id: string | undefined) => void | Promise<void>; |
| 50 | }; |
| 51 | type CurrentMediaDeviceIds = Record<MediaDeviceKind, MediaDeviceIdState>; |
| 52 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 53 | export interface ICallContext { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 54 | mediaDevices: MediaDevicesInfo; |
| 55 | currentMediaDeviceIds: CurrentMediaDeviceIds; |
| 56 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 57 | isAudioOn: boolean; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 58 | setIsAudioOn: SetState<boolean>; |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 59 | videoStatus: VideoStatus; |
| 60 | updateVideoStatus: AsyncSetState<VideoStatus>; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 61 | isChatShown: boolean; |
| 62 | setIsChatShown: SetState<boolean>; |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 63 | isFullscreen: boolean; |
| 64 | setIsFullscreen: SetState<boolean>; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 65 | callRole: CallRole; |
| 66 | callStatus: CallStatus; |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 67 | callStartTime: number | undefined; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 68 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 69 | acceptCall: (withVideoOn: boolean) => void; |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 70 | endCall: () => void; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 71 | } |
| 72 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 73 | const optionalCallContext = createOptionalContext<ICallContext>('CallContext'); |
| 74 | export const useCallContext = optionalCallContext.useOptionalContext; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 75 | |
| 76 | export default ({ children }: WithChildren) => { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 77 | const webSocket = useContext(WebSocketContext); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 78 | const { callMembers, callData, exitCall } = useContext(CallManagerContext); |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 79 | const webRtcContext = useWebRtcContext(true); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 80 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 81 | const dependencies = useMemo( |
| 82 | () => ({ |
| 83 | webSocket, |
| 84 | webRtcContext, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 85 | callMembers, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 86 | callData, |
| 87 | exitCall, |
| 88 | conversationId: callData?.conversationId, |
| 89 | }), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 90 | [webSocket, webRtcContext, callMembers, callData, exitCall] |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 91 | ); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 92 | |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 93 | return ( |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 94 | <ConditionalContextProvider |
| 95 | Context={optionalCallContext.Context} |
| 96 | initialValue={undefined} |
| 97 | dependencies={dependencies} |
| 98 | useProviderValue={CallProvider} |
| 99 | > |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 100 | {children} |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 101 | </ConditionalContextProvider> |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 102 | ); |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | const CallProvider = ({ |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 106 | webRtcContext, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 107 | callMembers, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 108 | callData, |
| 109 | exitCall, |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 110 | conversationId, |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 111 | webSocket, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 112 | }: { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 113 | webSocket: IWebSocketContext; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 114 | webRtcContext: IWebRtcContext; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 115 | callMembers: ConversationMember[]; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 116 | callData: CallData; |
| 117 | exitCall: () => void; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 118 | conversationId: string; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 119 | }): ICallContext => { |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 120 | const { |
| 121 | localStream, |
| 122 | updateScreenShare, |
| 123 | sendWebRtcOffer, |
| 124 | iceConnectionState, |
| 125 | closeConnection, |
| 126 | getMediaDevices, |
| 127 | updateLocalStream, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 128 | } = webRtcContext; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 129 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 130 | const [mediaDevices, setMediaDevices] = useState<MediaDevicesInfo>({ |
| 131 | audioinput: [], |
| 132 | audiooutput: [], |
| 133 | videoinput: [], |
| 134 | }); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 135 | const [audioInputDeviceId, setAudioInputDeviceId] = useState<string>(); |
| 136 | const [audioOutputDeviceId, setAudioOutputDeviceId] = useState<string>(); |
| 137 | const [videoDeviceId, setVideoDeviceId] = useState<string>(); |
| 138 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 139 | const [isAudioOn, setIsAudioOn] = useState(false); |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 140 | const [videoStatus, setVideoStatus] = useState(VideoStatus.Off); |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 141 | const [isChatShown, setIsChatShown] = useState(false); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 142 | const [isFullscreen, setIsFullscreen] = useState(false); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 143 | const [callStatus, setCallStatus] = useState(CallStatus.Default); |
| 144 | const [callRole] = useState(callData?.role); |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 145 | const [callStartTime, setCallStartTime] = useState<number | undefined>(undefined); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 146 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 147 | // TODO: This logic will have to change to support multiple people in a call. Could we move this logic to the server? |
| 148 | // The client could make a single request with the conversationId, and the server would be tasked with sending |
| 149 | // all the individual requests to the members of the conversation. |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 150 | const contactUri = useMemo(() => callMembers[0].contact.uri, [callMembers]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 151 | |
| 152 | useEffect(() => { |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 153 | if (callStatus !== CallStatus.InCall) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | const updateMediaDevices = async () => { |
| 158 | try { |
| 159 | const newMediaDevices = await getMediaDevices(); |
| 160 | |
| 161 | if (newMediaDevices.audiooutput.length !== 0 && !audioOutputDeviceId) { |
| 162 | setAudioOutputDeviceId(newMediaDevices.audiooutput[0].deviceId); |
| 163 | } |
| 164 | |
| 165 | setMediaDevices(newMediaDevices); |
| 166 | } catch (e) { |
| 167 | console.error('Could not update media devices:', e); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | navigator.mediaDevices.addEventListener('devicechange', updateMediaDevices); |
| 172 | updateMediaDevices(); |
| 173 | |
| 174 | return () => { |
| 175 | navigator.mediaDevices.removeEventListener('devicechange', updateMediaDevices); |
| 176 | }; |
| 177 | }, [callStatus, getMediaDevices, audioOutputDeviceId]); |
| 178 | |
| 179 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 180 | if (localStream) { |
| 181 | for (const track of localStream.getAudioTracks()) { |
| 182 | track.enabled = isAudioOn; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 183 | const deviceId = track.getSettings().deviceId; |
| 184 | if (deviceId) { |
| 185 | setAudioInputDeviceId(deviceId); |
| 186 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 187 | } |
simon | feaa1db | 2022-11-26 20:13:18 -0500 | [diff] [blame] | 188 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 189 | }, [isAudioOn, localStream]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 190 | |
| 191 | useEffect(() => { |
simon | f353ef4 | 2022-11-28 23:14:53 -0500 | [diff] [blame] | 192 | if (localStream) { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 193 | for (const track of localStream.getVideoTracks()) { |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 194 | track.enabled = videoStatus === VideoStatus.Camera; |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 195 | const deviceId = track.getSettings().deviceId; |
| 196 | if (deviceId) { |
| 197 | setVideoDeviceId(deviceId); |
| 198 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 199 | } |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 200 | } |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 201 | }, [videoStatus, localStream]); |
| 202 | |
| 203 | const updateVideoStatus = useCallback( |
| 204 | async (newStatus: ((prevState: VideoStatus) => VideoStatus) | VideoStatus) => { |
| 205 | if (typeof newStatus === 'function') { |
| 206 | newStatus = newStatus(videoStatus); |
| 207 | } |
| 208 | |
| 209 | const stream = await updateScreenShare(newStatus === VideoStatus.ScreenShare); |
| 210 | if (stream) { |
| 211 | for (const track of stream.getTracks()) { |
| 212 | track.addEventListener('ended', () => { |
| 213 | console.warn('Browser ended screen sharing'); |
| 214 | updateVideoStatus(VideoStatus.Off); |
| 215 | }); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | setVideoStatus(newStatus); |
| 220 | }, |
| 221 | [videoStatus, updateScreenShare] |
| 222 | ); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 223 | |
| 224 | useEffect(() => { |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 225 | const onFullscreenChange = () => { |
| 226 | setIsFullscreen(document.fullscreenElement !== null); |
| 227 | }; |
| 228 | |
| 229 | document.addEventListener('fullscreenchange', onFullscreenChange); |
| 230 | return () => { |
| 231 | document.removeEventListener('fullscreenchange', onFullscreenChange); |
| 232 | }; |
| 233 | }, []); |
| 234 | |
| 235 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 236 | if (callRole === 'caller' && callStatus === CallStatus.Default) { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 237 | const withVideoOn = callData?.withVideoOn ?? false; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 238 | setCallStatus(CallStatus.Loading); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 239 | updateLocalStream() |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 240 | .then(() => { |
| 241 | const callBegin: CallBegin = { |
| 242 | contactId: contactUri, |
| 243 | conversationId, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 244 | withVideoOn, |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | setCallStatus(CallStatus.Ringing); |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 248 | setVideoStatus(withVideoOn ? VideoStatus.Camera : VideoStatus.Off); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 249 | console.info('Sending CallBegin', callBegin); |
| 250 | webSocket.send(WebSocketMessageType.CallBegin, callBegin); |
| 251 | }) |
| 252 | .catch((e) => { |
| 253 | console.error(e); |
| 254 | setCallStatus(CallStatus.PermissionsDenied); |
| 255 | }); |
| 256 | } |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 257 | }, [webSocket, updateLocalStream, callRole, callStatus, contactUri, conversationId, callData]); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 258 | |
| 259 | const acceptCall = useCallback( |
| 260 | (withVideoOn: boolean) => { |
| 261 | setCallStatus(CallStatus.Loading); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 262 | updateLocalStream() |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 263 | .then(() => { |
| 264 | const callAccept: CallAction = { |
| 265 | contactId: contactUri, |
| 266 | conversationId, |
| 267 | }; |
| 268 | |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 269 | setVideoStatus(withVideoOn ? VideoStatus.Camera : VideoStatus.Off); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 270 | setCallStatus(CallStatus.Connecting); |
| 271 | console.info('Sending CallAccept', callAccept); |
| 272 | webSocket.send(WebSocketMessageType.CallAccept, callAccept); |
| 273 | }) |
| 274 | .catch((e) => { |
| 275 | console.error(e); |
| 276 | setCallStatus(CallStatus.PermissionsDenied); |
| 277 | }); |
| 278 | }, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 279 | [webSocket, updateLocalStream, contactUri, conversationId] |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 280 | ); |
| 281 | |
| 282 | useEffect(() => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 283 | if (callRole === 'caller' && callStatus === CallStatus.Ringing) { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 284 | const callAcceptListener = (data: CallAction) => { |
| 285 | console.info('Received event on CallAccept', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 286 | if (data.conversationId !== conversationId) { |
| 287 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 288 | return; |
| 289 | } |
| 290 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 291 | setCallStatus(CallStatus.Connecting); |
| 292 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 293 | sendWebRtcOffer(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | webSocket.bind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 297 | |
| 298 | return () => { |
| 299 | webSocket.unbind(WebSocketMessageType.CallAccept, callAcceptListener); |
| 300 | }; |
| 301 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 302 | }, [callRole, webSocket, sendWebRtcOffer, callStatus, conversationId]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 303 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 304 | const endCall = useCallback(() => { |
| 305 | const callEnd: CallAction = { |
| 306 | contactId: contactUri, |
| 307 | conversationId, |
| 308 | }; |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 309 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 310 | console.info('Sending CallEnd', callEnd); |
| 311 | closeConnection(); |
| 312 | webSocket.send(WebSocketMessageType.CallEnd, callEnd); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 313 | exitCall(); |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 314 | // TODO: write in chat that the call ended |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 315 | }, [webSocket, contactUri, conversationId, closeConnection, exitCall]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 316 | |
| 317 | useEffect(() => { |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 318 | const callEndListener = (data: CallAction) => { |
| 319 | console.info('Received event on CallEnd', data); |
Charlie | c18d640 | 2022-11-27 13:01:04 -0500 | [diff] [blame] | 320 | if (data.conversationId !== conversationId) { |
| 321 | console.warn('Wrong incoming conversationId, ignoring action'); |
| 322 | return; |
| 323 | } |
| 324 | |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 325 | closeConnection(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 326 | exitCall(); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 327 | // TODO: write in chat that the call ended |
| 328 | }; |
| 329 | |
| 330 | webSocket.bind(WebSocketMessageType.CallEnd, callEndListener); |
| 331 | return () => { |
| 332 | webSocket.unbind(WebSocketMessageType.CallEnd, callEndListener); |
| 333 | }; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 334 | }, [webSocket, exitCall, conversationId, closeConnection]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 335 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 336 | useEffect(() => { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 337 | if ( |
| 338 | callStatus === CallStatus.Connecting && |
| 339 | (iceConnectionState === 'connected' || iceConnectionState === 'completed') |
| 340 | ) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 341 | console.info('Changing call status to InCall'); |
| 342 | setCallStatus(CallStatus.InCall); |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 343 | setCallStartTime(Date.now()); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 344 | } |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 345 | }, [iceConnectionState, callStatus]); |
simon | accd802 | 2022-11-24 15:04:53 -0500 | [diff] [blame] | 346 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 347 | useEffect(() => { |
Charlie | 380dc5e | 2022-11-29 16:51:42 -0500 | [diff] [blame] | 348 | if (iceConnectionState === 'disconnected' || iceConnectionState === 'failed') { |
| 349 | console.info('ICE connection disconnected or failed, ending call'); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 350 | endCall(); |
| 351 | } |
simon | 1e2bf34 | 2022-12-02 12:19:40 -0500 | [diff] [blame] | 352 | }, [iceConnectionState, callStatus, videoStatus, endCall]); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 353 | |
| 354 | useEffect(() => { |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 355 | const checkStatusTimeout = () => { |
| 356 | if (callStatus !== CallStatus.InCall) { |
| 357 | endCall(); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 358 | } |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 359 | }; |
| 360 | const timeoutId = setTimeout(checkStatusTimeout, callTimeoutMs); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 361 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 362 | return () => { |
| 363 | clearTimeout(timeoutId); |
| 364 | }; |
| 365 | }, [callStatus, endCall]); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 366 | |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 367 | const currentMediaDeviceIds: CurrentMediaDeviceIds = useMemo(() => { |
| 368 | const createSetIdForDeviceKind = (mediaInputKind: MediaInputKind) => async (id: string | undefined) => { |
| 369 | const mediaDeviceIds = { |
| 370 | audio: audioInputDeviceId, |
| 371 | video: videoDeviceId, |
| 372 | }; |
| 373 | |
| 374 | mediaDeviceIds[mediaInputKind] = id; |
| 375 | |
| 376 | await updateLocalStream(mediaDeviceIds); |
| 377 | }; |
| 378 | |
| 379 | return { |
| 380 | audioinput: { |
| 381 | id: audioInputDeviceId, |
| 382 | setId: createSetIdForDeviceKind('audio'), |
| 383 | }, |
| 384 | audiooutput: { |
| 385 | id: audioOutputDeviceId, |
| 386 | setId: setAudioOutputDeviceId, |
| 387 | }, |
| 388 | videoinput: { |
| 389 | id: videoDeviceId, |
| 390 | setId: createSetIdForDeviceKind('video'), |
| 391 | }, |
| 392 | }; |
| 393 | }, [updateLocalStream, audioInputDeviceId, audioOutputDeviceId, videoDeviceId]); |
| 394 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 395 | return useMemo( |
| 396 | () => ({ |
| 397 | mediaDevices, |
| 398 | currentMediaDeviceIds, |
| 399 | isAudioOn, |
| 400 | setIsAudioOn, |
| 401 | videoStatus, |
| 402 | updateVideoStatus, |
| 403 | isChatShown, |
| 404 | setIsChatShown, |
| 405 | isFullscreen, |
| 406 | setIsFullscreen, |
| 407 | callRole, |
| 408 | callStatus, |
| 409 | callStartTime, |
| 410 | acceptCall, |
| 411 | endCall, |
| 412 | }), |
| 413 | [ |
| 414 | mediaDevices, |
| 415 | currentMediaDeviceIds, |
| 416 | isAudioOn, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 417 | videoStatus, |
| 418 | updateVideoStatus, |
| 419 | isChatShown, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 420 | isFullscreen, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 421 | callRole, |
| 422 | callStatus, |
| 423 | callStartTime, |
| 424 | acceptCall, |
| 425 | endCall, |
| 426 | ] |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 427 | ); |
| 428 | }; |