LAPI-675: Refactor transport-connect new-producer
This commit is contained in:
parent
0f4342777a
commit
fb99fd57f8
307
app.js
307
app.js
@ -1,4 +1,4 @@
|
|||||||
require('dotenv').config()
|
require('dotenv').config();
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -15,7 +15,7 @@ const mediasoup = require('mediasoup');
|
|||||||
|
|
||||||
let worker;
|
let worker;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* videoCalls - Dictionary of Object(s)
|
* videoCalls - Dictionary of Object(s)
|
||||||
* '<callId>': {
|
* '<callId>': {
|
||||||
* router: Router,
|
* router: Router,
|
||||||
@ -31,13 +31,13 @@ let worker;
|
|||||||
* initiatorSocket
|
* initiatorSocket
|
||||||
* receiverSocket
|
* receiverSocket
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
let videoCalls = {};
|
let videoCalls = {};
|
||||||
let socketDetails = {};
|
let socketDetails = {};
|
||||||
|
|
||||||
app.get('/', (_req, res) => {
|
app.get('/', (_req, res) => {
|
||||||
res.send('Hello from mediasoup app!')
|
res.send('Hello from mediasoup app!');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use('/sfu', express.static(path.join(__dirname, 'public')));
|
app.use('/sfu', express.static(path.join(__dirname, 'public')));
|
||||||
@ -52,7 +52,7 @@ const httpsServer = https.createServer(options, app);
|
|||||||
|
|
||||||
const io = new Server(httpsServer, {
|
const io = new Server(httpsServer, {
|
||||||
allowEIO3: true,
|
allowEIO3: true,
|
||||||
origins: ["*:*"]
|
origins: ['*:*'],
|
||||||
});
|
});
|
||||||
|
|
||||||
httpsServer.listen(process.env.PORT, () => {
|
httpsServer.listen(process.env.PORT, () => {
|
||||||
@ -66,19 +66,19 @@ const createWorker = async () => {
|
|||||||
worker = await mediasoup.createWorker({
|
worker = await mediasoup.createWorker({
|
||||||
rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
|
rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
|
||||||
rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
|
rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
|
||||||
})
|
});
|
||||||
console.log(`[createWorker] worker pid ${worker.pid}`);
|
console.log(`[createWorker] worker pid ${worker.pid}`);
|
||||||
|
|
||||||
worker.on('died', error => {
|
worker.on('died', (error) => {
|
||||||
// This implies something serious happened, so kill the application
|
// This implies something serious happened, so kill the application
|
||||||
console.error('mediasoup worker has died', error);
|
console.error('mediasoup worker has died', error);
|
||||||
setTimeout(() => process.exit(1), 2000); // exit in 2 seconds
|
setTimeout(() => process.exit(1), 2000); // exit in 2 seconds
|
||||||
})
|
});
|
||||||
return worker;
|
return worker;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | createWorker | ${error.message}`);
|
console.log(`ERROR | createWorker | ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// We create a Worker as soon as our application starts
|
// We create a Worker as soon as our application starts
|
||||||
worker = createWorker();
|
worker = createWorker();
|
||||||
@ -87,57 +87,53 @@ worker = createWorker();
|
|||||||
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
|
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
|
||||||
// list of media codecs supported by mediasoup ...
|
// list of media codecs supported by mediasoup ...
|
||||||
// https://github.com/versatica/mediasoup/blob/v3/src/supportedRtpCapabilities.ts
|
// https://github.com/versatica/mediasoup/blob/v3/src/supportedRtpCapabilities.ts
|
||||||
const mediaCodecs = [
|
const mediaCodecs = [
|
||||||
{
|
{
|
||||||
kind : 'audio',
|
kind: 'audio',
|
||||||
mimeType : 'audio/opus',
|
mimeType: 'audio/opus',
|
||||||
clockRate : 48000,
|
clockRate: 48000,
|
||||||
channels : 2
|
channels: 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind : 'video',
|
kind: 'video',
|
||||||
mimeType : 'video/VP8',
|
mimeType: 'video/VP8',
|
||||||
clockRate : 90000,
|
clockRate: 90000,
|
||||||
parameters :
|
parameters: {
|
||||||
{
|
'x-google-start-bitrate': 1000,
|
||||||
'x-google-start-bitrate' : 1000
|
|
||||||
},
|
},
|
||||||
channels : 2
|
channels: 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind : 'video',
|
kind: 'video',
|
||||||
mimeType : 'video/VP9',
|
mimeType: 'video/VP9',
|
||||||
clockRate : 90000,
|
clockRate: 90000,
|
||||||
parameters :
|
parameters: {
|
||||||
{
|
'profile-id': 2,
|
||||||
'profile-id' : 2,
|
'x-google-start-bitrate': 1000,
|
||||||
'x-google-start-bitrate' : 1000
|
},
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind : 'video',
|
kind: 'video',
|
||||||
mimeType : 'video/h264',
|
mimeType: 'video/h264',
|
||||||
clockRate : 90000,
|
clockRate: 90000,
|
||||||
parameters :
|
parameters: {
|
||||||
{
|
'packetization-mode': 1,
|
||||||
'packetization-mode' : 1,
|
'profile-level-id': '4d0032',
|
||||||
'profile-level-id' : '4d0032',
|
'level-asymmetry-allowed': 1,
|
||||||
'level-asymmetry-allowed' : 1,
|
'x-google-start-bitrate': 1000,
|
||||||
'x-google-start-bitrate' : 1000
|
},
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind : 'video',
|
kind: 'video',
|
||||||
mimeType : 'video/h264',
|
mimeType: 'video/h264',
|
||||||
clockRate : 90000,
|
clockRate: 90000,
|
||||||
parameters :
|
parameters: {
|
||||||
{
|
'packetization-mode': 1,
|
||||||
'packetization-mode' : 1,
|
'profile-level-id': '42e01f',
|
||||||
'profile-level-id' : '42e01f',
|
'level-asymmetry-allowed': 1,
|
||||||
'level-asymmetry-allowed' : 1,
|
'x-google-start-bitrate': 1000,
|
||||||
'x-google-start-bitrate' : 1000
|
},
|
||||||
}
|
},
|
||||||
}
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const closeCall = (callId) => {
|
const closeCall = (callId) => {
|
||||||
@ -158,18 +154,18 @@ const closeCall = (callId) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`);
|
console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
- Handlers for WS events
|
- Handlers for WS events
|
||||||
- These are created only when we have a connection with a peer
|
- These are created only when we have a connection with a peer
|
||||||
*/
|
*/
|
||||||
peers.on('connection', async socket => {
|
peers.on('connection', async (socket) => {
|
||||||
console.log('[connection] socketId:', socket.id);
|
console.log('[connection] socketId:', socket.id);
|
||||||
|
|
||||||
// After making the connection successfully, we send the client a 'connection-success' event
|
// After making the connection successfully, we send the client a 'connection-success' event
|
||||||
socket.emit('connection-success', {
|
socket.emit('connection-success', {
|
||||||
socketId: socket.id
|
socketId: socket.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
// It is triggered when the peer is disconnected
|
// It is triggered when the peer is disconnected
|
||||||
@ -201,8 +197,8 @@ peers.on('connection', async socket => {
|
|||||||
socketDetails[socket.id] = callId;
|
socketDetails[socket.id] = callId;
|
||||||
// rtpCapabilities is set for callback
|
// rtpCapabilities is set for callback
|
||||||
console.log('[getRtpCapabilities] callId', callId);
|
console.log('[getRtpCapabilities] callId', callId);
|
||||||
callbackResponse = {
|
callbackResponse = {
|
||||||
rtpCapabilities :videoCalls[callId].router.rtpCapabilities
|
rtpCapabilities: videoCalls[callId].router.rtpCapabilities,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
console.log(`[createRoom] missing callId ${callId}`);
|
console.log(`[createRoom] missing callId ${callId}`);
|
||||||
@ -227,23 +223,25 @@ peers.on('connection', async socket => {
|
|||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log(`[createWebRtcTransport] socket ${socket.id} | sender ${sender} | callId ${callId}`);
|
console.log(`[createWebRtcTransport] socket ${socket.id} | sender ${sender} | callId ${callId}`);
|
||||||
if (sender) {
|
if (sender) {
|
||||||
if(!videoCalls[callId].receiverProducerTransport && !isInitiator(callId, socket.id)) {
|
if (!videoCalls[callId].receiverProducerTransport && !isInitiator(callId, socket.id)) {
|
||||||
videoCalls[callId].receiverProducerTransport = await createWebRtcTransportLayer(callId, callback);
|
videoCalls[callId].receiverProducerTransport = await createWebRtcTransportLayer(callId, callback);
|
||||||
} else if(!videoCalls[callId].initiatorProducerTransport && isInitiator(callId, socket.id)) {
|
} else if (!videoCalls[callId].initiatorProducerTransport && isInitiator(callId, socket.id)) {
|
||||||
videoCalls[callId].initiatorProducerTransport = await createWebRtcTransportLayer(callId, callback);
|
videoCalls[callId].initiatorProducerTransport = await createWebRtcTransportLayer(callId, callback);
|
||||||
} else {
|
} else {
|
||||||
console.log(`producerTransport has already been defined | callId ${callId}`);
|
console.log(`producerTransport has already been defined | callId ${callId}`);
|
||||||
callback(null);
|
callback(null);
|
||||||
}
|
}
|
||||||
} else if (!sender) {
|
} else if (!sender) {
|
||||||
if(!videoCalls[callId].receiverConsumerTransport && !isInitiator(callId, socket.id)) {
|
if (!videoCalls[callId].receiverConsumerTransport && !isInitiator(callId, socket.id)) {
|
||||||
videoCalls[callId].receiverConsumerTransport = await createWebRtcTransportLayer(callId, callback);
|
videoCalls[callId].receiverConsumerTransport = await createWebRtcTransportLayer(callId, callback);
|
||||||
} else if(!videoCalls[callId].initiatorConsumerTransport && isInitiator(callId, socket.id)) {
|
} else if (!videoCalls[callId].initiatorConsumerTransport && isInitiator(callId, socket.id)) {
|
||||||
videoCalls[callId].initiatorConsumerTransport = await createWebRtcTransportLayer(callId, callback);
|
videoCalls[callId].initiatorConsumerTransport = await createWebRtcTransportLayer(callId, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
|
console.log(
|
||||||
|
`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`
|
||||||
|
);
|
||||||
callback(error);
|
callback(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -259,18 +257,26 @@ peers.on('connection', async socket => {
|
|||||||
|
|
||||||
console.log(`[transport-connect] socket ${socket.id} | callId ${callId}`);
|
console.log(`[transport-connect] socket ${socket.id} | callId ${callId}`);
|
||||||
|
|
||||||
if (!isInitiator(callId, socket.id)) {
|
isInitiator(callId, socket.id)
|
||||||
await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters });
|
? await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters })
|
||||||
videoCalls[callId].initiatorSocket.emit('new-producer', {
|
: await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters });
|
||||||
callId
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters });
|
|
||||||
videoCalls[callId].receiverSocket.emit('new-producer', {
|
|
||||||
callId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const socketToEmit = isInitiator(callId, socket.id)
|
||||||
|
? videoCalls[callId].receiverSocket
|
||||||
|
: videoCalls[callId].initiatorSocket;
|
||||||
|
socketToEmit.emit('new-producer', { callId });
|
||||||
|
|
||||||
|
// if (!isInitiator(callId, socket.id)) {
|
||||||
|
// await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters });
|
||||||
|
// videoCalls[callId].initiatorSocket.emit('new-producer', {
|
||||||
|
// callId,
|
||||||
|
// });
|
||||||
|
// } else {
|
||||||
|
// await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters });
|
||||||
|
// videoCalls[callId].receiverSocket.emit('new-producer', {
|
||||||
|
// callId,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
}
|
}
|
||||||
@ -285,9 +291,9 @@ peers.on('connection', async socket => {
|
|||||||
try {
|
try {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
|
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
|
||||||
|
|
||||||
console.log(`[transport-produce] kind: ${kind} | socket: ${socket.id} | callId: ${callId}`);
|
console.log(`[transport-produce] kind: ${kind} | socket: ${socket.id} | callId: ${callId}`);
|
||||||
|
|
||||||
if (kind === 'video') {
|
if (kind === 'video') {
|
||||||
if (!isInitiator(callId, socket.id)) {
|
if (!isInitiator(callId, socket.id)) {
|
||||||
videoCalls[callId].receiverVideoProducer = await videoCalls[callId].receiverProducerTransport.produce({
|
videoCalls[callId].receiverVideoProducer = await videoCalls[callId].receiverProducerTransport.produce({
|
||||||
@ -295,35 +301,41 @@ peers.on('connection', async socket => {
|
|||||||
rtpParameters,
|
rtpParameters,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`[transport-produce] receiverVideoProducer Producer ID: ${videoCalls[callId].receiverVideoProducer.id} | kind: ${videoCalls[callId].receiverVideoProducer.kind}`);
|
console.log(
|
||||||
|
`[transport-produce] receiverVideoProducer Producer ID: ${videoCalls[callId].receiverVideoProducer.id} | kind: ${videoCalls[callId].receiverVideoProducer.kind}`
|
||||||
|
);
|
||||||
|
|
||||||
videoCalls[callId].receiverVideoProducer.on('transportclose', () => {
|
videoCalls[callId].receiverVideoProducer.on('transportclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('transport for this producer closed', callId)
|
console.log('transport for this producer closed', callId);
|
||||||
closeCall(callId);
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send back to the client the Producer's id
|
// Send back to the client the Producer's id
|
||||||
callback && callback({
|
callback &&
|
||||||
id: videoCalls[callId].receiverVideoProducer.id
|
callback({
|
||||||
});
|
id: videoCalls[callId].receiverVideoProducer.id,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
videoCalls[callId].initiatorVideoProducer = await videoCalls[callId].initiatorProducerTransport.produce({
|
videoCalls[callId].initiatorVideoProducer = await videoCalls[callId].initiatorProducerTransport.produce({
|
||||||
kind,
|
kind,
|
||||||
rtpParameters,
|
rtpParameters,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`[transport-produce] initiatorVideoProducer Producer ID: ${videoCalls[callId].initiatorVideoProducer.id} | kind: ${videoCalls[callId].initiatorVideoProducer.kind}`);
|
console.log(
|
||||||
|
`[transport-produce] initiatorVideoProducer Producer ID: ${videoCalls[callId].initiatorVideoProducer.id} | kind: ${videoCalls[callId].initiatorVideoProducer.kind}`
|
||||||
|
);
|
||||||
|
|
||||||
videoCalls[callId].initiatorVideoProducer.on('transportclose', () => {
|
videoCalls[callId].initiatorVideoProducer.on('transportclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('transport for this producer closed', callId)
|
console.log('transport for this producer closed', callId);
|
||||||
closeCall(callId);
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
callback && callback({
|
callback &&
|
||||||
id: videoCalls[callId].initiatorVideoProducer.id
|
callback({
|
||||||
});
|
id: videoCalls[callId].initiatorVideoProducer.id,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else if (kind === 'audio') {
|
} else if (kind === 'audio') {
|
||||||
if (!isInitiator(callId, socket.id)) {
|
if (!isInitiator(callId, socket.id)) {
|
||||||
@ -332,36 +344,42 @@ peers.on('connection', async socket => {
|
|||||||
rtpParameters,
|
rtpParameters,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`[transport-produce] receiverAudioProducer Producer ID: ${videoCalls[callId].receiverAudioProducer.id} | kind: ${videoCalls[callId].receiverAudioProducer.kind}`);
|
console.log(
|
||||||
|
`[transport-produce] receiverAudioProducer Producer ID: ${videoCalls[callId].receiverAudioProducer.id} | kind: ${videoCalls[callId].receiverAudioProducer.kind}`
|
||||||
|
);
|
||||||
|
|
||||||
videoCalls[callId].receiverAudioProducer.on('transportclose', () => {
|
videoCalls[callId].receiverAudioProducer.on('transportclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('transport for this producer closed', callId)
|
console.log('transport for this producer closed', callId);
|
||||||
closeCall(callId);
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send back to the client the Producer's id
|
// Send back to the client the Producer's id
|
||||||
callback && callback({
|
callback &&
|
||||||
id: videoCalls[callId].receiverAudioProducer.id
|
callback({
|
||||||
});
|
id: videoCalls[callId].receiverAudioProducer.id,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
videoCalls[callId].initiatorAudioProducer = await videoCalls[callId].initiatorProducerTransport.produce({
|
videoCalls[callId].initiatorAudioProducer = await videoCalls[callId].initiatorProducerTransport.produce({
|
||||||
kind,
|
kind,
|
||||||
rtpParameters,
|
rtpParameters,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`[transport-produce] initiatorAudioProducer Producer ID: ${videoCalls[callId].initiatorAudioProducer.id} | kind: ${videoCalls[callId].initiatorAudioProducer.kind}`);
|
console.log(
|
||||||
|
`[transport-produce] initiatorAudioProducer Producer ID: ${videoCalls[callId].initiatorAudioProducer.id} | kind: ${videoCalls[callId].initiatorAudioProducer.kind}`
|
||||||
|
);
|
||||||
|
|
||||||
videoCalls[callId].initiatorAudioProducer.on('transportclose', () => {
|
videoCalls[callId].initiatorAudioProducer.on('transportclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('transport for this producer closed', callId)
|
console.log('transport for this producer closed', callId);
|
||||||
closeCall(callId);
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send back to the client the Producer's id
|
// Send back to the client the Producer's id
|
||||||
callback && callback({
|
callback &&
|
||||||
id: videoCalls[callId].initiatorAudioProducer.id
|
callback({
|
||||||
});
|
id: videoCalls[callId].initiatorAudioProducer.id,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -379,15 +397,15 @@ peers.on('connection', async socket => {
|
|||||||
console.log(`[transport-recv-connect] socket ${socket.id} | callId ${callId}`);
|
console.log(`[transport-recv-connect] socket ${socket.id} | callId ${callId}`);
|
||||||
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
|
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
|
||||||
// await videoCalls[callId].consumerTransport.connect({ dtlsParameters });
|
// await videoCalls[callId].consumerTransport.connect({ dtlsParameters });
|
||||||
if(!isInitiator(callId, socket.id)) {
|
if (!isInitiator(callId, socket.id)) {
|
||||||
await videoCalls[callId].receiverConsumerTransport.connect({ dtlsParameters });
|
await videoCalls[callId].receiverConsumerTransport.connect({ dtlsParameters });
|
||||||
} else if(isInitiator(callId, socket.id)) {
|
} else if (isInitiator(callId, socket.id)) {
|
||||||
await videoCalls[callId].initiatorConsumerTransport.connect({ dtlsParameters });
|
await videoCalls[callId].initiatorConsumerTransport.connect({ dtlsParameters });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
- The customer consumes after successfully connecting to consumerTransport
|
- The customer consumes after successfully connecting to consumerTransport
|
||||||
@ -399,45 +417,54 @@ peers.on('connection', async socket => {
|
|||||||
socket.on('consume', async ({ rtpCapabilities }, callback) => {
|
socket.on('consume', async ({ rtpCapabilities }, callback) => {
|
||||||
try {
|
try {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log(`[consume] socket ${socket.id} | callId ${callId} | rtpCapabilities: ${JSON.stringify(rtpCapabilities)}`);
|
console.log(
|
||||||
|
`[consume] socket ${socket.id} | callId ${callId} | rtpCapabilities: ${JSON.stringify(rtpCapabilities)}`
|
||||||
|
);
|
||||||
|
|
||||||
if (typeof rtpCapabilities === 'string') rtpCapabilities = JSON.parse(rtpCapabilities);
|
if (typeof rtpCapabilities === 'string') rtpCapabilities = JSON.parse(rtpCapabilities);
|
||||||
|
|
||||||
console.log('[consume] callId', callId);
|
console.log('[consume] callId', callId);
|
||||||
let canConsumeVideo, canConsumeAudio;
|
let canConsumeVideo, canConsumeAudio;
|
||||||
if (isInitiator(callId, socket.id)) {
|
if (isInitiator(callId, socket.id)) {
|
||||||
canConsumeVideo = !!videoCalls[callId].receiverVideoProducer && !!videoCalls[callId].router.canConsume({
|
canConsumeVideo =
|
||||||
producerId: videoCalls[callId].receiverVideoProducer.id,
|
!!videoCalls[callId].receiverVideoProducer &&
|
||||||
rtpCapabilities
|
!!videoCalls[callId].router.canConsume({
|
||||||
});
|
producerId: videoCalls[callId].receiverVideoProducer.id,
|
||||||
canConsumeAudio = !!videoCalls[callId].receiverAudioProducer && !!videoCalls[callId].router.canConsume({
|
rtpCapabilities,
|
||||||
producerId: videoCalls[callId].receiverAudioProducer.id,
|
});
|
||||||
rtpCapabilities
|
canConsumeAudio =
|
||||||
});
|
!!videoCalls[callId].receiverAudioProducer &&
|
||||||
|
!!videoCalls[callId].router.canConsume({
|
||||||
|
producerId: videoCalls[callId].receiverAudioProducer.id,
|
||||||
|
rtpCapabilities,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
canConsumeVideo = !!videoCalls[callId].initiatorVideoProducer && !!videoCalls[callId].router.canConsume({
|
canConsumeVideo =
|
||||||
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
!!videoCalls[callId].initiatorVideoProducer &&
|
||||||
rtpCapabilities
|
!!videoCalls[callId].router.canConsume({
|
||||||
});
|
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
||||||
|
rtpCapabilities,
|
||||||
|
});
|
||||||
|
|
||||||
canConsumeAudio = !!videoCalls[callId].initiatorAudioProducer && !!videoCalls[callId].router.canConsume({
|
canConsumeAudio =
|
||||||
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
!!videoCalls[callId].initiatorAudioProducer &&
|
||||||
rtpCapabilities
|
!!videoCalls[callId].router.canConsume({
|
||||||
});
|
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
||||||
|
rtpCapabilities,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
console.log('[consume] canConsumeVideo', canConsumeVideo);
|
console.log('[consume] canConsumeVideo', canConsumeVideo);
|
||||||
console.log('[consume] canConsumeAudio', canConsumeAudio);
|
console.log('[consume] canConsumeAudio', canConsumeAudio);
|
||||||
|
|
||||||
if (canConsumeVideo && !canConsumeAudio) {
|
if (canConsumeVideo && !canConsumeAudio) {
|
||||||
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities)
|
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities);
|
||||||
callback({ videoParams, audioParams: null });
|
callback({ videoParams, audioParams: null });
|
||||||
} else if (canConsumeVideo && canConsumeAudio) {
|
} else if (canConsumeVideo && canConsumeAudio) {
|
||||||
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities)
|
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities);
|
||||||
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities)
|
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities);
|
||||||
callback({ videoParams, audioParams });
|
callback({ videoParams, audioParams });
|
||||||
} else if (!canConsumeVideo && canConsumeAudio) {
|
} else if (!canConsumeVideo && canConsumeAudio) {
|
||||||
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities)
|
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities);
|
||||||
const data = { videoParams: null, audioParams };
|
const data = { videoParams: null, audioParams };
|
||||||
callback(data);
|
callback(data);
|
||||||
} else {
|
} else {
|
||||||
@ -445,7 +472,7 @@ peers.on('connection', async socket => {
|
|||||||
callback(null);
|
callback(null);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
|
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
callback({ params: { error } });
|
callback({ params: { error } });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -455,10 +482,10 @@ peers.on('connection', async socket => {
|
|||||||
- When consuming on consumerTransport, it is initially done with paused: true, here we will resume
|
- 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
|
- For the initiator we resume the initiatorConsumerAUDIO/VIDEO and for receiver the receiverConsumerAUDIO/VIDEO
|
||||||
*/
|
*/
|
||||||
socket.on('consumer-resume',() => {
|
socket.on('consumer-resume', () => {
|
||||||
try {
|
try {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log(`[consumer-resume] callId ${callId}`)
|
console.log(`[consumer-resume] callId ${callId}`);
|
||||||
if (isInitiator(callId, socket.id)) {
|
if (isInitiator(callId, socket.id)) {
|
||||||
videoCalls[callId]?.initiatorConsumerVideo?.resume();
|
videoCalls[callId]?.initiatorConsumerVideo?.resume();
|
||||||
videoCalls[callId]?.initiatorConsumerAudio?.resume();
|
videoCalls[callId]?.initiatorConsumerAudio?.resume();
|
||||||
@ -473,8 +500,7 @@ peers.on('connection', async socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const consumeVideo = async (callId, socketId, rtpCapabilities) => {
|
const consumeVideo = async (callId, socketId, rtpCapabilities) => {
|
||||||
|
if (isInitiator(callId, socketId)) {
|
||||||
if(isInitiator(callId, socketId)) {
|
|
||||||
videoCalls[callId].initiatorConsumerVideo = await videoCalls[callId].initiatorConsumerTransport.consume({
|
videoCalls[callId].initiatorConsumerVideo = await videoCalls[callId].initiatorConsumerTransport.consume({
|
||||||
producerId: videoCalls[callId].receiverVideoProducer.id,
|
producerId: videoCalls[callId].receiverVideoProducer.id,
|
||||||
rtpCapabilities,
|
rtpCapabilities,
|
||||||
@ -500,7 +526,7 @@ const consumeVideo = async (callId, socketId, rtpCapabilities) => {
|
|||||||
producerId: videoCalls[callId].receiverVideoProducer.id,
|
producerId: videoCalls[callId].receiverVideoProducer.id,
|
||||||
kind: 'video',
|
kind: 'video',
|
||||||
rtpParameters: videoCalls[callId].initiatorConsumerVideo.rtpParameters,
|
rtpParameters: videoCalls[callId].initiatorConsumerVideo.rtpParameters,
|
||||||
}
|
};
|
||||||
} else {
|
} else {
|
||||||
videoCalls[callId].receiverConsumerVideo = await videoCalls[callId].receiverConsumerTransport.consume({
|
videoCalls[callId].receiverConsumerVideo = await videoCalls[callId].receiverConsumerTransport.consume({
|
||||||
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
||||||
@ -525,12 +551,12 @@ const consumeVideo = async (callId, socketId, rtpCapabilities) => {
|
|||||||
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
producerId: videoCalls[callId].initiatorVideoProducer.id,
|
||||||
kind: 'video',
|
kind: 'video',
|
||||||
rtpParameters: videoCalls[callId].receiverConsumerVideo.rtpParameters,
|
rtpParameters: videoCalls[callId].receiverConsumerVideo.rtpParameters,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const consumeAudio = async (callId, socketId, rtpCapabilities) => {
|
const consumeAudio = async (callId, socketId, rtpCapabilities) => {
|
||||||
if(isInitiator(callId, socketId)) {
|
if (isInitiator(callId, socketId)) {
|
||||||
videoCalls[callId].initiatorConsumerAudio = await videoCalls[callId].initiatorConsumerTransport.consume({
|
videoCalls[callId].initiatorConsumerAudio = await videoCalls[callId].initiatorConsumerTransport.consume({
|
||||||
producerId: videoCalls[callId].receiverAudioProducer.id,
|
producerId: videoCalls[callId].receiverAudioProducer.id,
|
||||||
rtpCapabilities,
|
rtpCapabilities,
|
||||||
@ -556,7 +582,7 @@ const consumeAudio = async (callId, socketId, rtpCapabilities) => {
|
|||||||
producerId: videoCalls[callId].receiverAudioProducer.id,
|
producerId: videoCalls[callId].receiverAudioProducer.id,
|
||||||
kind: 'audio',
|
kind: 'audio',
|
||||||
rtpParameters: videoCalls[callId].initiatorConsumerAudio.rtpParameters,
|
rtpParameters: videoCalls[callId].initiatorConsumerAudio.rtpParameters,
|
||||||
}
|
};
|
||||||
} else {
|
} else {
|
||||||
videoCalls[callId].receiverConsumerAudio = await videoCalls[callId].receiverConsumerTransport.consume({
|
videoCalls[callId].receiverConsumerAudio = await videoCalls[callId].receiverConsumerTransport.consume({
|
||||||
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
||||||
@ -581,13 +607,13 @@ const consumeAudio = async (callId, socketId, rtpCapabilities) => {
|
|||||||
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
producerId: videoCalls[callId].initiatorAudioProducer.id,
|
||||||
kind: 'audio',
|
kind: 'audio',
|
||||||
rtpParameters: videoCalls[callId].receiverConsumerAudio.rtpParameters,
|
rtpParameters: videoCalls[callId].receiverConsumerAudio.rtpParameters,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const isInitiator = (callId, socketId) => {
|
const isInitiator = (callId, socketId) => {
|
||||||
return (videoCalls[callId]?.initiatorSocket?.id === socketId);
|
return videoCalls[callId]?.initiatorSocket?.id === socketId;
|
||||||
}
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
- Called from at event 'createWebRtcTransport' and assigned to the consumer or producer transport
|
- Called from at event 'createWebRtcTransport' and assigned to the consumer or producer transport
|
||||||
@ -605,18 +631,18 @@ const createWebRtcTransportLayer = async (callId, callback) => {
|
|||||||
{
|
{
|
||||||
ip: process.env.IP, // Listening IPv4 or IPv6.
|
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).
|
announcedIp: process.env.ANNOUNCED_IP, // Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
enableUdp: true,
|
enableUdp: true,
|
||||||
enableTcp: true,
|
enableTcp: true,
|
||||||
preferUdp: true,
|
preferUdp: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
|
||||||
let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options)
|
let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options);
|
||||||
|
|
||||||
// Handler for when DTLS(Datagram Transport Layer Security) changes
|
// Handler for when DTLS(Datagram Transport Layer Security) changes
|
||||||
transport.on('dtlsstatechange', dtlsState => {
|
transport.on('dtlsstatechange', (dtlsState) => {
|
||||||
console.log(`transport | dtlsstatechange | calldId ${callId} | dtlsState ${dtlsState}`);
|
console.log(`transport | dtlsstatechange | calldId ${callId} | dtlsState ${dtlsState}`);
|
||||||
if (dtlsState === 'closed') {
|
if (dtlsState === 'closed') {
|
||||||
transport.close();
|
transport.close();
|
||||||
@ -641,9 +667,8 @@ const createWebRtcTransportLayer = async (callId, callback) => {
|
|||||||
|
|
||||||
// Set transport to producerTransport or consumerTransport
|
// Set transport to producerTransport or consumerTransport
|
||||||
return transport;
|
return transport;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
|
console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
callback({ params: { error } });
|
callback({ params: { error } });
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user