Compare commits

..

11 Commits

Author SHA1 Message Date
bbf23c33d4 Merge pull request 'LH-252: Update .env variables' (#11) from LH-252-mediasoup-add-a-config-file-with-keys-and-ports into master
Reviewed-on: #11
2022-10-09 06:50:45 +00:00
5c2808e75a LH-252: Update .env variables 2022-10-06 15:21:54 +03:00
2aea7497cc Merge pull request 'added log for dtls transport-connect' (#10) from LH-249-debug-for-i-os-dtls-problems into master
Reviewed-on: #10
2022-10-06 06:41:07 +00:00
56835d6660 added log for dtls transport-connect 2022-10-05 15:44:46 +03:00
fc42c79210 Fix missing callId 2022-09-27 13:13:29 +03:00
d81bc8582d Merge branch 'master' of https://git.safemobile.org/Safemobile/mediasoup 2022-09-27 13:05:15 +03:00
a4d16998cd Fix call check before call close() 2022-09-27 13:03:32 +03:00
de1458bbde Merge pull request 'LINXD-2197: Added comments; Catch errors; Fix package.json start:run script' (#8) from LINXD-2197-refactor-improving-mediasoup-web-socket-component into master
Reviewed-on: #8
2022-09-27 10:00:25 +00:00
b0fad5f1db LINXD-2197: On peer disconnect delete the call; Added log when call is already deleted; Added log when user send multiple createWebRtcTransport 2022-09-27 12:43:07 +03:00
eb5aa12d65 LINXD-2197: Added the initial demo project used; Check before set producerTransport and consumerTransport if it was set before 2022-09-27 07:55:25 +03:00
52b4794a86 LINXD-2197: Added workflow diagram 2022-09-25 20:29:32 +03:00
4 changed files with 53 additions and 24 deletions

4
.env
View File

@ -1,3 +1,7 @@
PORT=3000
IP=0.0.0.0 # Listening IPv4 or IPv6.
ANNOUNCED_IP=185.8.154.190 # Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).
RTC_MIN_PORT=2000
RTC_MAX_PORT=2020
SERVER_CERT="./server/ssl/cert.pem"
SERVER_KEY="./server/ssl/key.pem"

View File

@ -32,4 +32,8 @@ accountId = account id of the unit on which you are doing the test
producer = it will always be true because you are the producer
(it's possible to put false, but then you have to have another client with producer true)
assetName = asset name of the unit on which you are doing the test
assetType = asset type of the unit on which you are doing the test
assetType = asset type of the unit on which you are doing the test
### Demo project
The demo project used initially and then modified for our needs `https://github.com/jamalag/mediasoup2`

67
app.js
View File

@ -5,7 +5,7 @@ const app = express();
const Server = require('socket.io');
const path = require('node:path');
const fs = require('node:fs');
let https = require('https');
let https;
try {
https = require('node:https');
} catch (err) {
@ -42,8 +42,8 @@ app.use('/sfu', express.static(path.join(__dirname, 'public')))
// SSL cert for HTTPS access
const options = {
key: fs.readFileSync('./server/ssl/key.pem', 'utf-8'),
cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8'),
key: fs.readFileSync(process.env.SERVER_KEY, 'utf-8'),
cert: fs.readFileSync(process.env.SERVER_CERT, 'utf-8'),
}
const httpsServer = https.createServer(options, app);
@ -59,16 +59,16 @@ const io = new Server(httpsServer, {
// const io = new Server(server, { origins: '*:*', allowEIO3: true });
httpsServer.listen(process.env.PORT, () => {
console.log('Video server listening on port:', process.env.PORT)
})
console.log('Video server listening on port:', process.env.PORT);
});
const peers = io.of('/')
const peers = io.of('/');
const createWorker = async () => {
try {
worker = await mediasoup.createWorker({
rtcMinPort: 2000,
rtcMaxPort: 2020,
rtcMinPort: process.env.RTC_MIN_PORT,
rtcMaxPort: process.env.RTC_MAX_PORT,
})
console.log(`[createWorker] worker pid ${worker.pid}`);
@ -84,7 +84,7 @@ const createWorker = async () => {
}
// We create a Worker as soon as our application starts
worker = createWorker()
worker = createWorker();
// This is an Array of RtpCapabilities
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
@ -105,17 +105,19 @@ const mediaCodecs = [
'x-google-start-bitrate': 1000,
},
},
]
];
const closeCall = (callId) => {
try {
if (videoCalls[callId]) {
videoCalls[callId].producer?.close();
videoCalls[callId].consumer?.close();
videoCalls[callId]?.consumerTransport.close();
videoCalls[callId]?.producerTransport.close();
videoCalls[callId].router.close();
videoCalls[callId]?.consumerTransport?.close();
videoCalls[callId]?.producerTransport?.close();
videoCalls[callId]?.router?.close();
delete videoCalls[callId];
} else {
console.log(`The call with id ${callId} has already been deleted`);
}
} catch (error) {
console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`);
@ -146,8 +148,10 @@ peers.on('connection', async socket => {
// It is triggered when the peer is disconnected
socket.on('disconnect', () => {
console.log('peer disconnected | socket.id', socket.id);
const callId = socketDetails[socket.id];
console.log(`disconnect | socket ${socket.id} | callId ${callId}`);
delete socketDetails[socket.id];
closeCall(callId);
});
/*
@ -187,12 +191,20 @@ peers.on('connection', async socket => {
const callId = socketDetails[socket.id];
console.log(`[createWebRtcTransport] sender ${sender} | callId ${callId}`);
if (sender) {
videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback);
} else {
videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback);
if (!videoCalls[callId].producerTransport) {
videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback);
} else {
console.log(`producerTransport has already been defined | callId ${callId}`);
}
} else if (!sender) {
if (!videoCalls[callId].consumerTransport) {
videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback);
} else {
console.log(`consumerTransport has already been defined | callId ${callId}`);
}
}
} catch (error) {
console.log(`ERROR | createWebRtcTransport | callId ${callId} | sender ${sender} | ${error.message}`);
console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
}
});
@ -203,7 +215,9 @@ peers.on('connection', async socket => {
socket.on('transport-connect', async ({ dtlsParameters }) => {
try {
const callId = socketDetails[socket.id];
console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId}`)
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId}`);
await videoCalls[callId].producerTransport.connect({ dtlsParameters });
} catch (error) {
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
@ -215,9 +229,11 @@ peers.on('connection', async socket => {
- 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 }) => {
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] | socket.id', socket.id, '| callId', callId);
videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({
kind,
@ -230,6 +246,11 @@ peers.on('connection', async socket => {
console.log('transport for this producer closed', callId)
closeCall(callId);
});
// Send back to the client the Producer's id
// callback({
// id: videoCalls[callId].producer.id
// });
} catch (error) {
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
}
@ -245,7 +266,7 @@ peers.on('connection', async socket => {
console.log(`[transport-recv-connect] socket.id ${socket.id} | callId ${callId}`);
await videoCalls[callId].consumerTransport.connect({ dtlsParameters });
} catch (error) {
console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ERROR`);
console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
}
})
@ -302,7 +323,7 @@ peers.on('connection', async socket => {
console.log(`[canConsume] Can't consume | callId ${callId}`);
}
} catch (error) {
console.log(`ERROR | consume | callId ${callId} | ${error.message}`)
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
callback({ params: { error } });
}
});
@ -376,7 +397,7 @@ const createWebRtcTransportLayer = async (callId, callback) => {
return transport;
} catch (error) {
console.log(`ERROR | createWebRtcTransportLayer | callId ${callId} | ${error.message}`);
console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
callback({ params: { error } });
}
}

BIN
doc/[video] Workflow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 KiB