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