-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add MediasoupPipeTransportManager
- Loading branch information
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './worker.js'; | ||
export * from './router.js'; | ||
export * from './webrtc.transport.js'; | ||
export * from './pipe.transport.js'; | ||
export * from './producer.js'; | ||
export * from './consumer.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { types } from 'mediasoup'; | ||
import { ServiceError } from '../base.js'; | ||
import { mediasoupRouterManager } from './router.js'; | ||
import { fetchApi } from '../../utils/api.js'; | ||
|
||
class MediasoupPipeTransportManager { | ||
static transports = new Map<string, types.PipeTransport>(); | ||
|
||
async create(data: { routerId: string }) { | ||
const router = mediasoupRouterManager.get(data.routerId); | ||
if (router) { | ||
const transport = await router.createPipeTransport({ | ||
listenIp: process.env.SLAVE_INTERNAL_HOST || '127.0.0.1', | ||
enableSctp: true, | ||
numSctpStreams: { OS: 1024, MIS: 1024 }, | ||
}); | ||
MediasoupPipeTransportManager.transports.set(transport.id, transport); | ||
return transport; | ||
} | ||
throw new ServiceError(404, 'Router not found'); | ||
} | ||
|
||
async createDestination(data: { | ||
routerId: string; | ||
sourceHost: string; | ||
sourcePort: string; | ||
sourceRouterId: string; | ||
sourceProducerId: string; | ||
}) { | ||
const transport = await this.create(data); | ||
const sourceResult = await fetchApi({ | ||
host: data.sourceHost, | ||
port: data.sourcePort, | ||
path: '/routers/:routerId/source_pipe_transports', | ||
method: 'POST', | ||
data: { | ||
routerId: data.sourceRouterId, | ||
destinationIp: transport.tuple.localIp, | ||
destinationPort: transport.tuple.localPort, | ||
destinationSrtpParameters: transport.srtpParameters, | ||
}, | ||
}); | ||
await transport.connect({ | ||
ip: sourceResult.sourceIp, | ||
port: sourceResult.sourcePort, | ||
srtpParameters: sourceResult.sourceSrtpParameters, | ||
}); | ||
const consumerResult = await fetchApi({ | ||
host: data.sourceHost, | ||
port: data.sourcePort, | ||
path: '/pipe_transports/:transportId/consume', | ||
method: 'POST', | ||
data: { | ||
transportId: sourceResult.id, | ||
producerId: data.sourceProducerId, | ||
}, | ||
}); | ||
const pipeDataProducer = await transport.produce({ | ||
id: data.sourceProducerId, | ||
kind: consumerResult.kind, | ||
rtpParameters: consumerResult.rtpParameters, | ||
paused: consumerResult.producerPaused, | ||
}); | ||
return { id: pipeDataProducer.id }; | ||
} | ||
|
||
async createSource(data: { | ||
routerId: string; | ||
destinationIp: string; | ||
destinationPort: number; | ||
destinationSrtpParameters: types.SrtpParameters; | ||
}) { | ||
const transport = await this.create(data); | ||
await transport.connect({ | ||
ip: data.destinationIp, | ||
port: data.destinationPort, | ||
srtpParameters: data.destinationSrtpParameters, | ||
}); | ||
return { | ||
id: transport.id, | ||
sourceIp: transport.tuple.localIp, | ||
sourcePort: transport.tuple.localPort, | ||
sourceSrtpParameters: transport.srtpParameters, | ||
}; | ||
} | ||
|
||
async consume(data: { transportId: string; producerId: string }) { | ||
const transport = MediasoupPipeTransportManager.transports.get( | ||
data.transportId | ||
); | ||
if (transport) { | ||
const pipeConsumer = await transport.consume({ | ||
producerId: data.producerId, | ||
}); | ||
return { | ||
kind: pipeConsumer.kind, | ||
rtpParameters: pipeConsumer.rtpParameters, | ||
paused: pipeConsumer.producerPaused, | ||
}; | ||
} | ||
throw new ServiceError(404, 'Transport not found'); | ||
} | ||
} | ||
|
||
export const mediasoupPipeTransportManager = | ||
new MediasoupPipeTransportManager(); |