Skip to content

Commit

Permalink
feat: add MediasoupSlaveService.addFromEnv()
Browse files Browse the repository at this point in the history
  • Loading branch information
woody146 committed Jun 8, 2023
1 parent b31f7a3 commit 957e14a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ DATABASE_URL = "postgresql://postgres:123456@localhost:5432/mediasoup"
DATABASE_SYNC = 1
DATABASE_LOGGING = 1

PORT = 3000
# PORT = 3000
SLAVE_FOR = producer
SLAVE_INTERNAL_HOST = 'localhost'
SLAVE_EXTERNAL_HOST = 'localhost'
SLAVE_MAX_PEER = 1000

MEDIASOUP_LOG_LEVEL = warn
MEDIASOUP_LOG_TAGS = 'info ice dtls rtp srtp rtcp rtx bwe score simulcast svc sctp'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"start:master": "node dist/run_master.js",
"start:master": "PORT=3000 node dist/run_master.js",
"start:slave": "PORT=3002 node dist/run_slave.js",
"dev": "node dist/run_master.js",
"prepare": "husky install"
Expand Down
13 changes: 10 additions & 3 deletions src/entities/mediasoup.slave.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from 'typeorm';
import {
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
Index,
} from 'typeorm';

@Entity()
@Index(['internalHost', 'apiPort'], { unique: true })
export class MediasoupSlave extends BaseEntity {
@PrimaryGeneratedColumn('increment')
id!: number;
Expand All @@ -17,8 +24,8 @@ export class MediasoupSlave extends BaseEntity {
@Column('integer')
apiPort!: number;

@Column('integer', { nullable: true })
maxPeer?: number;
@Column('integer', { default: 1e9 })
maxPeer!: number;

@Column('integer', { default: 0 })
peerCount!: number;
Expand Down
12 changes: 10 additions & 2 deletions src/run_slave.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { startServer } from './utils/index.js';
import { MediasoupSlaveService } from './services/mediasoup.slave.js';
import { startServer, getDataSource } from './utils/index.js';

startServer([]);
async function bootstrap() {
await startServer([]);
try {
await new MediasoupSlaveService(getDataSource()).addFromEnv();
} catch (e) {}
}

bootstrap();
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './mediasoup.slave.js';
export * from './mediasoup.room.js';
export * from './mediasoup.worker.js';
26 changes: 24 additions & 2 deletions src/services/mediasoup.slave.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Raw } from 'typeorm';
import { constants } from '../constants.js';
import { MediasoupSlave } from '../entities/index.js';
import { BaseService } from './base.js';

export class MediasoupSlaveService extends BaseService {
add(data: {
async add(data: {
internalHost: string;
externalHost: string;
for: string; // consumer | producer
Expand All @@ -15,6 +16,27 @@ export class MediasoupSlaveService extends BaseService {
throw new Error('Invalid for param');
}
Object.assign(mediasoupSlave, data);
return this.dataSource.getRepository(MediasoupSlave).save(mediasoupSlave);
await this.dataSource.getRepository(MediasoupSlave).save(mediasoupSlave);
return {};
}

addFromEnv() {
return this.add({
internalHost: process.env.SLAVE_INTERNAL_HOST || 'localhost',
externalHost: process.env.SLAVE_EXTERNAL_HOST || 'localhost',
for: process.env.SLAVE_FOR || constants.CONSUMER,
apiPort: Number(process.env.PORT || 80),
maxPeer: Number(process.env.SLAVE_MAX_PEER),
});
}

async getForNewRoom() {
const result = await this.dataSource.getRepository(MediasoupSlave).findOne({
where: {
for: constants.PRODUCER,
peerCount: Raw((alias) => `${alias} < maxPeer`),
},
});
return result;
}
}

0 comments on commit 957e14a

Please sign in to comment.