Skip to content

Commit

Permalink
feat: add MediasoupWorkerManager
Browse files Browse the repository at this point in the history
  • Loading branch information
woody146 committed Jun 8, 2023
1 parent b9d3bf4 commit b31f7a3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
DATABASE_URL = "postgresql://postgres:123456@localhost:5432/mediasoup"
DATABASE_SYNC = 1
DATABASE_LOGGING = 1

PORT = 3000

MEDIASOUP_LOG_LEVEL = warn
MEDIASOUP_LOG_TAGS = 'info ice dtls rtp srtp rtcp rtx bwe score simulcast svc sctp'
MEDIASOUP_NUMBER_OF_WORKERS = 10
MEDIASOUP_RTC_MIN_PORT = 20000
MEDIASOUP_RTC_MAX_PORT = 40000
MEDIASOUP_MEDIA_CODECS = '
[
{
kind : "audio",
mimeType : "audio/opus",
clockRate : 48000,
channels : 2
},
{
kind : "video",
mimeType : "video/VP8",
clockRate : 90000,
parameters :
{
"x-google-start-bitrate" : 1000
}
},
{
kind : "video",
mimeType : "video/VP9",
clockRate : 90000,
parameters :
{
"profile-id" : 2,
"x-google-start-bitrate" : 1000
}
},
{
kind : "video",
mimeType : "video/h264",
clockRate : 90000,
parameters :
{
"packetization-mode" : 1,
"profile-level-id" : "4d0032",
"level-asymmetry-allowed" : 1,
"x-google-start-bitrate" : 1000
}
},
{
kind : "video",
mimeType : "video/h264",
clockRate : 90000,
parameters :
{
"packetization-mode" : 1,
"profile-level-id" : "42e01f",
"level-asymmetry-allowed" : 1,
"x-google-start-bitrate" : 1000
}
}
]'
36 changes: 36 additions & 0 deletions src/services/mediasoup.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import mediasoup, { type types } from 'mediasoup';

class MediasoupWorkerManager {
workers: types.Worker[] = [];
currentWorker = 0;

async init() {
const numWorkers = Number(process.env.MEDIASOUP_NUMBER_OF_WORKERS || '1');

for (let i = 0; i < numWorkers; ++i) {
const worker = await mediasoup.createWorker({
logLevel: (process.env.MEDIASOUP_LOG_LEVEL || 'none') as any,
logTags: (process.env.MEDIASOUP_LOG_TAGS || ' ').split(' ') as any,
rtcMinPort: Number(process.env.MEDIASOUP_RTC_MIN_PORT),
rtcMaxPort: Number(process.env.MEDIASOUP_RTC_MAX_PORT),
});

worker.on('died', (e) => {
console.error(e);
});

this.workers.push(worker);
}
}

getWorker() {
const result = this.workers[this.currentWorker];
this.currentWorker += 1;
if (this.currentWorker === this.workers.length) {
this.currentWorker = 0;
}
return result;
}
}

export const mediasoupWorkerManager = new MediasoupWorkerManager();

0 comments on commit b31f7a3

Please sign in to comment.