@@ -3,6 +3,10 @@ import type { WebRtcServerOptions } from "mediasoup/node/lib/types";
33
44import * as config from "#src/config.ts" ;
55import { Logger } from "#src/utils/utils.ts" ;
6+ import { PortLimitReachedError } from "#src/utils/errors.ts" ;
7+ import os from "node:os" ;
8+
9+ const availablePorts : Set < number > = new Set ( ) ;
610
711export interface RtcWorker extends mediasoup . types . Worker {
812 appData : {
@@ -12,6 +16,7 @@ export interface RtcWorker extends mediasoup.types.Worker {
1216
1317const logger = new Logger ( "RESOURCES" ) ;
1418const workers = new Set < RtcWorker > ( ) ;
19+ const tempDir = os . tmpdir ( ) + "/ongoing_recordings" ;
1520
1621export async function start ( ) : Promise < void > {
1722 logger . info ( "starting..." ) ;
@@ -22,6 +27,10 @@ export async function start(): Promise<void> {
2227 logger . info (
2328 `transport(RTC) layer at ${ config . PUBLIC_IP } :${ config . RTC_MIN_PORT } -${ config . RTC_MAX_PORT } `
2429 ) ;
30+ for ( let i = config . dynamicPorts . min ; i <= config . dynamicPorts . max ; i ++ ) {
31+ availablePorts . add ( i ) ;
32+ }
33+ logger . info ( `${ availablePorts . size } dynamic ports available [${ config . dynamicPorts . min } -${ config . dynamicPorts . max } ]` ) ;
2534}
2635
2736export function close ( ) : void {
@@ -78,18 +87,26 @@ export async function getWorker(): Promise<mediasoup.types.Worker> {
7887}
7988
8089export function getFolder ( ) {
81- // create a temp folder at a path, returns the path and a function to seal the folder
90+ const tempName = `${ Date . now ( ) } ` ;
91+ const path = `${ tempDir } /${ tempName } ` ;
92+ // TODO we may want to track these temp folders to remove them periodically (although os.tempDir() has already such a mechanism)
8293 return {
83- path : "" ,
84- sealFolder : ( ) => {
85- // move the content into a permanent folder location so it can easily be retrieved for processing later
86- // or directly forward for transcription
94+ path,
95+ sealFolder : ( name : string = tempName ) => {
96+ // TODO move whatever is in path to
97+ console . log ( ` ${ config . recording . directory } / ${ name } ` ) ;
8798 } ,
8899 }
89100}
90101
91- export function getPort ( ) {
102+ export function getPort ( ) : number {
103+ const port = availablePorts . values ( ) . next ( ) . value ;
104+ if ( ! port ) {
105+ throw new PortLimitReachedError ( ) ;
106+ }
107+ return port ;
92108}
93109
94110export function releasePort ( port : number ) {
111+ availablePorts . add ( port ) ;
95112}
0 commit comments