blob: e6ebcbcf9e811df1a538c4ad6e96aa62f52433fc [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 */
Issam E. Maghni0432cb72022-11-12 06:09:26 +000018import { AccountTextMessage, 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
Issam E. Maghni0432cb72022-11-12 06:09:26 +000028function sendWebRTCData<T>(data: Partial<AccountTextMessage<T>>) {
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050029 if (data.from === undefined || data.to === undefined || data.message === undefined) {
30 log.warn('Message is not a valid AccountTextMessage (missing from, to, or message fields)');
Charlie2bc0d672022-11-04 11:53:44 -040031 return;
32 }
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050033
Issam E. Maghni0432cb72022-11-12 06:09:26 +000034 jamid.sendAccountTextMessage(data.from, data.to, JSON.stringify(data.message));
Charlie2bc0d672022-11-04 11:53:44 -040035}
36
37export function bindWebRTCCallbacks() {
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050038 webSocketServer.bind(WebSocketMessageType.WebRTCOffer, sendWebRTCData);
39 webSocketServer.bind(WebSocketMessageType.WebRTCAnswer, sendWebRTCData);
40 webSocketServer.bind(WebSocketMessageType.IceCandidate, sendWebRTCData);
Charlie2bc0d672022-11-04 11:53:44 -040041}