Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat extra listenips #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"optionalDependencies": {
"bufferutil": "^4.0.7",
"utf-8-validate": "^6.0.3"
}
},
"resolutions": {
"strip-ansi": "6.0.1"
}
}
64 changes: 49 additions & 15 deletions src/MediaService.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -62,8 +62,10 @@ interface MetricsData {
}

export interface MediaServiceOptions {
ip: string;
ip?: string;
ip6?: string;
announcedIp?: string;
announcedIp6?: string;
initialAvailableOutgoingBitrate: number;
maxIncomingBitrate: number;
maxOutgoingBitrate: number;
Expand All @@ -88,8 +90,10 @@ export default class MediaService {
}

public closed = false;
public ip: string;
public ip: string | undefined;
public ip6: string | undefined;
public announcedIp?: string;
public announcedIp6?: string;
public initialAvailableOutgoingBitrate: number;
public maxIncomingBitrate: number;
public maxOutgoingBitrate: number;
Expand All @@ -101,7 +105,9 @@ export default class MediaService {

constructor({
ip,
ip6,
announcedIp,
announcedIp6,
initialAvailableOutgoingBitrate,
maxIncomingBitrate,
maxOutgoingBitrate,
Expand All @@ -112,9 +118,13 @@ export default class MediaService {
}: MediaServiceOptions) {
logger.debug('constructor()');

this.ip = ip;
if (ip)
this.ip = ip;
if (ip6)
this.ip6 = ip6;

if (announcedIp && announcedIp !== ip) this.announcedIp = announcedIp;
if (announcedIp6 && announcedIp6 !== ip6) this.announcedIp6 = announcedIp6;

this.initialAvailableOutgoingBitrate = initialAvailableOutgoingBitrate;
this.maxIncomingBitrate = maxIncomingBitrate;
Expand Down Expand Up @@ -167,17 +177,41 @@ export default class MediaService {
const worker = await mediasoup.createWorker(settings);
const workerData = worker.appData as unknown as WorkerData;

const webRtcServer = await worker.createWebRtcServer({
listenInfos: [ {
protocol: 'udp',
ip: this.ip,
announcedIp: this.announcedIp,
}, {
protocol: 'tcp',
ip: this.ip,
announcedIp: this.announcedIp,
} ]
});
const options: { listenInfos: Array<TransportListenInfo> } = {
listenInfos: []
};

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);

workerData.webRtcServer = webRtcServer;

Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/transportMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
announcedIp: internal ? undefined : mediaService.announcedIp
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,
Expand Down
12 changes: 10 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ const showUsage = () => {
logger.debug(' The path to the key file used for socket.\n\n');
logger.debug(' --ip <ip> (required)');
logger.debug(' The IP address used to create mediasoup transports.\n\n');
logger.debug(' --ip6 <ip> (required)');
logger.debug(' The IPv6 address used to create mediasoup transports.\n\n');
logger.debug(' --announcedIp <ip> (optional, default: none)');
logger.debug(' The IP address to be announced to clients for mediasoup transports.\n\n');
logger.debug(' The IPv4 address to be announced to clients for mediasoup transports.\n\n');
logger.debug(' --announcedIp6 <ip> (optional, default: none)');
logger.debug(' The IPv6 address to be announced to clients for mediasoup transports.\n\n');
logger.debug(' --initialAvailableOutgoingBitrate <bitrate> (optional, default: 600000)');
logger.debug(' The initial available outgoing bitrate for mediasoup transports.\n\n');
logger.debug(' --maxIncomingBitrate <bitrate> (optional, default: 10000000)');
Expand Down Expand Up @@ -108,7 +112,9 @@ export const cancelDrain = () => {
cert = './certs/edumeet-demo-cert.pem',
key = './certs/edumeet-demo-key.pem',
ip,
ip6,
announcedIp,
announcedIp6,
initialAvailableOutgoingBitrate = 600000,
maxIncomingBitrate = 10000000,
maxOutgoingBitrate = 10000000,
Expand All @@ -127,13 +133,15 @@ export const cancelDrain = () => {
return process.exit(1);
}

logger.debug('Starting...', { listenPort, listenHost, ip, announcedIp });
logger.debug('Starting...', { listenPort, listenHost, ip, announcedIp, ip6, announcedIp6 });

interactiveServer(roomServerConnections, roomServers);

const mediaService = await MediaService.create({
ip,
ip6,
announcedIp,
announcedIp6,
initialAvailableOutgoingBitrate,
maxIncomingBitrate,
maxOutgoingBitrate,
Expand Down
Loading