2023-01-16 15:32:31 +00:00
|
|
|
require('dotenv').config();
|
2022-09-19 20:09:55 +00:00
|
|
|
|
2022-09-19 14:43:39 +00:00
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
2022-09-19 14:55:21 +00:00
|
|
|
const Server = require('socket.io');
|
2022-09-19 14:40:57 +00:00
|
|
|
const path = require('node:path');
|
2022-09-19 14:44:45 +00:00
|
|
|
const fs = require('node:fs');
|
2022-10-06 12:21:54 +00:00
|
|
|
let https;
|
2022-09-20 06:36:31 +00:00
|
|
|
try {
|
|
|
|
https = require('node:https');
|
|
|
|
} catch (err) {
|
|
|
|
console.log('https support is disabled!');
|
|
|
|
}
|
2022-09-19 14:40:57 +00:00
|
|
|
const mediasoup = require('mediasoup');
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-12-27 11:11:06 +00:00
|
|
|
let worker;
|
2022-09-13 19:24:10 +00:00
|
|
|
/**
|
2023-01-16 15:32:31 +00:00
|
|
|
*
|
2022-12-19 17:31:33 +00:00
|
|
|
* videoCalls - Dictionary of Object(s)
|
2022-09-13 19:24:10 +00:00
|
|
|
* '<callId>': {
|
2022-12-27 11:11:06 +00:00
|
|
|
* router: Router,
|
2022-12-19 17:31:33 +00:00
|
|
|
* initiatorAudioProducer: Producer,
|
|
|
|
* initiatorVideoProducer: Producer,
|
2022-12-27 11:11:06 +00:00
|
|
|
* receiverVideoProducer: Producer,
|
|
|
|
* receiverAudioProducer: Producer,
|
2022-12-19 17:31:33 +00:00
|
|
|
* initiatorProducerTransport: Producer Transport,
|
2022-12-27 11:11:06 +00:00
|
|
|
* receiverProducerTransport: Producer Transport,
|
|
|
|
* initiatorConsumerVideo: Consumer,
|
|
|
|
* initiatorConsumerAudio: Consumer,
|
|
|
|
* initiatorConsumerTransport: Consumer Transport
|
2023-01-13 18:11:24 +00:00
|
|
|
* initiatorSocket
|
|
|
|
* receiverSocket
|
2022-09-13 19:24:10 +00:00
|
|
|
* }
|
2023-01-16 15:32:31 +00:00
|
|
|
*
|
2022-09-13 19:24:10 +00:00
|
|
|
**/
|
2022-12-27 11:11:06 +00:00
|
|
|
let videoCalls = {};
|
|
|
|
let socketDetails = {};
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-07-29 08:32:51 +00:00
|
|
|
app.get('/', (_req, res) => {
|
2023-01-16 15:32:31 +00:00
|
|
|
res.send('Hello from mediasoup app!');
|
2022-12-27 11:11:06 +00:00
|
|
|
});
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-12-27 11:11:06 +00:00
|
|
|
app.use('/sfu', express.static(path.join(__dirname, 'public')));
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
// SSL cert for HTTPS access
|
|
|
|
const options = {
|
2022-10-06 12:21:54 +00:00
|
|
|
key: fs.readFileSync(process.env.SERVER_KEY, 'utf-8'),
|
|
|
|
cert: fs.readFileSync(process.env.SERVER_CERT, 'utf-8'),
|
2022-12-27 11:11:06 +00:00
|
|
|
};
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-09-20 06:36:31 +00:00
|
|
|
const httpsServer = https.createServer(options, app);
|
2022-09-19 15:02:30 +00:00
|
|
|
|
2022-09-19 14:55:21 +00:00
|
|
|
const io = new Server(httpsServer, {
|
2022-09-20 06:36:31 +00:00
|
|
|
allowEIO3: true,
|
2023-01-16 15:32:31 +00:00
|
|
|
origins: ['*:*'],
|
2022-09-19 14:55:21 +00:00
|
|
|
});
|
2022-09-19 13:21:50 +00:00
|
|
|
|
2022-09-20 06:36:31 +00:00
|
|
|
httpsServer.listen(process.env.PORT, () => {
|
2022-10-06 12:21:54 +00:00
|
|
|
console.log('Video server listening on port:', process.env.PORT);
|
|
|
|
});
|
2022-09-20 06:36:31 +00:00
|
|
|
|
2022-10-06 12:21:54 +00:00
|
|
|
const peers = io.of('/');
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
const createWorker = async () => {
|
2022-09-25 17:03:17 +00:00
|
|
|
try {
|
|
|
|
worker = await mediasoup.createWorker({
|
2022-10-18 15:27:02 +00:00
|
|
|
rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
|
|
|
|
rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
|
2023-01-16 15:32:31 +00:00
|
|
|
});
|
2022-09-25 17:03:17 +00:00
|
|
|
console.log(`[createWorker] worker pid ${worker.pid}`);
|
2023-01-16 15:32:31 +00:00
|
|
|
|
|
|
|
worker.on('died', (error) => {
|
2022-09-25 17:03:17 +00:00
|
|
|
// 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
|
2023-01-16 15:32:31 +00:00
|
|
|
});
|
2022-09-25 17:03:17 +00:00
|
|
|
return worker;
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`ERROR | createWorker | ${error.message}`);
|
|
|
|
}
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
// We create a Worker as soon as our application starts
|
2022-10-06 12:21:54 +00:00
|
|
|
worker = createWorker();
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
// 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
|
2023-01-16 15:32:31 +00:00
|
|
|
const mediaCodecs = [
|
2022-07-23 07:32:54 +00:00
|
|
|
{
|
2023-01-16 15:32:31 +00:00
|
|
|
kind: 'audio',
|
|
|
|
mimeType: 'audio/opus',
|
|
|
|
clockRate: 48000,
|
|
|
|
channels: 2,
|
2022-07-23 07:32:54 +00:00
|
|
|
},
|
|
|
|
{
|
2023-01-16 15:32:31 +00:00
|
|
|
kind: 'video',
|
|
|
|
mimeType: 'video/VP8',
|
|
|
|
clockRate: 90000,
|
|
|
|
parameters: {
|
|
|
|
'x-google-start-bitrate': 1000,
|
2022-07-23 07:32:54 +00:00
|
|
|
},
|
2023-01-16 15:32:31 +00:00
|
|
|
channels: 2,
|
2022-11-29 12:19:02 +00:00
|
|
|
},
|
|
|
|
{
|
2023-01-16 15:32:31 +00:00
|
|
|
kind: 'video',
|
|
|
|
mimeType: 'video/VP9',
|
|
|
|
clockRate: 90000,
|
|
|
|
parameters: {
|
|
|
|
'profile-id': 2,
|
|
|
|
'x-google-start-bitrate': 1000,
|
|
|
|
},
|
2022-07-23 07:32:54 +00:00
|
|
|
},
|
2022-11-29 12:19:02 +00:00
|
|
|
{
|
2023-01-16 15:32:31 +00:00
|
|
|
kind: 'video',
|
|
|
|
mimeType: 'video/h264',
|
|
|
|
clockRate: 90000,
|
|
|
|
parameters: {
|
|
|
|
'packetization-mode': 1,
|
|
|
|
'profile-level-id': '4d0032',
|
|
|
|
'level-asymmetry-allowed': 1,
|
|
|
|
'x-google-start-bitrate': 1000,
|
|
|
|
},
|
2022-11-29 12:19:02 +00:00
|
|
|
},
|
|
|
|
{
|
2023-01-16 15:32:31 +00:00
|
|
|
kind: 'video',
|
|
|
|
mimeType: 'video/h264',
|
|
|
|
clockRate: 90000,
|
|
|
|
parameters: {
|
|
|
|
'packetization-mode': 1,
|
|
|
|
'profile-level-id': '42e01f',
|
|
|
|
'level-asymmetry-allowed': 1,
|
|
|
|
'x-google-start-bitrate': 1000,
|
|
|
|
},
|
|
|
|
},
|
2022-10-06 12:21:54 +00:00
|
|
|
];
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-09-15 14:07:47 +00:00
|
|
|
const closeCall = (callId) => {
|
2022-09-25 17:03:17 +00:00
|
|
|
try {
|
2022-10-18 07:51:20 +00:00
|
|
|
if (callId && videoCalls[callId]) {
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].receiverVideoProducer?.close();
|
|
|
|
videoCalls[callId].receiverAudioProducer?.close();
|
|
|
|
videoCalls[callId].initiatorConsumerVideo?.close();
|
|
|
|
videoCalls[callId].initiatorConsumerAudio?.close();
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId]?.initiatorConsumerTransport?.close();
|
|
|
|
videoCalls[callId]?.receiverProducerTransport?.close();
|
2022-09-27 10:03:32 +00:00
|
|
|
videoCalls[callId]?.router?.close();
|
2022-09-25 17:03:17 +00:00
|
|
|
delete videoCalls[callId];
|
2022-09-27 09:43:07 +00:00
|
|
|
} else {
|
|
|
|
console.log(`The call with id ${callId} has already been deleted`);
|
2022-09-25 17:03:17 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`);
|
2022-09-15 14:07:47 +00:00
|
|
|
}
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-09-15 14:07:47 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
|
|
|
- Handlers for WS events
|
|
|
|
- These are created only when we have a connection with a peer
|
|
|
|
*/
|
2023-01-16 15:32:31 +00:00
|
|
|
peers.on('connection', async (socket) => {
|
2022-09-25 17:03:17 +00:00
|
|
|
console.log('[connection] socketId:', socket.id);
|
|
|
|
|
|
|
|
// After making the connection successfully, we send the client a 'connection-success' event
|
2022-07-29 09:22:35 +00:00
|
|
|
socket.emit('connection-success', {
|
2023-01-16 15:32:31 +00:00
|
|
|
socketId: socket.id,
|
2022-09-25 17:03:17 +00:00
|
|
|
});
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
// It is triggered when the peer is disconnected
|
2022-07-23 07:32:54 +00:00
|
|
|
socket.on('disconnect', () => {
|
2022-09-27 09:43:07 +00:00
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log(`disconnect | socket ${socket.id} | callId ${callId}`);
|
2022-09-15 06:19:04 +00:00
|
|
|
delete socketDetails[socket.id];
|
2022-09-27 09:43:07 +00:00
|
|
|
closeCall(callId);
|
2022-09-25 17:03:17 +00:00
|
|
|
});
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
|
|
|
- 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
|
|
|
|
*/
|
2022-07-27 06:55:07 +00:00
|
|
|
socket.on('createRoom', async ({ callId }, callback) => {
|
2022-10-24 19:11:14 +00:00
|
|
|
let callbackResponse = null;
|
2022-09-25 17:03:17 +00:00
|
|
|
try {
|
2022-10-24 19:11:14 +00:00
|
|
|
// We can continue with the room creation process only if we have a callId
|
2022-09-25 17:03:17 +00:00
|
|
|
if (callId) {
|
|
|
|
console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`);
|
|
|
|
if (!videoCalls[callId]) {
|
2023-01-13 18:11:24 +00:00
|
|
|
videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) };
|
2022-09-25 17:03:17 +00:00
|
|
|
console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
|
2023-01-13 18:11:24 +00:00
|
|
|
videoCalls[callId].receiverSocket = socket;
|
2022-12-19 17:31:33 +00:00
|
|
|
} else {
|
2023-01-13 18:11:24 +00:00
|
|
|
videoCalls[callId].initiatorSocket = socket;
|
2022-09-25 17:03:17 +00:00
|
|
|
}
|
|
|
|
socketDetails[socket.id] = callId;
|
2022-10-24 19:11:14 +00:00
|
|
|
// rtpCapabilities is set for callback
|
|
|
|
console.log('[getRtpCapabilities] callId', callId);
|
2023-01-16 15:32:31 +00:00
|
|
|
callbackResponse = {
|
|
|
|
rtpCapabilities: videoCalls[callId].router.rtpCapabilities,
|
2022-10-24 19:16:47 +00:00
|
|
|
};
|
2022-09-25 17:03:17 +00:00
|
|
|
} else {
|
|
|
|
console.log(`[createRoom] missing callId ${callId}`);
|
2022-09-15 14:07:47 +00:00
|
|
|
}
|
2022-09-25 17:03:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`);
|
2022-10-24 19:11:14 +00:00
|
|
|
} finally {
|
|
|
|
callback(callbackResponse);
|
2022-07-27 06:55:07 +00:00
|
|
|
}
|
2022-09-25 17:03:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
- Client emits a request to create server side Transport
|
2022-12-19 17:31:33 +00:00
|
|
|
- Depending on the sender, a producer or consumer is created is created on that router
|
2022-09-25 17:03:17 +00:00
|
|
|
- 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];
|
2022-12-19 17:31:33 +00:00
|
|
|
console.log(`[createWebRtcTransport] socket ${socket.id} | sender ${sender} | callId ${callId}`);
|
2022-09-27 09:43:07 +00:00
|
|
|
if (sender) {
|
2023-01-16 15:32:31 +00:00
|
|
|
if (!videoCalls[callId].receiverProducerTransport && !isInitiator(callId, socket.id)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].receiverProducerTransport = await createWebRtcTransportLayer(callId, callback);
|
2023-01-16 15:32:31 +00:00
|
|
|
} else if (!videoCalls[callId].initiatorProducerTransport && isInitiator(callId, socket.id)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].initiatorProducerTransport = await createWebRtcTransportLayer(callId, callback);
|
2022-09-27 09:43:07 +00:00
|
|
|
} else {
|
|
|
|
console.log(`producerTransport has already been defined | callId ${callId}`);
|
2022-10-24 19:11:14 +00:00
|
|
|
callback(null);
|
2022-09-27 09:43:07 +00:00
|
|
|
}
|
|
|
|
} else if (!sender) {
|
2023-01-16 15:32:31 +00:00
|
|
|
if (!videoCalls[callId].receiverConsumerTransport && !isInitiator(callId, socket.id)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].receiverConsumerTransport = await createWebRtcTransportLayer(callId, callback);
|
2023-01-16 15:32:31 +00:00
|
|
|
} else if (!videoCalls[callId].initiatorConsumerTransport && isInitiator(callId, socket.id)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].initiatorConsumerTransport = await createWebRtcTransportLayer(callId, callback);
|
2022-09-27 09:43:07 +00:00
|
|
|
}
|
2022-09-25 17:03:17 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(
|
|
|
|
`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`
|
|
|
|
);
|
2022-10-24 19:11:14 +00:00
|
|
|
callback(error);
|
2022-09-25 17:03:17 +00:00
|
|
|
}
|
|
|
|
});
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
|
|
|
- The client sends this event after successfully creating a createSendTransport(AS PRODUCER)
|
|
|
|
- The connection is made to the created transport
|
|
|
|
*/
|
2022-07-27 06:55:07 +00:00
|
|
|
socket.on('transport-connect', async ({ dtlsParameters }) => {
|
2022-09-25 17:03:17 +00:00
|
|
|
try {
|
|
|
|
const callId = socketDetails[socket.id];
|
2022-09-29 11:24:44 +00:00
|
|
|
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
|
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
console.log(`[transport-connect] socket ${socket.id} | callId ${callId}`);
|
2023-01-13 18:11:24 +00:00
|
|
|
|
2023-01-16 15:32:31 +00:00
|
|
|
isInitiator(callId, socket.id)
|
|
|
|
? await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters })
|
|
|
|
: await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters });
|
2022-09-25 17:03:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
|
|
|
}
|
|
|
|
});
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
2022-12-19 17:31:33 +00:00
|
|
|
- The event sent by the client (PRODUCER) after successfully connecting to receiverProducerTransport/initiatorProducerTransport
|
|
|
|
- For the router with the id callId, we make produce on receiverProducerTransport/initiatorProducerTransport
|
2022-09-25 17:03:17 +00:00
|
|
|
- Create the handler on producer at the 'transportclose' event
|
2022-11-29 12:19:02 +00:00
|
|
|
*/
|
2022-12-19 17:31:33 +00:00
|
|
|
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
|
|
|
|
try {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
|
2023-01-16 15:32:31 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
console.log(`[transport-produce] kind: ${kind} | socket: ${socket.id} | callId: ${callId}`);
|
2023-01-16 15:32:31 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
if (kind === 'video') {
|
|
|
|
if (!isInitiator(callId, socket.id)) {
|
|
|
|
videoCalls[callId].receiverVideoProducer = await videoCalls[callId].receiverProducerTransport.produce({
|
|
|
|
kind,
|
|
|
|
rtpParameters,
|
|
|
|
});
|
|
|
|
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(
|
|
|
|
`[transport-produce] receiverVideoProducer Producer ID: ${videoCalls[callId].receiverVideoProducer.id} | kind: ${videoCalls[callId].receiverVideoProducer.kind}`
|
|
|
|
);
|
2022-12-19 17:31:33 +00:00
|
|
|
|
|
|
|
videoCalls[callId].receiverVideoProducer.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log('transport for this producer closed', callId);
|
2022-12-19 17:31:33 +00:00
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send back to the client the Producer's id
|
2023-01-16 15:32:31 +00:00
|
|
|
callback &&
|
|
|
|
callback({
|
|
|
|
id: videoCalls[callId].receiverVideoProducer.id,
|
|
|
|
});
|
2022-12-19 17:31:33 +00:00
|
|
|
} else {
|
|
|
|
videoCalls[callId].initiatorVideoProducer = await videoCalls[callId].initiatorProducerTransport.produce({
|
|
|
|
kind,
|
|
|
|
rtpParameters,
|
|
|
|
});
|
|
|
|
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(
|
|
|
|
`[transport-produce] initiatorVideoProducer Producer ID: ${videoCalls[callId].initiatorVideoProducer.id} | kind: ${videoCalls[callId].initiatorVideoProducer.kind}`
|
|
|
|
);
|
2022-12-19 17:31:33 +00:00
|
|
|
|
|
|
|
videoCalls[callId].initiatorVideoProducer.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log('transport for this producer closed', callId);
|
2022-12-19 17:31:33 +00:00
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
2023-01-16 15:32:31 +00:00
|
|
|
callback &&
|
|
|
|
callback({
|
|
|
|
id: videoCalls[callId].initiatorVideoProducer.id,
|
|
|
|
});
|
2022-12-19 17:31:33 +00:00
|
|
|
}
|
2022-11-29 12:19:02 +00:00
|
|
|
} else if (kind === 'audio') {
|
2022-12-19 17:31:33 +00:00
|
|
|
if (!isInitiator(callId, socket.id)) {
|
|
|
|
videoCalls[callId].receiverAudioProducer = await videoCalls[callId].receiverProducerTransport.produce({
|
|
|
|
kind,
|
|
|
|
rtpParameters,
|
|
|
|
});
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(
|
|
|
|
`[transport-produce] receiverAudioProducer Producer ID: ${videoCalls[callId].receiverAudioProducer.id} | kind: ${videoCalls[callId].receiverAudioProducer.kind}`
|
|
|
|
);
|
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].receiverAudioProducer.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log('transport for this producer closed', callId);
|
2022-12-19 17:31:33 +00:00
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send back to the client the Producer's id
|
2023-01-16 15:32:31 +00:00
|
|
|
callback &&
|
|
|
|
callback({
|
|
|
|
id: videoCalls[callId].receiverAudioProducer.id,
|
|
|
|
});
|
2022-12-19 17:31:33 +00:00
|
|
|
} else {
|
|
|
|
videoCalls[callId].initiatorAudioProducer = await videoCalls[callId].initiatorProducerTransport.produce({
|
|
|
|
kind,
|
|
|
|
rtpParameters,
|
|
|
|
});
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(
|
|
|
|
`[transport-produce] initiatorAudioProducer Producer ID: ${videoCalls[callId].initiatorAudioProducer.id} | kind: ${videoCalls[callId].initiatorAudioProducer.kind}`
|
|
|
|
);
|
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].initiatorAudioProducer.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log('transport for this producer closed', callId);
|
2022-12-19 17:31:33 +00:00
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send back to the client the Producer's id
|
2023-01-16 15:32:31 +00:00
|
|
|
callback &&
|
|
|
|
callback({
|
|
|
|
id: videoCalls[callId].initiatorAudioProducer.id,
|
|
|
|
});
|
2022-12-19 17:31:33 +00:00
|
|
|
}
|
2022-11-29 12:19:02 +00:00
|
|
|
}
|
2023-01-18 09:49:02 +00:00
|
|
|
|
|
|
|
const socketToEmit = isInitiator(callId, socket.id)
|
|
|
|
? videoCalls[callId].receiverSocket
|
|
|
|
: videoCalls[callId].initiatorSocket;
|
|
|
|
|
2023-02-11 19:13:57 +00:00
|
|
|
socketToEmit.emit('new-producer', { callId, kind });
|
2022-09-25 17:03:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
|
|
|
|
}
|
|
|
|
});
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
|
|
|
- The client sends this event after successfully creating a createRecvTransport(AS CONSUMER)
|
|
|
|
- The connection is made to the created consumerTransport
|
|
|
|
*/
|
2022-07-27 06:55:07 +00:00
|
|
|
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
|
2022-09-25 17:03:17 +00:00
|
|
|
try {
|
|
|
|
const callId = socketDetails[socket.id];
|
2022-12-19 17:31:33 +00:00
|
|
|
console.log(`[transport-recv-connect] socket ${socket.id} | callId ${callId}`);
|
2023-01-12 14:23:30 +00:00
|
|
|
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
|
2022-12-19 17:31:33 +00:00
|
|
|
// await videoCalls[callId].consumerTransport.connect({ dtlsParameters });
|
2023-01-16 15:32:31 +00:00
|
|
|
if (!isInitiator(callId, socket.id)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
await videoCalls[callId].receiverConsumerTransport.connect({ dtlsParameters });
|
2023-01-16 15:32:31 +00:00
|
|
|
} else if (isInitiator(callId, socket.id)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
await videoCalls[callId].initiatorConsumerTransport.connect({ dtlsParameters });
|
|
|
|
}
|
2022-09-25 17:03:17 +00:00
|
|
|
} catch (error) {
|
2022-09-27 09:43:07 +00:00
|
|
|
console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
2022-09-25 17:03:17 +00:00
|
|
|
}
|
2023-01-16 15:32:31 +00:00
|
|
|
});
|
2022-07-27 06:55:07 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
|
|
|
- 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)
|
|
|
|
*/
|
2022-09-15 06:19:04 +00:00
|
|
|
socket.on('consume', async ({ rtpCapabilities }, callback) => {
|
2022-07-27 06:55:07 +00:00
|
|
|
try {
|
2022-09-25 17:03:17 +00:00
|
|
|
const callId = socketDetails[socket.id];
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(
|
|
|
|
`[consume] socket ${socket.id} | callId ${callId} | rtpCapabilities: ${JSON.stringify(rtpCapabilities)}`
|
|
|
|
);
|
2022-09-25 17:03:17 +00:00
|
|
|
|
2023-01-12 14:23:30 +00:00
|
|
|
if (typeof rtpCapabilities === 'string') rtpCapabilities = JSON.parse(rtpCapabilities);
|
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
console.log('[consume] callId', callId);
|
|
|
|
let canConsumeVideo, canConsumeAudio;
|
|
|
|
if (isInitiator(callId, socket.id)) {
|
2023-01-16 15:32:31 +00:00
|
|
|
canConsumeVideo =
|
|
|
|
!!videoCalls[callId].receiverVideoProducer &&
|
|
|
|
!!videoCalls[callId].router.canConsume({
|
|
|
|
producerId: videoCalls[callId].receiverVideoProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
});
|
|
|
|
canConsumeAudio =
|
|
|
|
!!videoCalls[callId].receiverAudioProducer &&
|
|
|
|
!!videoCalls[callId].router.canConsume({
|
|
|
|
producerId: videoCalls[callId].receiverAudioProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
});
|
2022-12-19 17:31:33 +00:00
|
|
|
} else {
|
2023-01-16 15:32:31 +00:00
|
|
|
canConsumeVideo =
|
|
|
|
!!videoCalls[callId].initiatorVideoProducer &&
|
|
|
|
!!videoCalls[callId].router.canConsume({
|
|
|
|
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
});
|
|
|
|
|
|
|
|
canConsumeAudio =
|
|
|
|
!!videoCalls[callId].initiatorAudioProducer &&
|
|
|
|
!!videoCalls[callId].router.canConsume({
|
|
|
|
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
});
|
2022-12-19 17:31:33 +00:00
|
|
|
}
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('[consume] canConsumeVideo', canConsumeVideo);
|
|
|
|
console.log('[consume] canConsumeAudio', canConsumeAudio);
|
|
|
|
|
|
|
|
if (canConsumeVideo && !canConsumeAudio) {
|
2023-01-16 15:32:31 +00:00
|
|
|
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities);
|
2022-11-29 12:19:02 +00:00
|
|
|
callback({ videoParams, audioParams: null });
|
|
|
|
} else if (canConsumeVideo && canConsumeAudio) {
|
2023-01-16 15:32:31 +00:00
|
|
|
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities);
|
|
|
|
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities);
|
2022-11-29 12:19:02 +00:00
|
|
|
callback({ videoParams, audioParams });
|
2022-12-19 17:31:33 +00:00
|
|
|
} else if (!canConsumeVideo && canConsumeAudio) {
|
2023-01-16 15:32:31 +00:00
|
|
|
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities);
|
2022-12-19 17:31:33 +00:00
|
|
|
const data = { videoParams: null, audioParams };
|
|
|
|
callback(data);
|
2022-09-13 18:33:04 +00:00
|
|
|
} else {
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log(`[consume] Can't consume | callId ${callId}`);
|
2022-10-24 19:11:14 +00:00
|
|
|
callback(null);
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
2022-07-27 06:55:07 +00:00
|
|
|
} catch (error) {
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`);
|
2022-09-25 17:03:17 +00:00
|
|
|
callback({ params: { error } });
|
2022-07-27 06:55:07 +00:00
|
|
|
}
|
2022-09-25 17:03:17 +00:00
|
|
|
});
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
|
|
|
- Event sent by the consumer after consuming to resume the pause
|
2023-01-12 10:44:11 +00:00
|
|
|
- When consuming on consumerTransport, it is initially done with paused: true, here we will resume
|
|
|
|
- For the initiator we resume the initiatorConsumerAUDIO/VIDEO and for receiver the receiverConsumerAUDIO/VIDEO
|
2022-09-25 17:03:17 +00:00
|
|
|
*/
|
2023-01-16 15:32:31 +00:00
|
|
|
socket.on('consumer-resume', () => {
|
2022-09-25 17:03:17 +00:00
|
|
|
try {
|
|
|
|
const callId = socketDetails[socket.id];
|
2023-01-16 15:32:31 +00:00
|
|
|
console.log(`[consumer-resume] callId ${callId}`);
|
2022-12-19 17:31:33 +00:00
|
|
|
if (isInitiator(callId, socket.id)) {
|
2023-01-12 12:32:48 +00:00
|
|
|
videoCalls[callId]?.initiatorConsumerVideo?.resume();
|
|
|
|
videoCalls[callId]?.initiatorConsumerAudio?.resume();
|
2022-12-19 17:31:33 +00:00
|
|
|
} else {
|
2023-01-12 12:32:48 +00:00
|
|
|
videoCalls[callId]?.receiverConsumerVideo?.resume();
|
|
|
|
videoCalls[callId]?.receiverConsumerAudio?.resume();
|
2022-12-19 17:31:33 +00:00
|
|
|
}
|
2022-09-25 17:03:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`ERROR | consumer-resume | callId ${socketDetails[socket.id]} | ${error.message}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
const consumeVideo = async (callId, socketId, rtpCapabilities) => {
|
2023-01-16 15:32:31 +00:00
|
|
|
if (isInitiator(callId, socketId)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].initiatorConsumerVideo = await videoCalls[callId].initiatorConsumerTransport.consume({
|
|
|
|
producerId: videoCalls[callId].receiverVideoProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
paused: true,
|
|
|
|
});
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
|
|
|
|
videoCalls[callId].initiatorConsumerVideo.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('transport close from consumer', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
|
|
|
videoCalls[callId].initiatorConsumerVideo.on('producerclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('producer of consumer closed', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: videoCalls[callId].initiatorConsumerVideo.id,
|
|
|
|
producerId: videoCalls[callId].receiverVideoProducer.id,
|
|
|
|
kind: 'video',
|
|
|
|
rtpParameters: videoCalls[callId].initiatorConsumerVideo.rtpParameters,
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-12-19 17:31:33 +00:00
|
|
|
} else {
|
|
|
|
videoCalls[callId].receiverConsumerVideo = await videoCalls[callId].receiverConsumerTransport.consume({
|
|
|
|
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
paused: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
videoCalls[callId].receiverConsumerVideo.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('transport close from consumer', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].receiverConsumerVideo.on('producerclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('producer of consumer closed', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: videoCalls[callId].receiverConsumerVideo.id,
|
|
|
|
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
|
|
|
kind: 'video',
|
|
|
|
rtpParameters: videoCalls[callId].receiverConsumerVideo.rtpParameters,
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-11-29 12:19:02 +00:00
|
|
|
}
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
const consumeAudio = async (callId, socketId, rtpCapabilities) => {
|
2023-01-16 15:32:31 +00:00
|
|
|
if (isInitiator(callId, socketId)) {
|
2022-12-19 17:31:33 +00:00
|
|
|
videoCalls[callId].initiatorConsumerAudio = await videoCalls[callId].initiatorConsumerTransport.consume({
|
|
|
|
producerId: videoCalls[callId].receiverAudioProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
paused: true,
|
|
|
|
});
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
|
|
|
|
videoCalls[callId].initiatorConsumerAudio.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('transport close from consumer', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
|
|
|
videoCalls[callId].initiatorConsumerAudio.on('producerclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('producer of consumer closed', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: videoCalls[callId].initiatorConsumerAudio.id,
|
|
|
|
producerId: videoCalls[callId].receiverAudioProducer.id,
|
|
|
|
kind: 'audio',
|
|
|
|
rtpParameters: videoCalls[callId].initiatorConsumerAudio.rtpParameters,
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-12-19 17:31:33 +00:00
|
|
|
} else {
|
|
|
|
videoCalls[callId].receiverConsumerAudio = await videoCalls[callId].receiverConsumerTransport.consume({
|
|
|
|
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
|
|
|
rtpCapabilities,
|
|
|
|
paused: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
videoCalls[callId].receiverConsumerAudio.on('transportclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('transport close from consumer', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
videoCalls[callId].receiverConsumerAudio.on('producerclose', () => {
|
|
|
|
const callId = socketDetails[socket.id];
|
|
|
|
console.log('producer of consumer closed', callId);
|
|
|
|
closeCall(callId);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: videoCalls[callId].receiverConsumerAudio.id,
|
|
|
|
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
|
|
|
kind: 'audio',
|
|
|
|
rtpParameters: videoCalls[callId].receiverConsumerAudio.rtpParameters,
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-11-29 12:19:02 +00:00
|
|
|
}
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-12-19 17:31:33 +00:00
|
|
|
const isInitiator = (callId, socketId) => {
|
2023-01-16 15:32:31 +00:00
|
|
|
return videoCalls[callId]?.initiatorSocket?.id === socketId;
|
|
|
|
};
|
2022-12-19 17:31:33 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
/*
|
|
|
|
- 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)
|
|
|
|
*/
|
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).
|
2023-01-16 15:32:31 +00:00
|
|
|
},
|
2022-07-23 07:32:54 +00:00
|
|
|
],
|
|
|
|
enableUdp: true,
|
|
|
|
enableTcp: true,
|
|
|
|
preferUdp: true,
|
2022-09-25 17:03:17 +00:00
|
|
|
};
|
2023-01-16 15:32:31 +00:00
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
|
2023-01-16 15:32:31 +00:00
|
|
|
let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options);
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
// Handler for when DTLS(Datagram Transport Layer Security) changes
|
2023-01-16 15:32:31 +00:00
|
|
|
transport.on('dtlsstatechange', (dtlsState) => {
|
2022-09-25 17:03:17 +00:00
|
|
|
console.log(`transport | dtlsstatechange | calldId ${callId} | dtlsState ${dtlsState}`);
|
2022-07-23 07:32:54 +00:00
|
|
|
if (dtlsState === 'closed') {
|
2022-09-25 17:03:17 +00:00
|
|
|
transport.close();
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
2022-09-25 17:03:17 +00:00
|
|
|
});
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
// Handler if the transport layer has closed (for various reasons)
|
2022-07-23 07:32:54 +00:00
|
|
|
transport.on('close', () => {
|
2022-09-25 17:03:17 +00:00
|
|
|
console.log(`transport | closed | calldId ${callId}`);
|
|
|
|
});
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-08-16 11:21:02 +00:00
|
|
|
const params = {
|
|
|
|
id: transport.id,
|
|
|
|
iceParameters: transport.iceParameters,
|
|
|
|
iceCandidates: transport.iceCandidates,
|
|
|
|
dtlsParameters: transport.dtlsParameters,
|
2022-09-25 17:03:17 +00:00
|
|
|
};
|
2022-08-16 11:21:02 +00:00
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('[createWebRtcTransportLayer] callback params', params);
|
2022-09-25 17:03:17 +00:00
|
|
|
// Send back to the client the params
|
|
|
|
callback({ params });
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-09-25 17:03:17 +00:00
|
|
|
// Set transport to producerTransport or consumerTransport
|
|
|
|
return transport;
|
2022-07-23 07:32:54 +00:00
|
|
|
} catch (error) {
|
2022-09-27 10:13:29 +00:00
|
|
|
console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
|
2022-09-25 17:03:17 +00:00
|
|
|
callback({ params: { error } });
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
2023-01-16 15:32:31 +00:00
|
|
|
};
|