Compare commits

..

21 Commits

Author SHA1 Message Date
df3482ac15 LH-265: Enable audio on video server 2022-11-21 23:02:49 +02:00
75d0e3aee7 exe right for build.sh 2022-10-31 22:26:08 +00:00
30ac997634 Merge pull request 'added build.sh' (#14) from temp-build into develop
Reviewed-on: #14
2022-10-31 22:22:20 +00:00
5aea138f6a added build.sh 2022-10-31 12:17:07 +02:00
5b01ddc2a8 Merge pull request 'LH-259-mediasoup-always-return-a-callback-response-to-clients' (#13) from LH-259-mediasoup-always-return-a-callback-response-to-clients into develop
Reviewed-on: #13
2022-10-25 16:18:54 +00:00
084ff36ebe LH-259: Refactor createRoom 2022-10-24 22:38:06 +03:00
f4ebf92783 LH-259: Added callback from transport-produce 2022-10-24 22:35:22 +03:00
b59a157b18 LH-259: Comment callback from transport-produce 2022-10-24 22:19:37 +03:00
9f8347bec5 LH-259: Update createRoom callback 2022-10-24 22:16:47 +03:00
24390c98e5 LH-259: Added callbacks 2022-10-24 22:11:14 +03:00
1a7371fe18 Parse RTC_MIN_PORT and RTC_MAX_PORT 2022-10-18 18:27:02 +03:00
be5f97762a Merge pull request 'LH-253: Added callId for transportclose and producerclose events' (#12) from LH-253-mediasoup-handle-callid-undefined into master
Reviewed-on: #12
2022-10-18 07:53:33 +00:00
03a11126c4 LH-253: Check if we have callId in closeCall 2022-10-18 10:51:20 +03:00
fafbee6e4c LH-253: Added callId for transportclose and producerclose events 2022-10-18 02:05:22 +03:00
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
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
5 changed files with 95 additions and 30 deletions

4
.env
View File

@ -1,3 +1,7 @@
PORT=3000 PORT=3000
IP=0.0.0.0 # Listening IPv4 or IPv6. 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). 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"

1
.gitignore vendored
View File

@ -1 +1,2 @@
/node_modules /node_modules
/dist

70
app.js
View File

@ -5,7 +5,7 @@ const app = express();
const Server = require('socket.io'); const Server = require('socket.io');
const path = require('node:path'); const path = require('node:path');
const fs = require('node:fs'); const fs = require('node:fs');
let https = require('https'); let https;
try { try {
https = require('node:https'); https = require('node:https');
} catch (err) { } catch (err) {
@ -42,8 +42,8 @@ app.use('/sfu', express.static(path.join(__dirname, 'public')))
// SSL cert for HTTPS access // SSL cert for HTTPS access
const options = { const options = {
key: fs.readFileSync('./server/ssl/key.pem', 'utf-8'), key: fs.readFileSync(process.env.SERVER_KEY, 'utf-8'),
cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8'), cert: fs.readFileSync(process.env.SERVER_CERT, 'utf-8'),
} }
const httpsServer = https.createServer(options, app); const httpsServer = https.createServer(options, app);
@ -59,16 +59,16 @@ const io = new Server(httpsServer, {
// const io = new Server(server, { origins: '*:*', allowEIO3: true }); // const io = new Server(server, { origins: '*:*', allowEIO3: true });
httpsServer.listen(process.env.PORT, () => { 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 () => { const createWorker = async () => {
try { try {
worker = await mediasoup.createWorker({ worker = await mediasoup.createWorker({
rtcMinPort: 2000, rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
rtcMaxPort: 2020, rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
}) })
console.log(`[createWorker] worker pid ${worker.pid}`); console.log(`[createWorker] worker pid ${worker.pid}`);
@ -84,7 +84,7 @@ const createWorker = async () => {
} }
// We create a Worker as soon as our application starts // We create a Worker as soon as our application starts
worker = createWorker() worker = createWorker();
// This is an Array of RtpCapabilities // This is an Array of RtpCapabilities
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability // https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
@ -105,11 +105,11 @@ const mediaCodecs = [
'x-google-start-bitrate': 1000, 'x-google-start-bitrate': 1000,
}, },
}, },
] ];
const closeCall = (callId) => { const closeCall = (callId) => {
try { try {
if (videoCalls[callId]) { if (callId && videoCalls[callId]) {
videoCalls[callId].producer?.close(); videoCalls[callId].producer?.close();
videoCalls[callId].consumer?.close(); videoCalls[callId].consumer?.close();
videoCalls[callId]?.consumerTransport?.close(); videoCalls[callId]?.consumerTransport?.close();
@ -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 - Handlers for WS events
- These are created only when we have a connection with a peer - 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 - If the room already exists, it will not create it, but will only return rtpCapabilities
*/ */
socket.on('createRoom', async ({ callId }, callback) => { socket.on('createRoom', async ({ callId }, callback) => {
let callbackResponse = null;
try { try {
// We can continue with the room creation process only if we have a callId
if (callId) { if (callId) {
console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`); console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`);
if (!videoCalls[callId]) { if (!videoCalls[callId]) {
@ -169,12 +161,19 @@ peers.on('connection', async socket => {
console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`); console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
} }
socketDetails[socket.id] = callId; socketDetails[socket.id] = callId;
getRtpCapabilities(callId, callback);
// rtpCapabilities is set for callback
console.log('[getRtpCapabilities] callId', callId);
callbackResponse = {
rtpCapabilities :videoCalls[callId].router.rtpCapabilities
};
} else { } else {
console.log(`[createRoom] missing callId ${callId}`); console.log(`[createRoom] missing callId ${callId}`);
} }
} catch (error) { } catch (error) {
console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`); console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`);
} finally {
callback(callbackResponse);
} }
}); });
@ -195,16 +194,19 @@ peers.on('connection', async socket => {
videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback); videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback);
} else { } else {
console.log(`producerTransport has already been defined | callId ${callId}`); console.log(`producerTransport has already been defined | callId ${callId}`);
callback(null);
} }
} else if (!sender) { } else if (!sender) {
if (!videoCalls[callId].consumerTransport) { if (!videoCalls[callId].consumerTransport) {
videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback); videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback);
} else { } else {
console.log(`consumerTransport has already been defined | callId ${callId}`); console.log(`consumerTransport has already been defined | callId ${callId}`);
callback(null);
} }
} }
} catch (error) { } catch (error) {
console.log(`ERROR | createWebRtcTransport | callId ${callId} | sender ${sender} | ${error.message}`); console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
callback(error);
} }
}); });
@ -215,7 +217,9 @@ peers.on('connection', async socket => {
socket.on('transport-connect', async ({ dtlsParameters }) => { socket.on('transport-connect', async ({ dtlsParameters }) => {
try { try {
const callId = socketDetails[socket.id]; 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 }); await videoCalls[callId].producerTransport.connect({ dtlsParameters });
} catch (error) { } catch (error) {
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`); console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
@ -227,9 +231,11 @@ peers.on('connection', async socket => {
- For the router with the id callId, we make produce on producerTransport - For the router with the id callId, we make produce on producerTransport
- Create the handler on producer at the 'transportclose' event - 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 { try {
const callId = socketDetails[socket.id]; const callId = socketDetails[socket.id];
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
console.log('[transport-produce] | socket.id', socket.id, '| callId', callId); console.log('[transport-produce] | socket.id', socket.id, '| callId', callId);
videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({ videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({
kind, kind,
@ -242,6 +248,11 @@ peers.on('connection', async socket => {
console.log('transport for this producer closed', callId) console.log('transport for this producer closed', callId)
closeCall(callId); closeCall(callId);
}); });
// Send back to the client the Producer's id
callback && callback({
id: videoCalls[callId].producer.id
});
} catch (error) { } catch (error) {
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`); console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
} }
@ -290,14 +301,14 @@ peers.on('connection', async socket => {
videoCalls[callId].consumer.on('transportclose', () => { videoCalls[callId].consumer.on('transportclose', () => {
const callId = socketDetails[socket.id]; const callId = socketDetails[socket.id];
console.log('transport close from consumer', callId); console.log('transport close from consumer', callId);
closeCall(); closeCall(callId);
}); });
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose // https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
videoCalls[callId].consumer.on('producerclose', () => { videoCalls[callId].consumer.on('producerclose', () => {
const callId = socketDetails[socket.id]; const callId = socketDetails[socket.id];
console.log('producer of consumer closed', callId); console.log('producer of consumer closed', callId);
closeCall(); closeCall(callId);
}); });
// From the consumer extract the following params to send back to the Client // From the consumer extract the following params to send back to the Client
@ -312,9 +323,10 @@ peers.on('connection', async socket => {
callback({ params }); callback({ params });
} else { } else {
console.log(`[canConsume] Can't consume | callId ${callId}`); console.log(`[canConsume] Can't consume | callId ${callId}`);
callback(null);
} }
} catch (error) { } catch (error) {
console.log(`ERROR | consume | callId ${callId} | ${error.message}`) console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
callback({ params: { error } }); callback({ params: { error } });
} }
}); });
@ -388,7 +400,7 @@ const createWebRtcTransportLayer = async (callId, callback) => {
return transport; return transport;
} catch (error) { } catch (error) {
console.log(`ERROR | createWebRtcTransportLayer | callId ${callId} | ${error.message}`); console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
callback({ params: { error } }); callback({ params: { error } });
} }
} }

48
build.sh Executable file
View File

@ -0,0 +1,48 @@
#/!bin/bash
## PREBUILD PROCESS
# check dist dir to be present and empty
if [ ! -d "dist" ]; then
## MAKE DIR
mkdir "dist"
echo "Directory dist created."
else
## CLEANUP
rm -fr dist/*
fi
# Install dependencies
#npm install
## PROJECT NEEDS
echo "Building app... from $(git rev-parse --abbrev-ref HEAD)"
#npm run-script build
cp -r {.env,app.js,package.json,server,public} dist/
#Add version control for pm2
cd dist
#Add version control for pm2
version=$(git describe)
file_pkg="package.json"
key=" \"version\": \""
count=$(echo ${version%%-*} | grep -o "\." | wc -l)
if (( $count > 1 )); then
version=${version%%-*}
else
version="${version%%-*}.0"
fi
if [ -f "$file_pkg" ] && [ ! -z "$version" ]; then
version=" \"version\": \"$version\","
sed -i "s|^.*$key.*|${version//\//\\/}|g" $file_pkg
text=$(cat $file_pkg | grep -c "$version")
if [ $text -eq 0 ]; then
echo "Version couldn't be set"
else
echo "Version $version successfully applied to App"
fi
fi
## POST BUILD
cd -

View File

@ -135,7 +135,7 @@ const streamSuccess = (stream) => {
const getLocalStream = () => { const getLocalStream = () => {
console.log('[getLocalStream]'); console.log('[getLocalStream]');
navigator.mediaDevices.getUserMedia({ navigator.mediaDevices.getUserMedia({
audio: false, audio: true,
video: { video: {
width: { width: {
min: 640, min: 640,