From b834016dcb5df89d2e7175664fb109eedf78c2ab Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 19:56:06 +0300 Subject: [PATCH 01/31] LINXD-2209: Create rooms in sequence --- app.js | 64 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/app.js b/app.js index 860dae3..3dbabbb 100644 --- a/app.js +++ b/app.js @@ -96,6 +96,36 @@ const mediaCodecs = [ }, ] + 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 + callback({ rtpCapabilities }) + } + + setInterval(async () => { + const { callId, callback } = queue.shift(); + if (router[callId] === undefined) { + router[callId] = await worker.createRouter({ mediaCodecs }) + console.log(`[createRoom] Router ID: ${router[callId].id}`) + getRtpCapabilities(callId, callback) + } + }, 2000); + peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) socket.emit('connection-success', { @@ -109,27 +139,25 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - console.log('[createRoom] callId', callId); - 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}`) - } + // console.log('[createRoom] callId', callId); + // 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}`) + // } - getRtpCapabilities(callId, callback) + // getRtpCapabilities(callId, callback) + queue.push({ + callId, + callback + }) }) - const getRtpCapabilities = (callId, callback) => { - const rtpCapabilities = router[callId].rtpCapabilities - - callback({ rtpCapabilities }) - } - // 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) => { From 782b749ea38c7af2c61b6bdcb736320eca292ed1 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:09:25 +0300 Subject: [PATCH 02/31] LINXD-2209: Check queue length --- .env | 2 +- app.js | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.env b/.env index 6d8b7b8..551a6ac 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -PORT=3000 +PORT=3001 IP=0.0.0.0 # Listening IPv4 or IPv6. ANNOUNCED_IP=185.8.154.190 # Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP). diff --git a/app.js b/app.js index 3dbabbb..8ef4ba6 100644 --- a/app.js +++ b/app.js @@ -118,11 +118,13 @@ const mediaCodecs = [ } setInterval(async () => { - const { callId, callback } = queue.shift(); - if (router[callId] === undefined) { - router[callId] = await worker.createRouter({ mediaCodecs }) - console.log(`[createRoom] Router ID: ${router[callId].id}`) - getRtpCapabilities(callId, callback) + if (queue.length > 0) { + const { callId, callback } = queue.shift(); + if (router[callId] === undefined) { + router[callId] = await worker.createRouter({ mediaCodecs }) + console.log(`[createRoom] Router ID: ${router[callId].id}`) + getRtpCapabilities(callId, callback) + } } }, 2000); From ecb5a88a2c625efffc6645da403c9e2064808fad Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:10:34 +0300 Subject: [PATCH 03/31] LINXD-2209: Update port --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 551a6ac..6d8b7b8 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -PORT=3001 +PORT=3000 IP=0.0.0.0 # Listening IPv4 or IPv6. ANNOUNCED_IP=185.8.154.190 # Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP). From 5abc3095029ef0e56d1d63867928d07f9e32c593 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:28:07 +0300 Subject: [PATCH 04/31] LINXD-2209: Added logs --- app.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 8ef4ba6..ec2727e 100644 --- a/app.js +++ b/app.js @@ -113,6 +113,7 @@ const mediaCodecs = [ const getRtpCapabilities = (callId, callback) => { + console.log('[getRtpCapabilities]', callId, callback); const rtpCapabilities = router[callId].rtpCapabilities callback({ rtpCapabilities }) } @@ -121,12 +122,13 @@ const mediaCodecs = [ if (queue.length > 0) { const { callId, callback } = queue.shift(); if (router[callId] === undefined) { + console.log('🟢 callId', callId, '| callback', callback) router[callId] = await worker.createRouter({ mediaCodecs }) console.log(`[createRoom] Router ID: ${router[callId].id}`) getRtpCapabilities(callId, callback) } } - }, 2000); + }, 4000); peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) From 8562f6c58ca1168c697b21f31518cf5595e400c8 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:49:40 +0300 Subject: [PATCH 05/31] LINXD-2209: Added logs --- app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app.js b/app.js index ec2727e..ddf336c 100644 --- a/app.js +++ b/app.js @@ -125,6 +125,7 @@ const mediaCodecs = [ console.log('🟢 callId', callId, '| callback', callback) router[callId] = await worker.createRouter({ mediaCodecs }) console.log(`[createRoom] Router ID: ${router[callId].id}`) + console.log('🟡 router', router); getRtpCapabilities(callId, callback) } } From 91b4db198283f24415e56325b0f13d48472ca59e Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:54:35 +0300 Subject: [PATCH 06/31] LINXD-2209: Added logs --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index ddf336c..c29047f 100644 --- a/app.js +++ b/app.js @@ -113,8 +113,8 @@ const mediaCodecs = [ const getRtpCapabilities = (callId, callback) => { - console.log('[getRtpCapabilities]', callId, callback); const rtpCapabilities = router[callId].rtpCapabilities + console.log('[getRtpCapabilities]', callId, callback, rtpCapabilities); callback({ rtpCapabilities }) } From 817a49204d9be2002b57a958436acc90488b4ebc Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:58:06 +0300 Subject: [PATCH 07/31] LINXD-2209: Added logs --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index c29047f..92b5259 100644 --- a/app.js +++ b/app.js @@ -144,7 +144,7 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - // console.log('[createRoom] callId', callId); + console.log('[createRoom] callId', callId); // console.log('Router length:', Object.keys(router).length); // if (router[callId] === undefined) { // // worker.createRouter(options) From 25a76c343b49c78a8bab3d5d67e0aa6b712bb3ff Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:59:12 +0300 Subject: [PATCH 08/31] LINXD-2209: Move getRtpCapa outside of room creation --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 92b5259..d3f4aea 100644 --- a/app.js +++ b/app.js @@ -126,8 +126,8 @@ const mediaCodecs = [ router[callId] = await worker.createRouter({ mediaCodecs }) console.log(`[createRoom] Router ID: ${router[callId].id}`) console.log('🟡 router', router); - getRtpCapabilities(callId, callback) } + getRtpCapabilities(callId, callback) } }, 4000); From bb684ca4dbc826b9af279feae87bdd1cfaf51f99 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 20:59:55 +0300 Subject: [PATCH 09/31] LINXD-2209: Add 300ms delay --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index d3f4aea..886a4ef 100644 --- a/app.js +++ b/app.js @@ -129,7 +129,7 @@ const mediaCodecs = [ } getRtpCapabilities(callId, callback) } - }, 4000); + }, 1000); peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) From 38931f0654b9241d01bc641bb9076b2b7a0701d6 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:02:29 +0300 Subject: [PATCH 10/31] LINXD-2209: Add 50ms delay between room creation --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 886a4ef..a162622 100644 --- a/app.js +++ b/app.js @@ -129,7 +129,7 @@ const mediaCodecs = [ } getRtpCapabilities(callId, callback) } - }, 1000); + }, 50); peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) From d7486d0fd622eb868cbe2d1a94e362805a932eaf Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:04:42 +0300 Subject: [PATCH 11/31] LINXD-2209: Add 10ms delay between room creation --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index a162622..5e2963c 100644 --- a/app.js +++ b/app.js @@ -129,7 +129,7 @@ const mediaCodecs = [ } getRtpCapabilities(callId, callback) } - }, 50); + }, 1); peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) From 031a7bc4c5e7ded5019569d76726a2dd363d6c90 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:05:24 +0300 Subject: [PATCH 12/31] LINXD-2209: Remove console.logs --- app.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/app.js b/app.js index 5e2963c..9409eef 100644 --- a/app.js +++ b/app.js @@ -114,7 +114,6 @@ const mediaCodecs = [ const getRtpCapabilities = (callId, callback) => { const rtpCapabilities = router[callId].rtpCapabilities - console.log('[getRtpCapabilities]', callId, callback, rtpCapabilities); callback({ rtpCapabilities }) } @@ -122,10 +121,8 @@ const mediaCodecs = [ if (queue.length > 0) { const { callId, callback } = queue.shift(); if (router[callId] === undefined) { - console.log('🟢 callId', callId, '| callback', callback) router[callId] = await worker.createRouter({ mediaCodecs }) console.log(`[createRoom] Router ID: ${router[callId].id}`) - console.log('🟡 router', router); } getRtpCapabilities(callId, callback) } From 6938e751fe4d3748fbdd6f152dac59b9226aa45d Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:08:03 +0300 Subject: [PATCH 13/31] LINXD-2209: Add 100ms delay between room creation --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 9409eef..24de64f 100644 --- a/app.js +++ b/app.js @@ -126,7 +126,7 @@ const mediaCodecs = [ } getRtpCapabilities(callId, callback) } - }, 1); + }, 1000); peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) From ab685270f1966bb8f69b830b776ec0cdb8341be1 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:08:14 +0300 Subject: [PATCH 14/31] LINXD-2209: Add 1000ms delay between room creation --- app.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app.js b/app.js index 24de64f..f07c0a0 100644 --- a/app.js +++ b/app.js @@ -111,7 +111,6 @@ const mediaCodecs = [ // callback: () => console.log('callback') // }) - const getRtpCapabilities = (callId, callback) => { const rtpCapabilities = router[callId].rtpCapabilities callback({ rtpCapabilities }) From accf960aa7a7871b09f65f41209090a8b25f3260 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:15:51 +0300 Subject: [PATCH 15/31] LINXD-2209: Added logs on consume --- app.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index f07c0a0..9bbe501 100644 --- a/app.js +++ b/app.js @@ -205,6 +205,7 @@ peers.on('connection', async socket => { }) socket.on('consume', async ({ rtpCapabilities, callId }, callback) => { + console.log('[consume] callId', callId); try { // console.log('consume', rtpCapabilities, callId); // check if the router can consume the specified producer @@ -212,6 +213,7 @@ peers.on('connection', async socket => { producerId: producer.id, rtpCapabilities })) { + console.log('[consume] Can consume', callId); // transport can now consume and return a consumer consumer = await consumerTransport.consume({ producerId: producer.id, @@ -252,7 +254,7 @@ peers.on('connection', async socket => { callback({ params }) } } catch (error) { - console.log(error.message) + console.log('[consume] Can\'t consume', error.message) callback({ params: { error: error From 47eb302f5fc7d7fb92ed14d812627a1a107f71ff Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:33:04 +0300 Subject: [PATCH 16/31] LINXD-2209: Added logs on consume --- app.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 9bbe501..2898019 100644 --- a/app.js +++ b/app.js @@ -252,9 +252,11 @@ peers.on('connection', async socket => { // send the parameters to the client callback({ params }) + } else { + console.log('[canConsume] Can\'t consume', error.message) } } catch (error) { - console.log('[consume] Can\'t consume', error.message) + console.log('[consume] Error', error.message) callback({ params: { error: error From c12ececf473ccbee106948389d3b88f965c0f0fb Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:35:26 +0300 Subject: [PATCH 17/31] LINXD-2209: Add 2000ms delay between room creation --- app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 2898019..03ce86d 100644 --- a/app.js +++ b/app.js @@ -125,7 +125,7 @@ const mediaCodecs = [ } getRtpCapabilities(callId, callback) } - }, 1000); + }, 2000); peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) @@ -253,7 +253,7 @@ peers.on('connection', async socket => { // send the parameters to the client callback({ params }) } else { - console.log('[canConsume] Can\'t consume', error.message) + console.log('[canConsume] Can\'t consume') } } catch (error) { console.log('[consume] Error', error.message) From c3d50fdc4ee6e298769d1e6090a3a8c771829711 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:38:06 +0300 Subject: [PATCH 18/31] LINXD-2209: Add 4000ms delay between room creation --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 03ce86d..f9166f1 100644 --- a/app.js +++ b/app.js @@ -125,7 +125,7 @@ const mediaCodecs = [ } getRtpCapabilities(callId, callback) } - }, 2000); + }, 4000); peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) From 294dbdf38ded94bac8e4aeaf9c3745106ae271de Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 21:43:16 +0300 Subject: [PATCH 19/31] LINXD-2209: Added logs --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index f9166f1..6d369a8 100644 --- a/app.js +++ b/app.js @@ -205,7 +205,7 @@ peers.on('connection', async socket => { }) socket.on('consume', async ({ rtpCapabilities, callId }, callback) => { - console.log('[consume] callId', callId); + console.log('[consume] callId', callId, router); try { // console.log('consume', rtpCapabilities, callId); // check if the router can consume the specified producer From 6acd27632462726ae11d2585d8e35042673c3b27 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 22:16:51 +0300 Subject: [PATCH 20/31] LINXD-2209: Refactor how we save router, consumer, producer, producerTransport and consumerTransport --- app.js | 138 ++++++++++++++++++++------------------------------------- 1 file changed, 48 insertions(+), 90 deletions(-) diff --git a/app.js b/app.js index 6d369a8..a8dd3eb 100644 --- a/app.js +++ b/app.js @@ -18,11 +18,7 @@ import Server from 'socket.io' import mediasoup, { getSupportedRtpCapabilities } from 'mediasoup' let worker -let router = {} -let producerTransport -let consumerTransport -let producer -let consumer +let videoCalls = {} app.get('/', (_req, res) => { res.send('Hello from mediasoup app!') @@ -96,42 +92,16 @@ const mediaCodecs = [ }, ] - 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 - callback({ rtpCapabilities }) - } - - setInterval(async () => { - if (queue.length > 0) { - const { callId, callback } = queue.shift(); - if (router[callId] === undefined) { - router[callId] = await worker.createRouter({ mediaCodecs }) - console.log(`[createRoom] Router ID: ${router[callId].id}`) - } - getRtpCapabilities(callId, callback) - } - }, 4000); +const getRtpCapabilities = (callId, callback) => { + console.log('[getRtpCapabilities] callId', callId); + const rtpCapabilities = videoCalls[callId].router.rtpCapabilities; + callback({ rtpCapabilities }); +} peers.on('connection', async socket => { console.log('[connection] socketId:', socket.id) socket.emit('connection-success', { - socketId: socket.id, - existsProducer: producer ? true : false, + socketId: socket.id }) socket.on('disconnect', () => { @@ -140,23 +110,12 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - console.log('[createRoom] callId', callId); - // 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}`) - // } - - // getRtpCapabilities(callId, callback) - queue.push({ - callId, - callback - }) + if (videoCalls[callId].router === undefined) { + console.log('[createRoom] callId', callId); + videoCalls[callId].router = await worker.createRouter({ mediaCodecs }) + console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) + } + getRtpCapabilities(callId, callback) }) // Client emits a request to create server side Transport @@ -166,88 +125,88 @@ peers.on('connection', async socket => { // 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) + videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback) else - consumerTransport = await createWebRtcTransportLayer(callId, callback) + videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback) }) // see client's socket.emit('transport-connect', ...) - socket.on('transport-connect', async ({ dtlsParameters }) => { + socket.on('transport-connect', async ({ dtlsParameters, callId }) => { console.log('[transport-connect] DTLS PARAMS... ', { dtlsParameters }) - await producerTransport.connect({ dtlsParameters }) + await videoCalls[callId].producerTransport.connect({ dtlsParameters }) }) // see client's socket.emit('transport-produce', ...) - socket.on('transport-produce', async ({ kind, rtpParameters, appData }) => { + socket.on('transport-produce', async ({ kind, rtpParameters, appData, callId }) => { // call produce based on the prameters from the client - producer = await producerTransport.produce({ + videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({ kind, rtpParameters, }) - console.log(`[transport-produce] Producer ID: ${producer.id} | kind: ${producer.kind}`) + console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producer.id} | kind: ${videoCalls[callId].producer.kind}`) - producer.on('transportclose', () => { + videoCalls[callId].producer.on('transportclose', () => { console.log('transport for this producer closed', callId) // https://mediasoup.org/documentation/v3/mediasoup/api/#producer-close - producer.close() + videoCalls[callId].producer.close() // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close - router[callId].close() - delete router[callId] + videoCalls[callId].router.close() + delete videoCalls[callId] }) }) // see client's socket.emit('transport-recv-connect', ...) - socket.on('transport-recv-connect', async ({ dtlsParameters }) => { + socket.on('transport-recv-connect', async ({ dtlsParameters, callId }) => { console.log(`[transport-recv-connect] DTLS PARAMS: ${dtlsParameters}`) - await consumerTransport.connect({ dtlsParameters }) + await videoCalls[callId].consumerTransport.connect({ dtlsParameters }) }) socket.on('consume', async ({ rtpCapabilities, callId }, callback) => { - console.log('[consume] callId', callId, router); + console.log('[consume] callId', callId); try { // console.log('consume', rtpCapabilities, callId); // check if the router can consume the specified producer - if (router[callId].canConsume({ - producerId: producer.id, + 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 - consumer = await consumerTransport.consume({ - producerId: producer.id, + videoCalls[callId].consumer = await videoCalls[callId].consumerTransport.consume({ + producerId: videoCalls[callId].producer.id, rtpCapabilities, paused: true, }) - consumer.on('transportclose', () => { + videoCalls[callId].consumer.on('transportclose', () => { console.log('transport close from consumer', callId) // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close - router[callId].close() - delete router[callId] - producer.close() - consumer.close() + stvideoCallsate[callId].router.close() + delete videoCalls[callId].router + videoCalls[callId].producer.close() + videoCalls[callId].consumer.close() }) - consumer.on('producerclose', () => { + videoCalls[callId].consumer.on('producerclose', () => { console.log('producer of consumer closed', callId) // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close - router[callId].close() - delete router[callId] - producer.close() - consumer.close() + videoCalls[callId].router.close() + delete videoCalls[callId].router + videoCalls[callId].producer.close() + videoCalls[callId].consumer.close() }) // 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, + 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 @@ -265,9 +224,9 @@ peers.on('connection', async socket => { } }) - socket.on('consumer-resume', async () => { + socket.on('consumer-resume', async ({ callId }) => { console.log(`[consumer-resume]`) - await consumer.resume() + await videoCalls[callId].consumer.resume() }) }) @@ -288,10 +247,9 @@ const createWebRtcTransportLayer = async (callId, callback) => { } // console.log('webRtcTransport_options', webRtcTransport_options); - // console.log('router', router, '| router[callId]', router[callId]); // https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport - let transport = await router[callId].createWebRtcTransport(webRtcTransport_options) + let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options) console.log(`callId: ${callId} | transport id: ${transport.id}`) transport.on('dtlsstatechange', dtlsState => { From b85ba68c9cb81e161fb8bb9f25e3ce2e4efc3cc5 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 13 Sep 2022 22:24:10 +0300 Subject: [PATCH 21/31] LINXD-2209: Added comments --- app.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/app.js b/app.js index a8dd3eb..925b963 100644 --- a/app.js +++ b/app.js @@ -15,9 +15,26 @@ import path from 'path' const __dirname = path.resolve() import Server from 'socket.io' -import mediasoup, { getSupportedRtpCapabilities } from 'mediasoup' +import mediasoup from '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 = {} app.get('/', (_req, res) => { @@ -39,19 +56,8 @@ httpsServer.listen(process.env.PORT, () => { }) const io = new Server(httpsServer) - -// socket.io namespace (could represent a room?) 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, @@ -269,8 +275,6 @@ const createWebRtcTransportLayer = async (callId, callback) => { dtlsParameters: transport.dtlsParameters, } - // console.log('params', params); - // send back to the client the following prameters callback({ // https://mediasoup.org/documentation/v3/mediasoup-client/api/#TransportOptions From 41b50d2a1101afd3e97b8282dc7143aedf475b48 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:19:04 +0300 Subject: [PATCH 22/31] LINXD-2209: Identify the callId from dictionary --- app.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 925b963..0f2bfab 100644 --- a/app.js +++ b/app.js @@ -36,6 +36,7 @@ let worker * **/ let videoCalls = {} +let socketDetails = {} app.get('/', (_req, res) => { res.send('Hello from mediasoup app!') @@ -112,16 +113,18 @@ peers.on('connection', async socket => { socket.on('disconnect', () => { // do some cleanup - console.log('peer disconnected') + console.log('peer disconnected | socket.id', socket.id) + delete socketDetails[socket.id]; }) socket.on('createRoom', async ({ callId }, callback) => { if (videoCalls[callId].router === undefined) { console.log('[createRoom] callId', callId); - videoCalls[callId].router = await worker.createRouter({ mediaCodecs }) + videoCalls[callId].router = await worker.createRouter({ mediaCodecs }); console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) } - getRtpCapabilities(callId, callback) + socketDetails[socket.id] = callId; + getRtpCapabilities(callId, callback); }) // Client emits a request to create server side Transport @@ -137,13 +140,16 @@ peers.on('connection', async socket => { }) // see client's socket.emit('transport-connect', ...) - socket.on('transport-connect', async ({ dtlsParameters, callId }) => { - console.log('[transport-connect] DTLS PARAMS... ', { dtlsParameters }) + socket.on('transport-connect', async ({ dtlsParameters }) => { + const callId = socketDetails[socket.id]; + console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId} | DTLS PARAMS... ${dtlsParameters}`) await videoCalls[callId].producerTransport.connect({ dtlsParameters }) }) // see client's socket.emit('transport-produce', ...) - socket.on('transport-produce', async ({ kind, rtpParameters, appData, callId }) => { + socket.on('transport-produce', async ({ kind, rtpParameters, appData }) => { + const callId = socketDetails[socket.id]; + console.log('[transport-produce] | socket.id', socket.id, '| callId', callId); // call produce based on the prameters from the client videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({ kind, @@ -164,12 +170,14 @@ peers.on('connection', async socket => { }) // see client's socket.emit('transport-recv-connect', ...) - socket.on('transport-recv-connect', async ({ dtlsParameters, callId }) => { - console.log(`[transport-recv-connect] DTLS PARAMS: ${dtlsParameters}`) + socket.on('transport-recv-connect', async ({ dtlsParameters }) => { + const callId = socketDetails[socket.id]; + console.log(`[transport-recv-connect] socket.id ${socket.id} | callId ${callId} | DTLS PARAMS: ${dtlsParameters}`); await videoCalls[callId].consumerTransport.connect({ dtlsParameters }) }) - socket.on('consume', async ({ rtpCapabilities, callId }, callback) => { + socket.on('consume', async ({ rtpCapabilities }, callback) => { + const callId = socketDetails[socket.id]; console.log('[consume] callId', callId); try { // console.log('consume', rtpCapabilities, callId); From f6290127128164300ef13c8499d46d1498824e24 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:32:41 +0300 Subject: [PATCH 23/31] LINXD-2209: Check for router in videoCalls --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 0f2bfab..bf092c8 100644 --- a/app.js +++ b/app.js @@ -118,7 +118,7 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - if (videoCalls[callId].router === undefined) { + if (videoCalls[callId]?.router === undefined) { console.log('[createRoom] callId', callId); videoCalls[callId].router = await worker.createRouter({ mediaCodecs }); console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) From 7198dc91b1bee3a10d86d442db2ba801a3c7b647 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:33:30 +0300 Subject: [PATCH 24/31] LINXD-2209: Check for router in videoCalls --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index bf092c8..7140d82 100644 --- a/app.js +++ b/app.js @@ -118,7 +118,7 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - if (videoCalls[callId]?.router === undefined) { + if (videoCalls[callId] && videoCalls[callId].router === undefined) { console.log('[createRoom] callId', callId); videoCalls[callId].router = await worker.createRouter({ mediaCodecs }); console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) From 434c8f744ceed718766ea45de04563cc4f0b7473 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:35:35 +0300 Subject: [PATCH 25/31] LINXD-2209: Correctly set router to videoCalls --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 7140d82..7d34e75 100644 --- a/app.js +++ b/app.js @@ -101,7 +101,7 @@ const mediaCodecs = [ const getRtpCapabilities = (callId, callback) => { console.log('[getRtpCapabilities] callId', callId); - const rtpCapabilities = videoCalls[callId].router.rtpCapabilities; + const rtpCapabilities = videoCalls[callId] = { router: rtpCapabilities }; callback({ rtpCapabilities }); } From ead0069aa8a06fe02280b41daf3a9e3a11668657 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:39:52 +0300 Subject: [PATCH 26/31] LINXD-2209: Correctly set router to videoCalls --- app.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 7d34e75..93e622c 100644 --- a/app.js +++ b/app.js @@ -101,7 +101,7 @@ const mediaCodecs = [ const getRtpCapabilities = (callId, callback) => { console.log('[getRtpCapabilities] callId', callId); - const rtpCapabilities = videoCalls[callId] = { router: rtpCapabilities }; + const rtpCapabilities = videoCalls[callId].router.rtpCapabilities; callback({ rtpCapabilities }); } @@ -118,11 +118,14 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - if (videoCalls[callId] && videoCalls[callId].router === undefined) { + console.log('[createRoom] videoCalls', videoCalls); + if (!videoCalls[callId]) { + console.log('create room'); console.log('[createRoom] callId', callId); videoCalls[callId].router = await worker.createRouter({ mediaCodecs }); console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) } + console.log('room exists'); socketDetails[socket.id] = callId; getRtpCapabilities(callId, callback); }) From 2f6c25c1715c78484dae394e928d8564229396c4 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:41:24 +0300 Subject: [PATCH 27/31] LINXD-2209: Correctly set router to videoCalls --- app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 93e622c..f6e7e9b 100644 --- a/app.js +++ b/app.js @@ -122,10 +122,10 @@ peers.on('connection', async socket => { if (!videoCalls[callId]) { console.log('create room'); console.log('[createRoom] callId', callId); - videoCalls[callId].router = await worker.createRouter({ mediaCodecs }); + videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) } console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) } - console.log('room exists'); + console.log('room exists', videoCalls); socketDetails[socket.id] = callId; getRtpCapabilities(callId, callback); }) From 19808da24ec9b6cc54abdbf500359a34193209eb Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:43:59 +0300 Subject: [PATCH 28/31] LINXD-2209: Get callId from soekct dictionary in consumer-resume case --- app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index f6e7e9b..db6b46d 100644 --- a/app.js +++ b/app.js @@ -241,8 +241,9 @@ peers.on('connection', async socket => { } }) - socket.on('consumer-resume', async ({ callId }) => { - console.log(`[consumer-resume]`) + socket.on('consumer-resume', async () => { + const callId = socketDetails[socket.id]; + console.log(`[consumer-resume] callId ${callId}`) await videoCalls[callId].consumer.resume() }) }) From c059dd5afccb89acac668f9e3e28908148405126 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:56:32 +0300 Subject: [PATCH 29/31] LINXD-2209: Correctly close the call --- app.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index db6b46d..a9662ce 100644 --- a/app.js +++ b/app.js @@ -198,23 +198,25 @@ peers.on('connection', async socket => { }) videoCalls[callId].consumer.on('transportclose', () => { + const callId = socketDetails[socket.id]; console.log('transport close from consumer', callId) // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close stvideoCallsate[callId].router.close() - delete videoCalls[callId].router videoCalls[callId].producer.close() videoCalls[callId].consumer.close() + delete videoCalls[callId].router }) videoCalls[callId].consumer.on('producerclose', () => { + const callId = socketDetails[socket.id]; console.log('producer of consumer closed', callId) // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close videoCalls[callId].router.close() - delete videoCalls[callId].router videoCalls[callId].producer.close() videoCalls[callId].consumer.close() + delete videoCalls[callId].router }) // from the consumer extract the following params From a51a757d17d6d63d0ca8f11acb1d3da70f8c1cb5 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 09:57:57 +0300 Subject: [PATCH 30/31] LINXD-2209: Correctly close the call --- app.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app.js b/app.js index a9662ce..b3c3c2a 100644 --- a/app.js +++ b/app.js @@ -118,14 +118,12 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - console.log('[createRoom] videoCalls', videoCalls); + console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`); if (!videoCalls[callId]) { - console.log('create room'); console.log('[createRoom] callId', callId); videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) } console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`) } - console.log('room exists', videoCalls); socketDetails[socket.id] = callId; getRtpCapabilities(callId, callback); }) From 22e8b4d364c6e77cfcc6e6a0bfc3784b1338e405 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 15 Sep 2022 17:07:47 +0300 Subject: [PATCH 31/31] LINXD-2209: Refactor how we close the call; Check for callId in createRoom event --- app.js | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index b3c3c2a..1a3530b 100644 --- a/app.js +++ b/app.js @@ -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) => { console.log('[getRtpCapabilities] callId', callId); const rtpCapabilities = videoCalls[callId].router.rtpCapabilities; @@ -118,14 +129,18 @@ peers.on('connection', async socket => { }) socket.on('createRoom', async ({ callId }, callback) => { - 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}`) + 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}`); } - socketDetails[socket.id] = callId; - getRtpCapabilities(callId, callback); }) // 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}`) videoCalls[callId].producer.on('transportclose', () => { + const callId = socketDetails[socket.id]; console.log('transport for this producer closed', 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] + closeCall(callId); }) }) @@ -200,7 +210,6 @@ peers.on('connection', async socket => { console.log('transport close from consumer', callId) // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close - stvideoCallsate[callId].router.close() videoCalls[callId].producer.close() videoCalls[callId].consumer.close() delete videoCalls[callId].router @@ -211,10 +220,7 @@ peers.on('connection', async socket => { console.log('producer of consumer closed', callId) // https://mediasoup.org/documentation/v3/mediasoup/api/#router-close - videoCalls[callId].router.close() - videoCalls[callId].producer.close() - videoCalls[callId].consumer.close() - delete videoCalls[callId].router + closeCall() }) // from the consumer extract the following params