From 46d3499e3d02fa6afa1b91bf3855221f33ba294e Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Tue, 29 Nov 2022 02:34:48 +0200 Subject: [PATCH] Update server --- app.js | 142 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/app.js b/app.js index 8c11bdc..62f0844 100644 --- a/app.js +++ b/app.js @@ -366,79 +366,22 @@ peers.on('connection', async socket => { console.log('🟠 videoCalls[callId].producerAudio', videoCalls[callId].producerAudio); - // Check if the router can consume the specified producer - 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].consumerVideo = await videoCalls[callId].consumerTransport.consume({ - producerId: videoCalls[callId].producerVideo.id, - rtpCapabilities, - paused: true, - }); + const canConsumeVideo = videoCalls[callId].producerVideo && videoCalls[callId].router.canConsume({ + producerId: videoCalls[callId].producerVideo.id, + rtpCapabilities + }) - // https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose - videoCalls[callId].consumerVideo.on('transportclose', () => { - const callId = socketDetails[socket.id]; - console.log('transport close from consumer', callId); - closeCall(callId); - }); + const canConsumeAudio = videoCalls[callId].producerAudio && videoCalls[callId].router.canConsume({ + producerId: videoCalls[callId].producerAudio.id, + rtpCapabilities + }) - // https://mediasoup.org/documentation/v3/mediasoup/api/#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].consumerAudio', videoCalls[callId].consumerAudio); - - // From the consumer extract the following params to send back to the Client - 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] videoParams', videoParams); - console.log('[consume] audioParams', audioParams); - // Send the parameters to the client + if (canConsumeVideo && !canConsumeAudio) { + const videoParams = consumeVideo() + callback({ videoParams, audioParams: null }); + } if (canConsumeVideo && canConsumeAudio) { + const videoParams = consumeVideo() + const audioParams = consumeAudio() callback({ videoParams, audioParams }); } else { console.log(`[canConsume] Can't consume | callId ${callId}`); @@ -466,6 +409,63 @@ peers.on('connection', async socket => { }); }); +const consumeVideo = async () => { + 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].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].consumerVideo.on('producerclose', () => { + const callId = socketDetails[socket.id]; + console.log('producer of consumer closed', callId); + closeCall(callId); + }); + + return { + id: videoCalls[callId].consumerVideo.id, + producerId: videoCalls[callId].producerVideo.id, + kind: 'video', + rtpParameters: videoCalls[callId].consumerVideo.rtpParameters, + } +} + +const consumeAudio = async () => { + 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); + }); + return { + id: videoCalls[callId].consumerAudio.id, + producerId: videoCalls[callId].producerAudio.id, + kind: 'audio', + rtpParameters: videoCalls[callId].consumerAudio.rtpParameters, + } +} + /* - 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