mediasoup-worker多进程启动时,rtcMinPort/rtcMaxPort可以使用相同的配置。
for (let i = 0; i < numWorkers; i++) {
let worker = await mediasoup.createWorker({
logLevel: config.mediasoup.worker.logLevel,
logTags: config.mediasoup.worker.logTags,
rtcMinPort: minPort,
rtcMaxPort: maxPort
})
}
如何避免port冲突呢?答案是mediasoup的node主进程来处理的。
async function createWorker({ logLevel = 'error', logTags, rtcMinPort = 10000, rtcMaxPort = 59999, dtlsCertificateFile, dtlsPrivateKeyFile, libwebrtcFieldTrials, appData } = {}) {
const worker = new Worker_1.Worker({
logLevel,
logTags,
rtcMinPort,
rtcMaxPort,
dtlsCertificateFile,
dtlsPrivateKeyFile,
libwebrtcFieldTrials,
appData
});
return new Promise((resolve, reject) => {
worker.on('@success', () => {
observer.safeEmit('newworker', worker);
resolve(worker);
});
worker.on('@failure', reject);
});
}
class Worker extends EnhancedEventEmitter_1.EnhancedEventEmitter {
// mediasoup-worker child process. mediasoup-worker是node.js的子进程。
// const child_process_1 = require("child_process"); //
#child;
let spawnBin = workerBin;
let spawnArgs = [];
if (typeof rtcMinPort === 'number' && !Number.isNaN(rtcMinPort)) {
spawnArgs.push(`--rtcMinPort=${rtcMinPort}`);
}
if (typeof rtcMaxPort === 'number' && !Number.isNaN(rtcMaxPort)) {
spawnArgs.push(`--rtcMaxPort=${rtcMaxPort}`);
}
spawnArgs.join(' '));
this.#child = (0, child_process_1.spawn)(
spawnBin,
spawnArgs,
}
async createWebRtcServer({ listenInfos, appData }) {
const webRtcServer = new WebRtcServer_1.WebRtcServer({
internal: { webRtcServerId: reqData.webRtcServerId },
channel: this.#channel,
appData
});
this.#webRtcServers.add(webRtcServer);
return webRtcServer;
}
}
node.js的webRtcServer是c++ mediasoup-worker的封装。
关于RTP port,包括组成medsoup-cluster的pipeToRemoteRouter的RTP转发port,
是由node.js主进程统一管理使用的,在各个子进程间不会有冲突。