LINXD-2209: Refactor how we close the call; Check for callId in createRoom event

This commit is contained in:
Sergiu Toma 2022-09-15 17:07:47 +03:00
parent a51a757d17
commit 22e8b4d364

44
app.js
View File

@ -99,6 +99,17 @@ const mediaCodecs = [
}, },
] ]
const closeCall = (callId) => {
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].router;
}
}
const getRtpCapabilities = (callId, callback) => { const getRtpCapabilities = (callId, callback) => {
console.log('[getRtpCapabilities] callId', callId); console.log('[getRtpCapabilities] callId', callId);
const rtpCapabilities = videoCalls[callId].router.rtpCapabilities; const rtpCapabilities = videoCalls[callId].router.rtpCapabilities;
@ -118,14 +129,18 @@ peers.on('connection', async socket => {
}) })
socket.on('createRoom', async ({ callId }, callback) => { socket.on('createRoom', async ({ callId }, callback) => {
console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`); if (callId) {
if (!videoCalls[callId]) { console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`);
console.log('[createRoom] callId', callId); if (!videoCalls[callId]) {
videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) } console.log('[createRoom] callId', callId);
console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) 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}`);
} }
socketDetails[socket.id] = callId;
getRtpCapabilities(callId, callback);
}) })
// Client emits a request to create server side Transport // Client emits a request to create server side Transport
@ -159,14 +174,9 @@ peers.on('connection', async socket => {
console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producer.id} | kind: ${videoCalls[callId].producer.kind}`) console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producer.id} | kind: ${videoCalls[callId].producer.kind}`)
videoCalls[callId].producer.on('transportclose', () => { videoCalls[callId].producer.on('transportclose', () => {
const callId = socketDetails[socket.id];
console.log('transport for this producer closed', callId) console.log('transport for this producer closed', callId)
closeCall(callId);
// https://mediasoup.org/documentation/v3/mediasoup/api/#producer-close
videoCalls[callId].producer.close()
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
videoCalls[callId].router.close()
delete videoCalls[callId]
}) })
}) })
@ -200,7 +210,6 @@ peers.on('connection', async socket => {
console.log('transport close from consumer', callId) console.log('transport close from consumer', callId)
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
stvideoCallsate[callId].router.close()
videoCalls[callId].producer.close() videoCalls[callId].producer.close()
videoCalls[callId].consumer.close() videoCalls[callId].consumer.close()
delete videoCalls[callId].router delete videoCalls[callId].router
@ -211,10 +220,7 @@ peers.on('connection', async socket => {
console.log('producer of consumer closed', callId) console.log('producer of consumer closed', callId)
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
videoCalls[callId].router.close() closeCall()
videoCalls[callId].producer.close()
videoCalls[callId].consumer.close()
delete videoCalls[callId].router
}) })
// from the consumer extract the following params // from the consumer extract the following params