Refactor code; Added logs; Added env variables

This commit is contained in:
Sergiu Toma 2022-07-29 11:32:51 +03:00
parent 5c88cd966d
commit 26f3122b96
8 changed files with 117 additions and 86 deletions

3
.env Normal file
View File

@ -0,0 +1,3 @@
PORT=3000
IP=0.0.0.0# Listening IPv4 or IPv6.
ANNOUNCED_IP=127.0.0.1 # Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).

39
app.js
View File

@ -1,3 +1,5 @@
import 'dotenv/config'
/** /**
* integrating mediasoup server with a node.js application * integrating mediasoup server with a node.js application
*/ */
@ -22,7 +24,7 @@ let consumerTransport
let producer let producer
let consumer let consumer
app.get('/', (req, res) => { app.get('/', (_req, res) => {
res.send('Hello from mediasoup app!') res.send('Hello from mediasoup app!')
}) })
@ -35,8 +37,9 @@ const options = {
} }
const httpsServer = https.createServer(options, app) const httpsServer = https.createServer(options, app)
httpsServer.listen(3000, () => {
console.log('listening on port: ' + 3000) httpsServer.listen(process.env.PORT, () => {
console.log('Listening on port:', process.env.PORT)
}) })
const io = new Server(httpsServer) const io = new Server(httpsServer)
@ -58,11 +61,11 @@ const createWorker = async () => {
rtcMinPort: 2000, rtcMinPort: 2000,
rtcMaxPort: 2020, rtcMaxPort: 2020,
}) })
console.log(`worker pid ${worker.pid}`) console.log(`[createWorker] worker pid ${worker.pid}`)
worker.on('died', error => { worker.on('died', error => {
// This implies something serious happened, so kill the application // This implies something serious happened, so kill the application
console.error('mediasoup worker has died') console.error('mediasoup worker has died', error)
setTimeout(() => process.exit(1), 2000) // exit in 2 seconds setTimeout(() => process.exit(1), 2000) // exit in 2 seconds
}) })
@ -94,11 +97,13 @@ const mediaCodecs = [
] ]
peers.on('connection', async socket => { peers.on('connection', async socket => {
console.log(socket.id) console.log('[connection] socketId:', socket.id)
setTimeout(() => {
socket.emit('connection-success', { socket.emit('connection-success', {
socketId: socket.id, socketId: socket.id,
existsProducer: producer ? true : false, existsProducer: producer ? true : false,
}) })
}, 1000);
socket.on('disconnect', () => { socket.on('disconnect', () => {
// do some cleanup // do some cleanup
@ -113,7 +118,7 @@ peers.on('connection', async socket => {
// appData -> custom application data - we are not supplying any // appData -> custom application data - we are not supplying any
// none of the two are required // none of the two are required
router[callId] = await worker.createRouter({ mediaCodecs }) router[callId] = await worker.createRouter({ mediaCodecs })
console.log(`Router ID: ${router[callId].id}`) console.log(`[createRoom] Router ID: ${router[callId].id}`)
} }
getRtpCapabilities(callId, callback) getRtpCapabilities(callId, callback)
@ -128,7 +133,7 @@ peers.on('connection', async socket => {
// Client emits a request to create server side Transport // Client emits a request to create server side Transport
// We need to differentiate between the producer and consumer transports // We need to differentiate between the producer and consumer transports
socket.on('createWebRtcTransport', async ({ sender, callId }, callback) => { socket.on('createWebRtcTransport', async ({ sender, callId }, callback) => {
console.log(`Is this a sender request? ${sender} | callId ${callId}`) console.log(`[createWebRtcTransport] Is this a sender request? ${sender} | callId ${callId}`)
// The client indicates if it is a producer or a consumer // The client indicates if it is a producer or a consumer
// if sender is true, indicates a producer else a consumer // if sender is true, indicates a producer else a consumer
if (sender) if (sender)
@ -139,7 +144,7 @@ peers.on('connection', async socket => {
// see client's socket.emit('transport-connect', ...) // see client's socket.emit('transport-connect', ...)
socket.on('transport-connect', async ({ dtlsParameters }) => { socket.on('transport-connect', async ({ dtlsParameters }) => {
console.log('DTLS PARAMS... ', { dtlsParameters }) console.log('[transport-connect] DTLS PARAMS... ', { dtlsParameters })
await producerTransport.connect({ dtlsParameters }) await producerTransport.connect({ dtlsParameters })
}) })
@ -151,7 +156,7 @@ peers.on('connection', async socket => {
rtpParameters, rtpParameters,
}) })
console.log('Producer ID: ', producer.id, producer.kind) console.log(`[transport-produce] Producer ID: ${producer.id} | kind: ${producer.kind}`)
producer.on('transportclose', () => { producer.on('transportclose', () => {
console.log('transport for this producer closed ') console.log('transport for this producer closed ')
@ -166,7 +171,7 @@ peers.on('connection', async socket => {
// see client's socket.emit('transport-recv-connect', ...) // see client's socket.emit('transport-recv-connect', ...)
socket.on('transport-recv-connect', async ({ dtlsParameters }) => { socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
console.log(`DTLS PARAMS: ${dtlsParameters}`) console.log(`[transport-recv-connect] DTLS PARAMS: ${dtlsParameters}`)
await consumerTransport.connect({ dtlsParameters }) await consumerTransport.connect({ dtlsParameters })
}) })
@ -215,30 +220,29 @@ peers.on('connection', async socket => {
}) })
socket.on('consumer-resume', async () => { socket.on('consumer-resume', async () => {
console.log('consumer resume') console.log(`[consumer-resume]`)
await consumer.resume() await consumer.resume()
}) })
}) })
const createWebRtcTransportLayer = async (callId, callback) => { const createWebRtcTransportLayer = async (callId, callback) => {
try { try {
console.log('createWebRtcTransportLayer | callId', callId);
// https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions // https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
const webRtcTransport_options = { const webRtcTransport_options = {
listenIps: [ listenIps: [
{ {
ip: '0.0.0.0', // replace with relevant IP address ip: process.env.IP, // Listening IPv4 or IPv6.
announcedIp: '127.0.0.1', announcedIp: process.env.ANNOUNCED_IP, // Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).
} }
], ],
enableUdp: true, enableUdp: true,
enableTcp: true, enableTcp: true,
preferUdp: true, preferUdp: true,
} }
console.log('🔴 router[callId]', JSON.stringify(router));
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport // https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
let transport = await router[callId].createWebRtcTransport(webRtcTransport_options) let transport = await router[callId].createWebRtcTransport(webRtcTransport_options)
console.log(`transport id: ${transport.id}`) console.log(`callId: ${callId} | transport id: ${transport.id}`)
transport.on('dtlsstatechange', dtlsState => { transport.on('dtlsstatechange', dtlsState => {
if (dtlsState === 'closed') { if (dtlsState === 'closed') {
@ -264,7 +268,6 @@ const createWebRtcTransportLayer = async (callId, callback) => {
return transport return transport
} catch (error) { } catch (error) {
console.log('11111', error)
callback({ callback({
params: { params: {
error: error error: error

14
package-lock.json generated
View File

@ -9,6 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^16.0.1",
"express": "^4.18.1", "express": "^4.18.1",
"httpolyglot": "^0.1.2", "httpolyglot": "^0.1.2",
"mediasoup": "^3.10.4", "mediasoup": "^3.10.4",
@ -919,6 +920,14 @@
"npm": ">=1.2" "npm": ">=1.2"
} }
}, },
"node_modules/dotenv": {
"version": "16.0.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz",
"integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==",
"engines": {
"node": ">=12"
}
},
"node_modules/duplexer2": { "node_modules/duplexer2": {
"version": "0.1.4", "version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz",
@ -4263,6 +4272,11 @@
"integrity": "sha512-jnjyiM6eRyZl2H+W8Q/zLMA481hzi0eszAaBUzIVnmYVDBbnLxVNnfu1HgEBvCbL+71FrxMl3E6lpKH7Ge3OXA==", "integrity": "sha512-jnjyiM6eRyZl2H+W8Q/zLMA481hzi0eszAaBUzIVnmYVDBbnLxVNnfu1HgEBvCbL+71FrxMl3E6lpKH7Ge3OXA==",
"dev": true "dev": true
}, },
"dotenv": {
"version": "16.0.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz",
"integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ=="
},
"duplexer2": { "duplexer2": {
"version": "0.1.4", "version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz",

View File

@ -13,12 +13,15 @@
"license": "ISC", "license": "ISC",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"dotenv": "^16.0.1",
"express": "^4.18.1", "express": "^4.18.1",
"httpolyglot": "^0.1.2", "httpolyglot": "^0.1.2",
"mediasoup": "^3.10.4", "mediasoup": "^3.10.4",
"mediasoup-client": "^3.6.54", "mediasoup-client": "^3.6.54",
"socket.io": "^2.0.3", "socket.io": "^2.0.3",
"socket.io-client": "^2.0.3" "socket.io-client": "^2.0.3",
"nodemon": "^2.0.19",
"watchify": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^2.0.19", "nodemon": "^2.0.19",

View File

@ -20651,22 +20651,45 @@ yeast.decode = decode;
module.exports = yeast; module.exports = yeast;
},{}],95:[function(require,module,exports){ },{}],95:[function(require,module,exports){
module.exports = {
hubAddress: 'https://hub.dev.linx.safemobile.com/',
mediasoupAddress: 'https://localhost:3000/mediasoup',
}
},{}],96:[function(require,module,exports){
const io = require('socket.io-client') const io = require('socket.io-client')
const mediasoupClient = require('mediasoup-client') const mediasoupClient = require('mediasoup-client')
const urlParams = new URLSearchParams(location.search); const urlParams = new URLSearchParams(location.search);
const config = require('./config')
console.log('[CONFIG]', config);
const ASSET_ID = parseInt(urlParams.get('assetId')) || null; const ASSET_ID = parseInt(urlParams.get('assetId')) || null;
const ACCOUNT_ID = parseInt(urlParams.get('accountId')) || null; const ACCOUNT_ID = parseInt(urlParams.get('accountId')) || null;
let callId = parseInt(urlParams.get('callId')) || null; let callId = parseInt(urlParams.get('callId')) || null;
const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false
console.log('ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER) console.log('[URL] ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER)
let socket let socket
hub = io('https://hub.dev.linx.safemobile.com/') hub = io(config.hubAddress)
const connectToMediasoup = () => {
socket = io(config.mediasoupAddress)
socket.on('connection-success', ({ _socketId, existsProducer }) => {
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() }
})
}
if (IS_PRODUCER === true) { if (IS_PRODUCER === true) {
hub.on('connect', async () => { hub.on('connect', async () => {
console.log('hub.connected', hub.connected); console.log(`[HUB] ${config.hubAddress} | connected: ${hub.connected}`)
connectToMediasoup()
hub.emit( hub.emit(
'ars', 'ars',
JSON.stringify({ JSON.stringify({
@ -20689,11 +20712,6 @@ if (IS_PRODUCER === true) {
if (parsedData.type === 'notify-request' && IS_PRODUCER) { if (parsedData.type === 'notify-request' && IS_PRODUCER) {
getLocalStream() getLocalStream()
} }
// setTimeout(() => {
// !IS_PRODUCER && goConnect()
// }, 1000);
// getRtpCapabilities()
}) })
}) })
@ -20708,23 +20726,10 @@ if (IS_PRODUCER === true) {
hub.on('disconnect', () => { hub.on('disconnect', () => {
console.log('disconnect') console.log('disconnect')
}) })
} else {
connectToMediasoup()
} }
socket = io('https://localhost:3000/mediasoup')
socket.on('connection-success', ({ socketId, existsProducer }) => {
console.log(socketId, existsProducer)
// if (IS_PRODUCER) { getLocalStream() }
// setInterval(() => {
// if (!IS_PRODUCER && existsProducer) { goConnect() }
// }, 5000);
if (!IS_PRODUCER && existsProducer && consumer === undefined) { goConnect() }
})
let device let device
let rtpCapabilities let rtpCapabilities
let producerTransport let producerTransport
@ -20769,11 +20774,11 @@ const streamSuccess = (stream) => {
track, track,
...params ...params
} }
goConnect() goConnect()
} }
const getLocalStream = () => { const getLocalStream = () => {
console.log('[getLocalStream]');
navigator.mediaDevices.getUserMedia({ navigator.mediaDevices.getUserMedia({
audio: false, audio: false,
video: { video: {
@ -20925,7 +20930,7 @@ const connectSendTransport = async () => {
const answer = { const answer = {
origin_asset_id: ASSET_ID, origin_asset_id: ASSET_ID,
dest_asset_id: originAssetId, dest_asset_id: originAssetId || parseInt(urlParams.get('dest_asset_id')),
type: 'notify-answer', type: 'notify-answer',
origin_asset_priority: 1, origin_asset_priority: 1,
origin_asset_type_name: originAssetTypeName, origin_asset_type_name: originAssetTypeName,
@ -20978,7 +20983,6 @@ const createRecvTransport = async () => {
errback(error) errback(error)
} }
}) })
connectRecvTransport() connectRecvTransport()
}) })
} }
@ -20997,7 +21001,6 @@ const connectRecvTransport = async () => {
return return
} }
console.log(params)
// then consume with the local consumer transport // then consume with the local consumer transport
// which creates a consumer // which creates a consumer
consumer = await consumerTransport.consume({ consumer = await consumerTransport.consume({
@ -21010,16 +21013,16 @@ const connectRecvTransport = async () => {
// destructure and retrieve the video track from the producer // destructure and retrieve the video track from the producer
const { track } = consumer const { track } = consumer
const stream = new MediaStream([track]) let stream = new MediaStream()
stream.addTrack(track)
// stream.removeTrack(track)
remoteVideo.srcObject = stream remoteVideo.srcObject = stream
// the server consumer started with media paused
// so we need to inform the server to resume
socket.emit('consumer-resume') socket.emit('consumer-resume')
console.log('consumer', consumer); console.log('consumer', consumer);
}) })
} }
btnLocalVideo.addEventListener('click', getLocalStream) btnLocalVideo.addEventListener('click', getLocalStream)
btnRecvSendTransport.addEventListener('click', goConnect) btnRecvSendTransport.addEventListener('click', goConnect)
},{"mediasoup-client":67,"socket.io-client":83}]},{},[95]); },{"./config":95,"mediasoup-client":67,"socket.io-client":83}]},{},[96]);

4
public/config.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
hubAddress: 'https://hub.dev.linx.safemobile.com/',
mediasoupAddress: 'https://localhost:3000/mediasoup',
}

View File

@ -55,6 +55,9 @@
<button id="btnRecvSendTransport">Consume</button> <button id="btnRecvSendTransport">Consume</button>
</div> </div>
</td> </td>
<td class="remoteColumn">
<div id="videoContainer"></div>
</td>
</tr> </tr>
<!-- <tr> <!-- <tr>
<td colspan="2"> <td colspan="2">

View File

@ -1,19 +1,37 @@
const io = require('socket.io-client') const io = require('socket.io-client')
const mediasoupClient = require('mediasoup-client') const mediasoupClient = require('mediasoup-client')
const urlParams = new URLSearchParams(location.search); const urlParams = new URLSearchParams(location.search);
const config = require('./config')
console.log('[CONFIG]', config);
const ASSET_ID = parseInt(urlParams.get('assetId')) || null; const ASSET_ID = parseInt(urlParams.get('assetId')) || null;
const ACCOUNT_ID = parseInt(urlParams.get('accountId')) || null; const ACCOUNT_ID = parseInt(urlParams.get('accountId')) || null;
let callId = parseInt(urlParams.get('callId')) || null; let callId = parseInt(urlParams.get('callId')) || null;
const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false
console.log('ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER) console.log('[URL] ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER)
let socket let socket
hub = io('https://hub.dev.linx.safemobile.com/') hub = io(config.hubAddress)
const connectToMediasoup = () => {
socket = io(config.mediasoupAddress)
socket.on('connection-success', ({ _socketId, existsProducer }) => {
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() }
})
}
if (IS_PRODUCER === true) { if (IS_PRODUCER === true) {
hub.on('connect', async () => { hub.on('connect', async () => {
console.log('hub.connected', hub.connected); console.log(`[HUB] ${config.hubAddress} | connected: ${hub.connected}`)
connectToMediasoup()
hub.emit( hub.emit(
'ars', 'ars',
JSON.stringify({ JSON.stringify({
@ -36,11 +54,6 @@ if (IS_PRODUCER === true) {
if (parsedData.type === 'notify-request' && IS_PRODUCER) { if (parsedData.type === 'notify-request' && IS_PRODUCER) {
getLocalStream() getLocalStream()
} }
// setTimeout(() => {
// !IS_PRODUCER && goConnect()
// }, 1000);
// getRtpCapabilities()
}) })
}) })
@ -55,23 +68,10 @@ if (IS_PRODUCER === true) {
hub.on('disconnect', () => { hub.on('disconnect', () => {
console.log('disconnect') console.log('disconnect')
}) })
} else {
connectToMediasoup()
} }
socket = io('https://localhost:3000/mediasoup')
socket.on('connection-success', ({ socketId, existsProducer }) => {
console.log(socketId, existsProducer)
// if (IS_PRODUCER) { getLocalStream() }
// setInterval(() => {
// if (!IS_PRODUCER && existsProducer) { goConnect() }
// }, 5000);
if (!IS_PRODUCER && existsProducer && consumer === undefined) { goConnect() }
})
let device let device
let rtpCapabilities let rtpCapabilities
let producerTransport let producerTransport
@ -116,11 +116,11 @@ const streamSuccess = (stream) => {
track, track,
...params ...params
} }
goConnect() goConnect()
} }
const getLocalStream = () => { const getLocalStream = () => {
console.log('[getLocalStream]');
navigator.mediaDevices.getUserMedia({ navigator.mediaDevices.getUserMedia({
audio: false, audio: false,
video: { video: {
@ -272,7 +272,7 @@ const connectSendTransport = async () => {
const answer = { const answer = {
origin_asset_id: ASSET_ID, origin_asset_id: ASSET_ID,
dest_asset_id: originAssetId, dest_asset_id: originAssetId || parseInt(urlParams.get('dest_asset_id')),
type: 'notify-answer', type: 'notify-answer',
origin_asset_priority: 1, origin_asset_priority: 1,
origin_asset_type_name: originAssetTypeName, origin_asset_type_name: originAssetTypeName,
@ -325,7 +325,6 @@ const createRecvTransport = async () => {
errback(error) errback(error)
} }
}) })
connectRecvTransport() connectRecvTransport()
}) })
} }
@ -344,7 +343,6 @@ const connectRecvTransport = async () => {
return return
} }
console.log(params)
// then consume with the local consumer transport // then consume with the local consumer transport
// which creates a consumer // which creates a consumer
consumer = await consumerTransport.consume({ consumer = await consumerTransport.consume({
@ -357,13 +355,13 @@ const connectRecvTransport = async () => {
// destructure and retrieve the video track from the producer // destructure and retrieve the video track from the producer
const { track } = consumer const { track } = consumer
const stream = new MediaStream([track]) let stream = new MediaStream()
stream.addTrack(track)
// stream.removeTrack(track)
remoteVideo.srcObject = stream remoteVideo.srcObject = stream
// the server consumer started with media paused
// so we need to inform the server to resume
socket.emit('consumer-resume') socket.emit('consumer-resume')
console.log('consumer', consumer); console.log('consumer', consumer);
}) })
} }