require('dotenv').config() const express = require('express'); const app = express(); const Server = require('socket.io'); const path = require('node:path'); const fs = require('node:fs'); let https = require('https'); try { https = require('node:https'); } catch (err) { console.log('https support is disabled!'); } const mediasoup = require('mediasoup'); let worker /** * videoCalls * |-> Router * |-> Producer * |-> Consumer * |-> Producer Transport * |-> Consumer Transport * * '': { * router: Router, * producer: Producer, * producerTransport: Producer Transport, * consumer: Consumer, * consumerTransport: Consumer Transport * } * **/ let videoCalls = {} let socketDetails = {} app.get('/', (_req, res) => { 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); const io = new Server(httpsServer, { allowEIO3: true, origins: ["*:*"], // allowRequest: (req, next) => { // console.log('req', req); // next(null, true) // } }); // const io = new Server(server, { origins: '*:*', allowEIO3: true }); httpsServer.listen(process.env.PORT, () => { console.log('Video server listening on port:', process.env.PORT) }) const peers = io.of('/') const createWorker = async () => { try { worker = await mediasoup.createWorker({ rtcMinPort: 2000, rtcMaxPort: 2020, }) console.log(`[createWorker] worker pid ${worker.pid}`); worker.on('died', error => { // This implies something serious happened, so kill the application console.error('mediasoup worker has died', error); setTimeout(() => process.exit(1), 2000); // exit in 2 seconds }) return worker; } catch (error) { console.log(`ERROR | createWorker | ${error.message}`); } } // 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, }, }, ] const closeCall = (callId) => { try { if (videoCalls[callId]) { videoCalls[callId].producer?.close(); videoCalls[callId].consumer?.close(); videoCalls[callId]?.consumerTransport?.close(); videoCalls[callId]?.producerTransport?.close(); videoCalls[callId]?.router?.close(); delete videoCalls[callId]; } else { console.log(`The call with id ${callId} has already been deleted`); } } catch (error) { console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`); } } const getRtpCapabilities = (callId, callback) => { try { console.log('[getRtpCapabilities] callId', callId); const rtpCapabilities = videoCalls[callId].router.rtpCapabilities; callback({ rtpCapabilities }); } catch (error) { console.log(`ERROR | getRtpCapabilities | callId ${callId} | ${error.message}`); } } /* - Handlers for WS events - These are created only when we have a connection with a peer */ peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id); // After making the connection successfully, we send the client a 'connection-success' event socket.emit('connection-success', { socketId: socket.id }); // It is triggered when the peer is disconnected socket.on('disconnect', () => { const callId = socketDetails[socket.id]; console.log(`disconnect | socket ${socket.id} | callId ${callId}`); delete socketDetails[socket.id]; closeCall(callId); }); /* - This event creates a room with the roomId and the callId sent - It will return the rtpCapabilities of that room - If the room already exists, it will not create it, but will only return rtpCapabilities */ socket.on('createRoom', async ({ callId }, callback) => { try { if (callId) { console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`); if (!videoCalls[callId]) { console.log('[createRoom] callId', callId); videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) } console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`); } socketDetails[socket.id] = callId; getRtpCapabilities(callId, callback); } else { console.log(`[createRoom] missing callId ${callId}`); } } catch (error) { console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`); } }); /* - Client emits a request to create server side Transport - Depending on the sender, producerTransport or consumerTransport is created on that router - It will return parameters, these are required for the client to create the RecvTransport from the client. - If the client is producer(sender: true) then it will use parameters for device.createSendTransport(params) - If the client is a consumer(sender: false) then it will use parameters for device.createRecvTransport(params) */ socket.on('createWebRtcTransport', async ({ sender }, callback) => { try { const callId = socketDetails[socket.id]; console.log(`[createWebRtcTransport] sender ${sender} | callId ${callId}`); if (sender) { if (!videoCalls[callId].producerTransport) { videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback); } else { console.log(`producerTransport has already been defined | callId ${callId}`); } } else if (!sender) { if (!videoCalls[callId].consumerTransport) { videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback); } else { console.log(`consumerTransport has already been defined | callId ${callId}`); } } } catch (error) { console.log(`ERROR | createWebRtcTransport | callId ${callId} | sender ${sender} | ${error.message}`); } }); /* - The client sends this event after successfully creating a createSendTransport(AS PRODUCER) - The connection is made to the created transport */ socket.on('transport-connect', async ({ dtlsParameters }) => { try { const callId = socketDetails[socket.id]; console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId}`) await videoCalls[callId].producerTransport.connect({ dtlsParameters }); } catch (error) { console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`); } }); /* - The event sent by the client (PRODUCER) after successfully connecting to producerTransport - For the router with the id callId, we make produce on producerTransport - Create the handler on producer at the 'transportclose' event */ socket.on('transport-produce', async ({ kind, rtpParameters, appData }) => { try { const callId = socketDetails[socket.id]; console.log('[transport-produce] | socket.id', socket.id, '| callId', callId); videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({ kind, rtpParameters, }); console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producer.id} | kind: ${videoCalls[callId].producer.kind}`); videoCalls[callId].producer.on('transportclose', () => { const callId = socketDetails[socket.id]; console.log('transport for this producer closed', callId) closeCall(callId); }); } catch (error) { console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`); } }); /* - The client sends this event after successfully creating a createRecvTransport(AS CONSUMER) - The connection is made to the created consumerTransport */ socket.on('transport-recv-connect', async ({ dtlsParameters }) => { try { const callId = socketDetails[socket.id]; console.log(`[transport-recv-connect] socket.id ${socket.id} | callId ${callId}`); await videoCalls[callId].consumerTransport.connect({ dtlsParameters }); } catch (error) { console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ${error.message}`); } }) /* - The customer consumes after successfully connecting to consumerTransport - The previous step was 'transport-recv-connect', and before that 'createWebRtcTransport' - This event is only sent by the consumer - The parameters that the consumer consumes are returned - The consumer does consumerTransport.consume(params) */ socket.on('consume', async ({ rtpCapabilities }, callback) => { try { const callId = socketDetails[socket.id]; console.log('[consume] callId', callId); // Check if the router can consume the specified producer if (videoCalls[callId].router.canConsume({ producerId: videoCalls[callId].producer.id, rtpCapabilities })) { console.log('[consume] Can consume', callId); // Transport can now consume and return a consumer videoCalls[callId].consumer = await videoCalls[callId].consumerTransport.consume({ producerId: videoCalls[callId].producer.id, rtpCapabilities, paused: true, }); // https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose videoCalls[callId].consumer.on('transportclose', () => { const callId = socketDetails[socket.id]; console.log('transport close from consumer', callId); closeCall(); }); // https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose videoCalls[callId].consumer.on('producerclose', () => { const callId = socketDetails[socket.id]; console.log('producer of consumer closed', callId); closeCall(); }); // From the consumer extract the following params to send back to the Client const params = { id: videoCalls[callId].consumer.id, producerId: videoCalls[callId].producer.id, kind: videoCalls[callId].consumer.kind, rtpParameters: videoCalls[callId].consumer.rtpParameters, }; // Send the parameters to the client callback({ params }); } else { console.log(`[canConsume] Can't consume | callId ${callId}`); } } catch (error) { console.log(`ERROR | consume | callId ${callId} | ${error.message}`) callback({ params: { error } }); } }); /* - Event sent by the consumer after consuming to resume the pause - When consuming on consumerTransport, it is initially done with paused: true, here we will resume */ socket.on('consumer-resume', async () => { try { const callId = socketDetails[socket.id]; console.log(`[consumer-resume] callId ${callId}`) await videoCalls[callId].consumer.resume(); } catch (error) { console.log(`ERROR | consumer-resume | callId ${socketDetails[socket.id]} | ${error.message}`); } }); }); /* - Called from at event 'createWebRtcTransport' and assigned to the consumer or producer transport - It will return parameters, these are required for the client to create the RecvTransport from the client. - If the client is producer(sender: true) then it will use parameters for device.createSendTransport(params) - If the client is a consumer(sender: false) then it will use parameters for device.createRecvTransport(params) */ const createWebRtcTransportLayer = async (callId, callback) => { try { console.log('[createWebRtcTransportLayer] callId', callId); // https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions const webRtcTransport_options = { listenIps: [ { 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). } ], enableUdp: true, enableTcp: true, preferUdp: true, }; // https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options) console.log(`callId: ${callId} | transport id: ${transport.id}`) // Handler for when DTLS(Datagram Transport Layer Security) changes transport.on('dtlsstatechange', dtlsState => { console.log(`transport | dtlsstatechange | calldId ${callId} | dtlsState ${dtlsState}`); if (dtlsState === 'closed') { transport.close(); } }); // Handler if the transport layer has closed (for various reasons) transport.on('close', () => { console.log(`transport | closed | calldId ${callId}`); }); const params = { id: transport.id, iceParameters: transport.iceParameters, iceCandidates: transport.iceCandidates, dtlsParameters: transport.dtlsParameters, }; // Send back to the client the params callback({ params }); // Set transport to producerTransport or consumerTransport return transport; } catch (error) { console.log(`ERROR | createWebRtcTransportLayer | callId ${callId} | ${error.message}`); callback({ params: { error } }); } }