diff --git a/app.js b/app.js index 6957b2a..649a7b5 100644 --- a/app.js +++ b/app.js @@ -28,8 +28,8 @@ let worker; * initiatorConsumerVideo: Consumer, * initiatorConsumerAudio: Consumer, * initiatorConsumerTransport: Consumer Transport - * initiatorSockerId - * receiverSocketId + * initiatorSocket + * receiverSocket * } * **/ @@ -192,12 +192,11 @@ peers.on('connection', async socket => { 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 }) } + videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) }; console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`); - videoCalls[callId].receiverSocketId = socket.id + videoCalls[callId].receiverSocket = socket; } else { - videoCalls[callId].initiatorSockerId = socket.id + videoCalls[callId].initiatorSocket = socket; } socketDetails[socket.id] = callId; // rtpCapabilities is set for callback @@ -259,11 +258,19 @@ peers.on('connection', async socket => { if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters); console.log(`[transport-connect] socket ${socket.id} | callId ${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) { console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`); } @@ -280,6 +287,7 @@ peers.on('connection', async socket => { if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters); console.log(`[transport-produce] kind: ${kind} | socket: ${socket.id} | callId: ${callId}`); + if (kind === 'video') { if (!isInitiator(callId, socket.id)) { videoCalls[callId].receiverVideoProducer = await videoCalls[callId].receiverProducerTransport.produce({ @@ -578,7 +586,7 @@ const consumeAudio = async (callId, socketId, rtpCapabilities) => { } const isInitiator = (callId, socketId) => { - return (videoCalls[callId].initiatorSockerId === socketId); + return (videoCalls[callId]?.initiatorSocket?.id === socketId); } /* @@ -606,7 +614,6 @@ const createWebRtcTransportLayer = async (callId, callback) => { // https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options) - console.log(`callId: ${callId} | transport id: ${transport.id}`) // Handler for when DTLS(Datagram Transport Layer Security) changes transport.on('dtlsstatechange', dtlsState => { diff --git a/public/index.js b/public/index.js index 2d2faac..1fbf13a 100644 --- a/public/index.js +++ b/public/index.js @@ -91,10 +91,14 @@ setTimeout(() => { console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`) if (!IS_PRODUCER && existsProducer && consumer === undefined) { goConnect() - // document.getElementById('btnRecvSendTransport').click(); } if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() } }) + + socket.on('new-producer', ({ callId }) => { + console.log(`🟢 new-producer | callId: ${callId} | Ready to consume`); + consume() + }) } if (IS_PRODUCER === true) {