From 8444809910abe74a79fffa123d886864bd5361b0 Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Thu, 24 Nov 2022 23:06:02 +0200 Subject: [PATCH] Update server --- app.js | 140 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 101 insertions(+), 39 deletions(-) diff --git a/app.js b/app.js index 264573a..59a3772 100644 --- a/app.js +++ b/app.js @@ -160,8 +160,11 @@ const mediaCodecs = [ const closeCall = (callId) => { try { if (callId && videoCalls[callId]) { - videoCalls[callId].producer?.close(); - videoCalls[callId].consumer?.close(); + videoCalls[callId].producerVideo?.close(); + videoCalls[callId].producerAudio?.close(); + videoCalls[callId].consumerVideo?.close(); + videoCalls[callId].consumerAudio?.close(); + videoCalls[callId]?.consumerTransport?.close(); videoCalls[callId]?.producerTransport?.close(); videoCalls[callId]?.router?.close(); @@ -280,31 +283,54 @@ peers.on('connection', async socket => { - The event sent by the client (PRODUCER) after successfully connecting to producerTransport - For the router with the id callId, we make produce on producerTransport - Create the handler on producer at the 'transportclose' event - */ - socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => { - try { - const callId = socketDetails[socket.id]; - if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters); + */ + socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => { + try { + const callId = socketDetails[socket.id]; + if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters); + + console.log(`[transport-produce] kind: ${kind} | socket.id: ${socket.id} | callId: ${callId}`); + console.log('kind', kind); + console.log('rtpParameters', rtpParameters); - console.log(`[transport-produce] kind: ${kind} | socket.id: ${socket.id} | callId: ${callId}`); - console.log('kind', kind); - console.log('rtpParameters', rtpParameters); - videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({ - kind, - rtpParameters, - }); - console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producer.id} | kind: ${videoCalls[callId].producer.kind}`); - - videoCalls[callId].producer.on('transportclose', () => { - const callId = socketDetails[socket.id]; - console.log('transport for this producer closed', callId) - closeCall(callId); - }); + if (kind === 'video') { + videoCalls[callId].producerVideo = await videoCalls[callId].producerTransport.produce({ + kind, + rtpParameters, + }); + - // Send back to the client the Producer's id - callback && callback({ - id: videoCalls[callId].producer.id - }); + console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producerVideo.id} | kind: ${videoCalls[callId].producerVideo.kind}`); + + videoCalls[callId].producerVideo.on('transportclose', () => { + const callId = socketDetails[socket.id]; + console.log('transport for this producer closed', callId) + closeCall(callId); + }); + + // Send back to the client the Producer's id + callback && callback({ + id: videoCalls[callId].producerVideo.id + }); + } else if (kind === 'audio') { + videoCalls[callId].producerAudio = await videoCalls[callId].producerTransport.produce({ + kind, + rtpParameters, + }); + + console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producerAudio.id} | kind: ${videoCalls[callId].producerAudio.kind}`); + + videoCalls[callId].producerAudio.on('transportclose', () => { + const callId = socketDetails[socket.id]; + console.log('transport for this producer closed', callId) + closeCall(callId); + }); + + // Send back to the client the Producer's id + callback && callback({ + id: videoCalls[callId].producerAudio.id + }); + } } catch (error) { console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`); } @@ -339,43 +365,79 @@ peers.on('connection', async socket => { console.log('[consume] callId', callId); // Check if the router can consume the specified producer - if (videoCalls[callId].router.canConsume({ - producerId: videoCalls[callId].producer.id, - rtpCapabilities - })) { + if ( + videoCalls[callId].router.canConsume({ + producerId: videoCalls[callId].producerVideo.id, + rtpCapabilities + }) && + videoCalls[callId].router.canConsume({ + producerId: videoCalls[callId].producerAudio.id, + rtpCapabilities + }) + ) { console.log('[consume] Can consume', callId); // Transport can now consume and return a consumer - videoCalls[callId].consumer = await videoCalls[callId].consumerTransport.consume({ - producerId: videoCalls[callId].producer.id, + videoCalls[callId].consumerVideo = await videoCalls[callId].consumerTransport.consume({ + producerId: videoCalls[callId].producerVideo.id, rtpCapabilities, paused: true, }); // https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose - videoCalls[callId].consumer.on('transportclose', () => { + videoCalls[callId].consumerVideo.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].consumer.on('producerclose', () => { + videoCalls[callId].consumerVideo.on('producerclose', () => { const callId = socketDetails[socket.id]; console.log('producer of consumer closed', callId); closeCall(callId); }); + videoCalls[callId].consumerAudio = await videoCalls[callId].consumerTransport.consume({ + producerId: videoCalls[callId].producerAudio.id, + rtpCapabilities, + paused: true, + }); + + // https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose + videoCalls[callId].consumerAudio.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].consumerAudio.on('producerclose', () => { + const callId = socketDetails[socket.id]; + console.log('producer of consumer closed', callId); + closeCall(callId); + }); + + console.log('🟠 videoCalls[callId].consumerVideo', videoCalls[callId].consumerVideo); + console.log('🟠 videoCalls[callId].consumerAudio', videoCalls[callId].consumerAudio); + // From the consumer extract the following params to send back to the Client - const params = { - id: videoCalls[callId].consumer.id, - producerId: videoCalls[callId].producer.id, - kind: videoCalls[callId].consumer.kind, - rtpParameters: videoCalls[callId].consumer.rtpParameters, + const videoParams = { + id: videoCalls[callId].consumerVideo.id, + producerId: videoCalls[callId].producerVideo.id, + kind: 'video', + rtpParameters: videoCalls[callId].consumerVideo.rtpParameters, + }; + + const audioParams = { + id: videoCalls[callId].consumerAudio.id, + producerId: videoCalls[callId].producerAudio.id, + kind: 'audio', + rtpParameters: videoCalls[callId].consumerAudio.rtpParameters, }; console.log('[consume] params', params); // Send the parameters to the client - callback({ params }); + callback({ videoParams, audioParams }); } else { console.log(`[canConsume] Can't consume | callId ${callId}`); callback(null);