blob: 5ef23004a338f56b8d33f043023acfebef2dfc82 [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,
simonf929a362022-11-18 16:53:45 -050028 WebSocketMessageType.CallEnd,
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050029 WebSocketMessageType.WebRtcOffer,
30 WebSocketMessageType.WebRtcAnswer,
31 WebSocketMessageType.WebRtcIceCandidate,
simonf929a362022-11-18 16:53:45 -050032] as const;
33
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050034const jamid = Container.get(Jamid);
35const webSocketServer = Container.get(WebSocketServer);
simonf929a362022-11-18 16:53:45 -050036
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050037export function bindWebRtcCallbacks() {
38 for (const messageType of webRtcWebSocketMessageTypes) {
39 webSocketServer.bind(messageType, (accountId, data) => {
40 sendWebRtcData(messageType, accountId, data);
simonf929a362022-11-18 16:53:45 -050041 });
42 }
Charlie2bc0d672022-11-04 11:53:44 -040043}
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050044
45function sendWebRtcData(type: WebSocketMessageType, accountId: string, data: Partial<ContactMessage>) {
46 if (data.contactId === undefined) {
47 log.warn('Message is not a valid ContactMessage (missing contactId field)');
48 return;
49 }
50
51 jamid.sendAccountTextMessage(accountId, data.contactId, JSON.stringify({ type, data }));
52}