blob: 7d8d843d317f280908cca33a184e2a32b155fc5b [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';
simon5c677962022-12-02 16:51:54 -050019import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
simone35acc22022-12-02 16:51:12 -050020import { 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
simon5c677962022-12-02 16:51:54 -050032export type CallData = {
simone35acc22022-12-02 16:51:12 -050033 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
simon5c677962022-12-02 16:51:54 -050046const defaultCallManagerContext: ICallManagerContext = {
47 callData: undefined,
48 callConversation: undefined,
49
50 startCall: () => {},
51 exitCall: () => {},
52};
53
54export const CallManagerContext = createContext<ICallManagerContext>(defaultCallManagerContext);
simone35acc22022-12-02 16:51:12 -050055CallManagerContext.displayName = 'CallManagerContext';
56
57export default ({ children }: WithChildren) => {
58 const [callData, setCallData] = useState<CallData>();
59 const webSocket = useContext(WebSocketContext);
60 const navigate = useNavigate();
simon5c677962022-12-02 16:51:54 -050061 const { conversation } = useConversationQuery(callData?.conversationId);
62 const { urlParams } = useUrlParams<ConversationRouteParams>();
simone35acc22022-12-02 16:51:12 -050063
64 const failStartCall = useCallback(() => {
65 throw new Error('Cannot start call: Already in a call');
66 }, []);
67
68 const startCall = !callData ? setCallData : failStartCall;
69
70 const exitCall = useCallback(() => {
71 if (!callData) {
72 return;
73 }
74
75 setCallData(undefined);
76 // TODO: write in chat that the call ended
77 }, [callData]);
78
79 useEffect(() => {
80 if (callData) {
81 // TODO: Currently, we simply do not bind the CallBegin listener if already in a call.
82 // In the future, we should handle receiving a call while already in another.
83 return;
84 }
85 if (!webSocket) {
86 return;
87 }
88
89 const callBeginListener = ({ conversationId, withVideoOn }: CallBegin) => {
90 startCall({ conversationId: conversationId, role: 'receiver', withVideoOn });
91 navigate(`/conversation/${conversationId}`);
92 };
93
94 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
95
96 return () => {
97 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
98 };
99 }, [webSocket, navigate, startCall, callData]);
100
simon5c677962022-12-02 16:51:54 -0500101 const value = useMemo(
102 () => ({
103 startCall,
104 callData,
105 callConversation: conversation,
106 exitCall,
107 }),
108 [startCall, callData, conversation, exitCall]
simone35acc22022-12-02 16:51:12 -0500109 );
simone35acc22022-12-02 16:51:12 -0500110
111 return (
simon5c677962022-12-02 16:51:54 -0500112 <CallManagerContext.Provider value={value}>
113 <WebRtcProvider>
114 <CallProvider>
115 {callData && callData.conversationId !== urlParams.conversationId && (
116 <RemoteVideoOverlay callConversationId={callData.conversationId} />
117 )}
118 {children}
119 </CallProvider>
120 </WebRtcProvider>
121 </CallManagerContext.Provider>
simone35acc22022-12-02 16:51:12 -0500122 );
123};