simon | e35acc2 | 2022-12-02 16:51:12 -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 | */ |
| 18 | import { CallBegin, WebSocketMessageType } from 'jami-web-common'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 19 | import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame^] | 20 | import { useTranslation } from 'react-i18next'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 21 | import { useNavigate } from 'react-router-dom'; |
| 22 | |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame^] | 23 | import { AlertSnackbar } from '../components/AlertSnackbar'; |
simon | daae910 | 2022-12-02 16:51:31 -0500 | [diff] [blame] | 24 | import { RemoteVideoOverlay } from '../components/VideoOverlay'; |
| 25 | import { useUrlParams } from '../hooks/useUrlParams'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 26 | import { Conversation } from '../models/conversation'; |
simon | daae910 | 2022-12-02 16:51:31 -0500 | [diff] [blame] | 27 | import { ConversationRouteParams } from '../router'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 28 | import { useConversationQuery } from '../services/conversationQueries'; |
| 29 | import { SetState, WithChildren } from '../utils/utils'; |
| 30 | import CallProvider, { CallRole } from './CallProvider'; |
| 31 | import WebRtcProvider from './WebRtcProvider'; |
| 32 | import { WebSocketContext } from './WebSocketProvider'; |
| 33 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 34 | export type CallData = { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 35 | conversationId: string; |
| 36 | role: CallRole; |
| 37 | withVideoOn?: boolean; |
| 38 | }; |
| 39 | |
| 40 | type ICallManagerContext = { |
| 41 | callData: CallData | undefined; |
| 42 | callConversation: Conversation | undefined; |
| 43 | |
| 44 | startCall: SetState<CallData | undefined>; |
| 45 | exitCall: () => void; |
| 46 | }; |
| 47 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 48 | const defaultCallManagerContext: ICallManagerContext = { |
| 49 | callData: undefined, |
| 50 | callConversation: undefined, |
| 51 | |
| 52 | startCall: () => {}, |
| 53 | exitCall: () => {}, |
| 54 | }; |
| 55 | |
| 56 | export const CallManagerContext = createContext<ICallManagerContext>(defaultCallManagerContext); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 57 | CallManagerContext.displayName = 'CallManagerContext'; |
| 58 | |
| 59 | export default ({ children }: WithChildren) => { |
| 60 | const [callData, setCallData] = useState<CallData>(); |
| 61 | const webSocket = useContext(WebSocketContext); |
| 62 | const navigate = useNavigate(); |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 63 | const { conversation } = useConversationQuery(callData?.conversationId); |
| 64 | const { urlParams } = useUrlParams<ConversationRouteParams>(); |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame^] | 65 | const [missedCallConversationId, setMissedCallConversationId] = useState<string>(); |
| 66 | const { t } = useTranslation(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 67 | |
| 68 | const failStartCall = useCallback(() => { |
| 69 | throw new Error('Cannot start call: Already in a call'); |
| 70 | }, []); |
| 71 | |
| 72 | const startCall = !callData ? setCallData : failStartCall; |
| 73 | |
| 74 | const exitCall = useCallback(() => { |
| 75 | if (!callData) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | setCallData(undefined); |
| 80 | // TODO: write in chat that the call ended |
| 81 | }, [callData]); |
| 82 | |
| 83 | useEffect(() => { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 84 | if (!webSocket) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | const callBeginListener = ({ conversationId, withVideoOn }: CallBegin) => { |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame^] | 89 | if (callData) { |
| 90 | // TODO: Currently, we display a notification if already in a call. |
| 91 | // In the future, we should handle receiving a call while already in another. |
| 92 | setMissedCallConversationId(conversationId); |
| 93 | return; |
| 94 | } |
| 95 | |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 96 | startCall({ conversationId: conversationId, role: 'receiver', withVideoOn }); |
| 97 | navigate(`/conversation/${conversationId}`); |
| 98 | }; |
| 99 | |
| 100 | webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener); |
| 101 | |
| 102 | return () => { |
| 103 | webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener); |
| 104 | }; |
| 105 | }, [webSocket, navigate, startCall, callData]); |
| 106 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 107 | const value = useMemo( |
| 108 | () => ({ |
| 109 | startCall, |
| 110 | callData, |
| 111 | callConversation: conversation, |
| 112 | exitCall, |
| 113 | }), |
| 114 | [startCall, callData, conversation, exitCall] |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 115 | ); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 116 | |
| 117 | return ( |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame^] | 118 | <> |
| 119 | <AlertSnackbar |
| 120 | severity={'info'} |
| 121 | open={missedCallConversationId !== undefined} |
| 122 | onClose={() => setMissedCallConversationId(undefined)} |
| 123 | > |
| 124 | {t('missed_incoming_call', { conversationId: missedCallConversationId })} |
| 125 | </AlertSnackbar> |
| 126 | <CallManagerContext.Provider value={value}> |
| 127 | <WebRtcProvider> |
| 128 | <CallProvider> |
| 129 | {callData && callData.conversationId !== urlParams.conversationId && ( |
| 130 | <RemoteVideoOverlay callConversationId={callData.conversationId} /> |
| 131 | )} |
| 132 | {children} |
| 133 | </CallProvider> |
| 134 | </WebRtcProvider> |
| 135 | </CallManagerContext.Provider> |
| 136 | </> |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 137 | ); |
| 138 | }; |