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 | */ |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 18 | import { WebSocketMessageTable, 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 | |
| 25 | const jamid = Container.get(Jamid); |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 26 | const webSocketServer = Container.get(WebSocketServer); |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 27 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 28 | const 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 | |
| 38 | type WebRTCWebSocketMessageType = typeof webRTCWebSocketMessageTypes[number]; |
| 39 | |
| 40 | function 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)'); |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 46 | return; |
| 47 | } |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 48 | 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 | ); |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | export function bindWebRTCCallbacks() { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 60 | for (const messageType of webRTCWebSocketMessageTypes) { |
| 61 | webSocketServer.bind(messageType, (data) => { |
| 62 | sendWebRTCData(messageType, data); |
| 63 | }); |
| 64 | } |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 65 | } |