From ab30f2a1e8eaed496f149a7ade480b9db32712bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mai=20G=C3=A1bor?= Date: Tue, 7 Jan 2025 12:01:29 +0100 Subject: [PATCH] fix type errors --- src/MediaService.ts | 68 ++++++++++++++------------ src/middlewares/transportMiddleware.ts | 4 +- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/MediaService.ts b/src/MediaService.ts index 1510ca8..10bb295 100644 --- a/src/MediaService.ts +++ b/src/MediaService.ts @@ -1,7 +1,7 @@ import * as mediasoup from 'mediasoup'; import { Router } from 'mediasoup/node/lib/Router'; import { Consumer } from 'mediasoup/node/lib/Consumer'; -import { Transport } from 'mediasoup/node/lib/Transport'; +import { Transport, TransportListenInfo } from 'mediasoup/node/lib/Transport'; import { RtpHeaderExtension } from 'mediasoup/node/lib/RtpParameters'; import { Worker, @@ -90,8 +90,8 @@ export default class MediaService { } public closed = false; - public ip: string; - public ip6: string; + public ip: string | undefined; + public ip6: string | undefined; public announcedIp?: string; public announcedIp6?: string; public initialAvailableOutgoingBitrate: number; @@ -118,8 +118,10 @@ export default class MediaService { }: MediaServiceOptions) { logger.debug('constructor()'); - this.ip = ip; - this.ip6 = ip6; + if (ip) + this.ip = ip; + if (ip6) + this.ip6 = ip6; if (announcedIp && announcedIp !== ip) this.announcedIp = announcedIp; if (announcedIp6 && announcedIp6 !== ip6) this.announcedIp6 = announcedIp6; @@ -175,33 +177,39 @@ export default class MediaService { const worker = await mediasoup.createWorker(settings); const workerData = worker.appData as unknown as WorkerData; - let options = { + const options: { listenInfos: Array } = { listenInfos: [] }; - if (this.ip) - options.listenInfos.push({ - protocol: 'udp', - ip: this.ip, - announcedIp: this.announcedIp, - }, - { - protocol: 'tcp', - ip: this.ip, - announcedIp: this.announcedIp, - }); - }; - if (this.ip6) - options.listenInfos.push({ - protocol: 'udp', - ip: this.ip6, - announcedIp: this.announcedIp6, - }, - { - protocol: 'tcp', - ip: this.ip6, - announcedIp: this.announcedIp6, - }); - }; + + if (this.ip) { + options.listenInfos.push( + { + protocol: 'udp', + ip: this.ip, + announcedIp: this.announcedIp || undefined, // Ensure announcedIp is undefined if not provided + }, + { + protocol: 'tcp', + ip: this.ip, + announcedIp: this.announcedIp || undefined, + } + ); + } + + if (this.ip6) { + options.listenInfos.push( + { + protocol: 'udp', + ip: this.ip6, + announcedIp: this.announcedIp6 || undefined, + }, + { + protocol: 'tcp', + ip: this.ip6, + announcedIp: this.announcedIp6 || undefined, + } + ); + } const webRtcServer = await worker.createWebRtcServer(options); diff --git a/src/middlewares/transportMiddleware.ts b/src/middlewares/transportMiddleware.ts index 3a3e03d..40b6ea9 100644 --- a/src/middlewares/transportMiddleware.ts +++ b/src/middlewares/transportMiddleware.ts @@ -36,8 +36,8 @@ export const createTransportMiddleware = ({ const routerData = router.appData as unknown as RouterData; const transport = await router.createPipeTransport({ listenIp: { - ip: internal ? '127.0.0.1' : (mediaService.ip || mediaService.ip6), - announcedIp: internal ? undefined : (mediaService.ip || mediaService.ip6) + ip: internal ? '127.0.0.1' : (mediaService.ip || mediaService.ip6 || '127.0.0.1'), + announcedIp: internal ? undefined : (mediaService.announcedIp || mediaService.announcedIp6) }, enableSrtp: !internal, enableSctp: true,