2022-07-29 08:32:51 +00:00
|
|
|
import 'dotenv/config'
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
/**
|
|
|
|
* integrating mediasoup server with a node.js application
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Please follow mediasoup installation requirements */
|
|
|
|
/* https://mediasoup.org/documentation/v3/mediasoup/installation/ */
|
2022-07-23 07:32:54 +00:00
|
|
|
import express from 'express'
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
import https from 'httpolyglot'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
const __dirname = path.resolve()
|
|
|
|
|
|
|
|
import Server from 'socket.io'
|
2022-07-27 06:55:07 +00:00
|
|
|
import mediasoup, { getSupportedRtpCapabilities } from 'mediasoup'
|
|
|
|
|
|
|
|
let worker
|
|
|
|
let router = {}
|
|
|
|
let producerTransport
|
|
|
|
let consumerTransport
|
|
|
|
let producer
|
|
|
|
let consumer
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-07-29 08:32:51 +00:00
|
|
|
app.get('/', (_req, res) => {
|
2022-07-23 07:32:54 +00:00
|
|
|
res.send('Hello from mediasoup app!')
|
|
|
|
})
|
|
|
|
|
|
|
|
app.use('/sfu', express.static(path.join(__dirname, 'public')))
|
|
|
|
|
|
|
|
// SSL cert for HTTPS access
|
|
|
|
const options = {
|
|
|
|
key: fs.readFileSync('./server/ssl/key.pem', 'utf-8'),
|
|
|
|
cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8')
|
|
|
|
}
|
|
|
|
|
|
|
|
const httpsServer = https.createServer(options, app)
|
2022-07-29 08:32:51 +00:00
|
|
|
|
|
|
|
httpsServer.listen(process.env.PORT, () => {
|
|
|
|
console.log('Listening on port:', process.env.PORT)
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const io = new Server(httpsServer)
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
// socket.io namespace (could represent a room?)
|
2022-07-23 07:32:54 +00:00
|
|
|
const peers = io.of('/mediasoup')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Worker
|
|
|
|
* |-> Router(s)
|
|
|
|
* |-> Producer Transport(s)
|
|
|
|
* |-> Producer
|
|
|
|
* |-> Consumer Transport(s)
|
|
|
|
* |-> Consumer
|
|
|
|
**/
|
|
|
|
|
|
|
|
const createWorker = async () => {
|
|
|
|
worker = await mediasoup.createWorker({
|
|
|
|
rtcMinPort: 2000,
|
|
|
|
rtcMaxPort: 2020,
|
|
|
|
})
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log(`[createWorker] worker pid ${worker.pid}`)
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
worker.on('died', error => {
|
|
|
|
// This implies something serious happened, so kill the application
|
2022-07-29 08:32:51 +00:00
|
|
|
console.error('mediasoup worker has died', error)
|
2022-07-23 07:32:54 +00:00
|
|
|
setTimeout(() => process.exit(1), 2000) // exit in 2 seconds
|
|
|
|
})
|
|
|
|
|
|
|
|
return worker
|
|
|
|
}
|
|
|
|
|
|
|
|
// We create a Worker as soon as our application starts
|
|
|
|
worker = createWorker()
|
|
|
|
|
|
|
|
// This is an Array of RtpCapabilities
|
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
|
|
|
|
// list of media codecs supported by mediasoup ...
|
|
|
|
// https://github.com/versatica/mediasoup/blob/v3/src/supportedRtpCapabilities.ts
|
|
|
|
const mediaCodecs = [
|
|
|
|
{
|
|
|
|
kind: 'audio',
|
|
|
|
mimeType: 'audio/opus',
|
|
|
|
clockRate: 48000,
|
|
|
|
channels: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: 'video',
|
|
|
|
mimeType: 'video/VP8',
|
|
|
|
clockRate: 90000,
|
|
|
|
parameters: {
|
|
|
|
'x-google-start-bitrate': 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2022-09-13 16:56:06 +00:00
|
|
|
let queue = []
|
|
|
|
|
|
|
|
// queue.push({
|
|
|
|
// callId: 1,
|
|
|
|
// callback: () => console.log('callback')
|
|
|
|
// })
|
|
|
|
// queue.push({
|
|
|
|
// callId: 2,
|
|
|
|
// callback: () => console.log('callback')
|
|
|
|
// })
|
|
|
|
// queue.push({
|
|
|
|
// callId: 3,
|
|
|
|
// callback: () => console.log('callback')
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
|
|
const getRtpCapabilities = (callId, callback) => {
|
|
|
|
const rtpCapabilities = router[callId].rtpCapabilities
|
2022-09-13 17:54:35 +00:00
|
|
|
console.log('[getRtpCapabilities]', callId, callback, rtpCapabilities);
|
2022-09-13 16:56:06 +00:00
|
|
|
callback({ rtpCapabilities })
|
|
|
|
}
|
|
|
|
|
|
|
|
setInterval(async () => {
|
2022-09-13 17:09:25 +00:00
|
|
|
if (queue.length > 0) {
|
|
|
|
const { callId, callback } = queue.shift();
|
|
|
|
if (router[callId] === undefined) {
|
2022-09-13 17:28:07 +00:00
|
|
|
console.log('🟢 callId', callId, '| callback', callback)
|
2022-09-13 17:09:25 +00:00
|
|
|
router[callId] = await worker.createRouter({ mediaCodecs })
|
|
|
|
console.log(`[createRoom] Router ID: ${router[callId].id}`)
|
2022-09-13 17:49:40 +00:00
|
|
|
console.log('🟡 router', router);
|
2022-09-13 17:09:25 +00:00
|
|
|
}
|
2022-09-13 17:59:12 +00:00
|
|
|
getRtpCapabilities(callId, callback)
|
2022-09-13 16:56:06 +00:00
|
|
|
}
|
2022-09-13 17:59:55 +00:00
|
|
|
}, 1000);
|
2022-09-13 16:56:06 +00:00
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
peers.on('connection', async socket => {
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log('[connection] socketId:', socket.id)
|
2022-07-29 09:22:35 +00:00
|
|
|
socket.emit('connection-success', {
|
|
|
|
socketId: socket.id,
|
|
|
|
existsProducer: producer ? true : false,
|
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
socket.on('disconnect', () => {
|
|
|
|
// do some cleanup
|
2022-07-27 06:55:07 +00:00
|
|
|
console.log('peer disconnected')
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
|
|
|
|
socket.on('createRoom', async ({ callId }, callback) => {
|
2022-09-13 17:58:06 +00:00
|
|
|
console.log('[createRoom] callId', callId);
|
2022-09-13 16:56:06 +00:00
|
|
|
// console.log('Router length:', Object.keys(router).length);
|
|
|
|
// if (router[callId] === undefined) {
|
|
|
|
// // worker.createRouter(options)
|
|
|
|
// // options = { mediaCodecs, appData }
|
|
|
|
// // mediaCodecs -> defined above
|
|
|
|
// // appData -> custom application data - we are not supplying any
|
|
|
|
// // none of the two are required
|
|
|
|
// router[callId] = await worker.createRouter({ mediaCodecs })
|
|
|
|
// console.log(`[createRoom] Router ID: ${router[callId].id}`)
|
|
|
|
// }
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-09-13 16:56:06 +00:00
|
|
|
// getRtpCapabilities(callId, callback)
|
|
|
|
queue.push({
|
|
|
|
callId,
|
|
|
|
callback
|
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Client emits a request to create server side Transport
|
|
|
|
// We need to differentiate between the producer and consumer transports
|
|
|
|
socket.on('createWebRtcTransport', async ({ sender, callId }, callback) => {
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log(`[createWebRtcTransport] Is this a sender request? ${sender} | callId ${callId}`)
|
2022-07-27 06:55:07 +00:00
|
|
|
// The client indicates if it is a producer or a consumer
|
|
|
|
// if sender is true, indicates a producer else a consumer
|
|
|
|
if (sender)
|
|
|
|
producerTransport = await createWebRtcTransportLayer(callId, callback)
|
|
|
|
else
|
|
|
|
consumerTransport = await createWebRtcTransportLayer(callId, callback)
|
|
|
|
})
|
|
|
|
|
|
|
|
// see client's socket.emit('transport-connect', ...)
|
|
|
|
socket.on('transport-connect', async ({ dtlsParameters }) => {
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log('[transport-connect] DTLS PARAMS... ', { dtlsParameters })
|
2022-07-27 06:55:07 +00:00
|
|
|
await producerTransport.connect({ dtlsParameters })
|
|
|
|
})
|
|
|
|
|
|
|
|
// see client's socket.emit('transport-produce', ...)
|
2022-08-31 13:43:59 +00:00
|
|
|
socket.on('transport-produce', async ({ kind, rtpParameters, appData }) => {
|
2022-07-27 06:55:07 +00:00
|
|
|
// call produce based on the prameters from the client
|
|
|
|
producer = await producerTransport.produce({
|
|
|
|
kind,
|
|
|
|
rtpParameters,
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log(`[transport-produce] Producer ID: ${producer.id} | kind: ${producer.kind}`)
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-08-11 09:18:01 +00:00
|
|
|
producer.on('transportclose', () => {
|
2022-08-11 19:11:49 +00:00
|
|
|
console.log('transport for this producer closed', callId)
|
2022-08-11 19:24:33 +00:00
|
|
|
|
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#producer-close
|
2022-08-11 12:03:00 +00:00
|
|
|
producer.close()
|
2022-08-11 19:24:33 +00:00
|
|
|
|
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
|
2022-08-11 19:11:49 +00:00
|
|
|
router[callId].close()
|
2022-08-11 12:03:00 +00:00
|
|
|
delete router[callId]
|
2022-08-11 09:18:01 +00:00
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// see client's socket.emit('transport-recv-connect', ...)
|
|
|
|
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log(`[transport-recv-connect] DTLS PARAMS: ${dtlsParameters}`)
|
2022-07-27 06:55:07 +00:00
|
|
|
await consumerTransport.connect({ dtlsParameters })
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('consume', async ({ rtpCapabilities, callId }, callback) => {
|
|
|
|
try {
|
2022-08-31 13:47:54 +00:00
|
|
|
// console.log('consume', rtpCapabilities, callId);
|
2022-07-27 06:55:07 +00:00
|
|
|
// check if the router can consume the specified producer
|
|
|
|
if (router[callId].canConsume({
|
|
|
|
producerId: producer.id,
|
|
|
|
rtpCapabilities
|
|
|
|
})) {
|
|
|
|
// transport can now consume and return a consumer
|
|
|
|
consumer = await consumerTransport.consume({
|
2022-07-23 07:32:54 +00:00
|
|
|
producerId: producer.id,
|
2022-07-27 06:55:07 +00:00
|
|
|
rtpCapabilities,
|
|
|
|
paused: true,
|
|
|
|
})
|
|
|
|
|
2022-08-11 09:18:01 +00:00
|
|
|
consumer.on('transportclose', () => {
|
2022-08-11 11:22:28 +00:00
|
|
|
console.log('transport close from consumer', callId)
|
2022-08-31 13:53:08 +00:00
|
|
|
|
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
|
|
|
|
router[callId].close()
|
2022-08-11 12:03:00 +00:00
|
|
|
delete router[callId]
|
2022-08-31 14:06:54 +00:00
|
|
|
producer.close()
|
|
|
|
consumer.close()
|
2022-08-11 09:18:01 +00:00
|
|
|
})
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
consumer.on('producerclose', () => {
|
2022-08-11 19:11:49 +00:00
|
|
|
console.log('producer of consumer closed', callId)
|
2022-08-11 19:24:33 +00:00
|
|
|
|
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
|
2022-08-11 19:11:49 +00:00
|
|
|
router[callId].close()
|
|
|
|
delete router[callId]
|
2022-08-31 14:06:54 +00:00
|
|
|
producer.close()
|
|
|
|
consumer.close()
|
2022-07-27 06:55:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// from the consumer extract the following params
|
|
|
|
// to send back to the Client
|
|
|
|
const params = {
|
|
|
|
id: consumer.id,
|
|
|
|
producerId: producer.id,
|
|
|
|
kind: consumer.kind,
|
|
|
|
rtpParameters: consumer.rtpParameters,
|
|
|
|
}
|
2022-08-18 06:34:22 +00:00
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
// send the parameters to the client
|
|
|
|
callback({ params })
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
2022-07-27 06:55:07 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error.message)
|
|
|
|
callback({
|
|
|
|
params: {
|
|
|
|
error: error
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
socket.on('consumer-resume', async () => {
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log(`[consumer-resume]`)
|
2022-07-27 06:55:07 +00:00
|
|
|
await consumer.resume()
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
const createWebRtcTransportLayer = async (callId, callback) => {
|
2022-07-23 07:32:54 +00:00
|
|
|
try {
|
2022-08-12 11:56:20 +00:00
|
|
|
console.log('[createWebRtcTransportLayer] callId', callId);
|
2022-07-23 07:32:54 +00:00
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
|
|
|
|
const webRtcTransport_options = {
|
|
|
|
listenIps: [
|
|
|
|
{
|
2022-07-29 08:32:51 +00:00
|
|
|
ip: process.env.IP, // Listening IPv4 or IPv6.
|
|
|
|
announcedIp: process.env.ANNOUNCED_IP, // Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
enableUdp: true,
|
|
|
|
enableTcp: true,
|
|
|
|
preferUdp: true,
|
|
|
|
}
|
2022-08-12 11:56:20 +00:00
|
|
|
|
2022-08-16 11:21:02 +00:00
|
|
|
// console.log('webRtcTransport_options', webRtcTransport_options);
|
|
|
|
// console.log('router', router, '| router[callId]', router[callId]);
|
2022-07-29 08:32:51 +00:00
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
|
|
|
|
let transport = await router[callId].createWebRtcTransport(webRtcTransport_options)
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log(`callId: ${callId} | transport id: ${transport.id}`)
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
transport.on('dtlsstatechange', dtlsState => {
|
|
|
|
if (dtlsState === 'closed') {
|
|
|
|
transport.close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
transport.on('close', () => {
|
|
|
|
console.log('transport closed')
|
|
|
|
})
|
|
|
|
|
2022-08-16 11:21:02 +00:00
|
|
|
const params = {
|
|
|
|
id: transport.id,
|
|
|
|
iceParameters: transport.iceParameters,
|
|
|
|
iceCandidates: transport.iceCandidates,
|
|
|
|
dtlsParameters: transport.dtlsParameters,
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:49:23 +00:00
|
|
|
// console.log('params', params);
|
2022-08-16 11:21:02 +00:00
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
// send back to the client the following prameters
|
|
|
|
callback({
|
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#TransportOptions
|
2022-08-16 11:21:02 +00:00
|
|
|
params
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return transport
|
|
|
|
|
|
|
|
} catch (error) {
|
2022-08-12 11:56:20 +00:00
|
|
|
console.log('[createWebRtcTransportLayer] ERROR', JSON.stringify(error));
|
2022-07-23 07:32:54 +00:00
|
|
|
callback({
|
|
|
|
params: {
|
|
|
|
error: error
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|