blob: da4a37419087a62eed0974ef2ab6fe81a1f27427 [file] [log] [blame]
Charlie2bc0d672022-11-04 11:53:44 -04001/*
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 */
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050018import { ContactMessage, WebSocketMessageType } from 'jami-web-common';
Charlie2bc0d672022-11-04 11:53:44 -040019import log from 'loglevel';
20import { Container } from 'typedi';
21
22import { Jamid } from '../jamid/jamid.js';
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050023import { WebSocketServer } from './websocket-server.js';
Charlie2bc0d672022-11-04 11:53:44 -040024
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050025const webRtcWebSocketMessageTypes = [
simonf929a362022-11-18 16:53:45 -050026 WebSocketMessageType.CallBegin,
27 WebSocketMessageType.CallAccept,
28 WebSocketMessageType.CallRefuse,
29 WebSocketMessageType.CallEnd,
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050030 WebSocketMessageType.WebRtcOffer,
31 WebSocketMessageType.WebRtcAnswer,
32 WebSocketMessageType.WebRtcIceCandidate,
simonf929a362022-11-18 16:53:45 -050033] as const;
34
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050035const jamid = Container.get(Jamid);
36const webSocketServer = Container.get(WebSocketServer);
simonf929a362022-11-18 16:53:45 -050037
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050038export function bindWebRtcCallbacks() {
39 for (const messageType of webRtcWebSocketMessageTypes) {
40 webSocketServer.bind(messageType, (accountId, data) => {
41 sendWebRtcData(messageType, accountId, data);
simonf929a362022-11-18 16:53:45 -050042 });
43 }
Charlie2bc0d672022-11-04 11:53:44 -040044}
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050045
46function sendWebRtcData(type: WebSocketMessageType, accountId: string, data: Partial<ContactMessage>) {
47 if (data.contactId === undefined) {
48 log.warn('Message is not a valid ContactMessage (missing contactId field)');
49 return;
50 }
51
52 jamid.sendAccountTextMessage(accountId, data.contactId, JSON.stringify({ type, data }));
53}