blob: abef6e703943cc6781e10f72707d0b0748ccee91 [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 */
simonf929a362022-11-18 16:53:45 -050018import { WebSocketMessageTable, 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
25const jamid = Container.get(Jamid);
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050026const webSocketServer = Container.get(WebSocketServer);
Charlie2bc0d672022-11-04 11:53:44 -040027
simonf929a362022-11-18 16:53:45 -050028const webRTCWebSocketMessageTypes = [
29 WebSocketMessageType.IceCandidate,
30 WebSocketMessageType.WebRTCOffer,
31 WebSocketMessageType.WebRTCAnswer,
32 WebSocketMessageType.CallBegin,
33 WebSocketMessageType.CallAccept,
34 WebSocketMessageType.CallRefuse,
35 WebSocketMessageType.CallEnd,
36] as const;
37
38type WebRTCWebSocketMessageType = typeof webRTCWebSocketMessageTypes[number];
39
40function sendWebRTCData<T extends WebRTCWebSocketMessageType>(
41 type: WebRTCWebSocketMessageType,
42 data: Partial<WebSocketMessageTable[T]>
43) {
44 if (data.from === undefined || data.to === undefined) {
45 log.warn('Message is not a valid AccountTextMessage (missing from or to fields)');
Charlie2bc0d672022-11-04 11:53:44 -040046 return;
47 }
simonf929a362022-11-18 16:53:45 -050048 log.info('Handling WebRTC message of type:', type);
49 jamid.sendAccountTextMessage(
50 data.from,
51 data.to,
52 JSON.stringify({
53 type,
54 data,
55 })
56 );
Charlie2bc0d672022-11-04 11:53:44 -040057}
58
59export function bindWebRTCCallbacks() {
simonf929a362022-11-18 16:53:45 -050060 for (const messageType of webRTCWebSocketMessageTypes) {
61 webSocketServer.bind(messageType, (data) => {
62 sendWebRTCData(messageType, data);
63 });
64 }
Charlie2bc0d672022-11-04 11:53:44 -040065}