blob: 9bde1867890dd1fc0031db3d240efbaf0d66c0ab [file] [log] [blame]
simone35acc22022-12-02 16:51:12 -05001/*
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 */
18import { CallBegin, WebSocketMessageType } from 'jami-web-common';
19import { createContext, useCallback, useContext, useEffect, useState } from 'react';
20import { useNavigate } from 'react-router-dom';
21
simondaae9102022-12-02 16:51:31 -050022import { RemoteVideoOverlay } from '../components/VideoOverlay';
23import { useUrlParams } from '../hooks/useUrlParams';
simone35acc22022-12-02 16:51:12 -050024import { Conversation } from '../models/conversation';
simondaae9102022-12-02 16:51:31 -050025import { ConversationRouteParams } from '../router';
simone35acc22022-12-02 16:51:12 -050026import { useConversationQuery } from '../services/conversationQueries';
27import { SetState, WithChildren } from '../utils/utils';
28import CallProvider, { CallRole } from './CallProvider';
29import WebRtcProvider from './WebRtcProvider';
30import { WebSocketContext } from './WebSocketProvider';
31
32type CallData = {
33 conversationId: string;
34 role: CallRole;
35 withVideoOn?: boolean;
36};
37
38type ICallManagerContext = {
39 callData: CallData | undefined;
40 callConversation: Conversation | undefined;
41
42 startCall: SetState<CallData | undefined>;
43 exitCall: () => void;
44};
45
46export const CallManagerContext = createContext<ICallManagerContext>(undefined!);
47CallManagerContext.displayName = 'CallManagerContext';
48
49export 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
107const CallManagerProvider = ({ children }: WithChildren) => {
108 const { callData } = useContext(CallManagerContext);
simondaae9102022-12-02 16:51:31 -0500109 const { urlParams } = useUrlParams<ConversationRouteParams>();
110 const conversationId = urlParams.conversationId;
simone35acc22022-12-02 16:51:12 -0500111
112 if (!callData) {
113 return <>{children}</>;
114 }
115
116 return (
117 <WebRtcProvider>
simondaae9102022-12-02 16:51:31 -0500118 <CallProvider>
119 {callData.conversationId !== conversationId && (
120 <RemoteVideoOverlay callConversationId={callData.conversationId} />
121 )}
122 {children}
123 </CallProvider>
simone35acc22022-12-02 16:51:12 -0500124 </WebRtcProvider>
125 );
126};