Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 1 | /* |
| 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-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 18 | import { ContactMessage, WebSocketMessageType } from 'jami-web-common'; |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 19 | import log from 'loglevel'; |
| 20 | import { Container } from 'typedi'; |
| 21 | |
| 22 | import { Jamid } from '../jamid/jamid.js'; |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 23 | import { WebSocketServer } from './websocket-server.js'; |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 24 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 25 | const webRtcWebSocketMessageTypes = [ |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 26 | WebSocketMessageType.CallBegin, |
| 27 | WebSocketMessageType.CallAccept, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 28 | WebSocketMessageType.CallEnd, |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 29 | WebSocketMessageType.WebRtcOffer, |
| 30 | WebSocketMessageType.WebRtcAnswer, |
| 31 | WebSocketMessageType.WebRtcIceCandidate, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 32 | ] as const; |
| 33 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 34 | const jamid = Container.get(Jamid); |
| 35 | const webSocketServer = Container.get(WebSocketServer); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 36 | |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 37 | export function bindWebRtcCallbacks() { |
| 38 | for (const messageType of webRtcWebSocketMessageTypes) { |
| 39 | webSocketServer.bind(messageType, (accountId, data) => { |
| 40 | sendWebRtcData(messageType, accountId, data); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 41 | }); |
| 42 | } |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 43 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 44 | |
| 45 | function 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 | } |