From 24390c98e54d1c3b1978ee7ad4f18b78acb15f4b Mon Sep 17 00:00:00 2001 From: Sergiu Toma Date: Mon, 24 Oct 2022 22:11:14 +0300 Subject: [PATCH] LH-259: Added callbacks --- app.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app.js b/app.js index f1ca550..bad26fe 100644 --- a/app.js +++ b/app.js @@ -124,16 +124,6 @@ const closeCall = (callId) => { } } -const getRtpCapabilities = (callId, callback) => { - try { - console.log('[getRtpCapabilities] callId', callId); - const rtpCapabilities = videoCalls[callId].router.rtpCapabilities; - callback({ rtpCapabilities }); - } catch (error) { - console.log(`ERROR | getRtpCapabilities | callId ${callId} | ${error.message}`); - } -} - /* - Handlers for WS events - These are created only when we have a connection with a peer @@ -160,7 +150,9 @@ peers.on('connection', async socket => { - If the room already exists, it will not create it, but will only return rtpCapabilities */ socket.on('createRoom', async ({ callId }, callback) => { + let callbackResponse = null; try { + // We can continue with the room creation process only if we have a callId if (callId) { console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`); if (!videoCalls[callId]) { @@ -169,12 +161,17 @@ peers.on('connection', async socket => { console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`); } socketDetails[socket.id] = callId; - getRtpCapabilities(callId, callback); + + // rtpCapabilities is set for callback + console.log('[getRtpCapabilities] callId', callId); + callbackResponse = videoCalls[callId].router.rtpCapabilities; } else { console.log(`[createRoom] missing callId ${callId}`); } } catch (error) { console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`); + } finally { + callback(callbackResponse); } }); @@ -195,16 +192,19 @@ peers.on('connection', async socket => { videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback); } else { console.log(`producerTransport has already been defined | callId ${callId}`); + callback(null); } } else if (!sender) { if (!videoCalls[callId].consumerTransport) { videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback); } else { console.log(`consumerTransport has already been defined | callId ${callId}`); + callback(null); } } } catch (error) { console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`); + callback(error); } }); @@ -248,9 +248,9 @@ peers.on('connection', async socket => { }); // Send back to the client the Producer's id - // callback({ - // id: videoCalls[callId].producer.id - // }); + callback({ + id: videoCalls[callId].producer.id + }); } catch (error) { console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`); } @@ -321,6 +321,7 @@ peers.on('connection', async socket => { callback({ params }); } else { console.log(`[canConsume] Can't consume | callId ${callId}`); + callback(null); } } catch (error) { console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)