Compare commits
31 Commits
5f8f2ab44c
...
LINXD-2180
Author | SHA1 | Date | |
---|---|---|---|
13829c5e6c | |||
9047e6c47d | |||
77da298944 | |||
c25e34bc37 | |||
9a25f16924 | |||
473f94fcbb | |||
9245373625 | |||
75d0e3aee7 | |||
30ac997634 | |||
5aea138f6a | |||
5b01ddc2a8 | |||
084ff36ebe | |||
f4ebf92783 | |||
b59a157b18 | |||
9f8347bec5 | |||
24390c98e5 | |||
1a7371fe18 | |||
be5f97762a | |||
03a11126c4 | |||
fafbee6e4c | |||
bbf23c33d4 | |||
5c2808e75a | |||
2aea7497cc | |||
56835d6660 | |||
fc42c79210 | |||
d81bc8582d | |||
a4d16998cd | |||
de1458bbde | |||
b0fad5f1db | |||
eb5aa12d65 | |||
52b4794a86 |
@ -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"
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/node_modules
|
||||
/dist
|
||||
|
@ -33,3 +33,7 @@ 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
|
||||
|
||||
### Demo project
|
||||
The demo project used initially and then modified for our needs `https://github.com/jamalag/mediasoup2`
|
||||
|
||||
|
94
app.js
94
app.js
@ -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: parseInt(process.env.RTC_MIN_PORT),
|
||||
rtcMaxPort: parseInt(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,33 +105,25 @@ const mediaCodecs = [
|
||||
'x-google-start-bitrate': 1000,
|
||||
},
|
||||
},
|
||||
]
|
||||
];
|
||||
|
||||
const closeCall = (callId) => {
|
||||
try {
|
||||
if (videoCalls[callId]) {
|
||||
if (callId && 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}`);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@ -146,8 +138,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);
|
||||
});
|
||||
|
||||
/*
|
||||
@ -156,21 +150,30 @@ 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}`);
|
||||
console.log(`[createRoom] --🟥-- socket.id ${socket.id} callId ${callId}`);
|
||||
if (!videoCalls[callId]) {
|
||||
console.log('[createRoom] callId', callId);
|
||||
videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) }
|
||||
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 = {
|
||||
rtpCapabilities :videoCalls[callId].router.rtpCapabilities
|
||||
};
|
||||
} else {
|
||||
console.log(`[createRoom] missing callId ${callId}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`);
|
||||
} finally {
|
||||
callback(callbackResponse);
|
||||
}
|
||||
});
|
||||
|
||||
@ -187,12 +190,23 @@ peers.on('connection', async socket => {
|
||||
const callId = socketDetails[socket.id];
|
||||
console.log(`[createWebRtcTransport] sender ${sender} | callId ${callId}`);
|
||||
if (sender) {
|
||||
if (!videoCalls[callId].producerTransport) {
|
||||
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 ${callId} | sender ${sender} | ${error.message}`);
|
||||
console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
|
||||
callback(error);
|
||||
}
|
||||
});
|
||||
|
||||
@ -203,7 +217,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 +231,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 +248,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 && callback({
|
||||
id: videoCalls[callId].producer.id
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||
}
|
||||
@ -245,7 +268,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}`);
|
||||
}
|
||||
})
|
||||
|
||||
@ -278,14 +301,14 @@ peers.on('connection', async socket => {
|
||||
videoCalls[callId].consumer.on('transportclose', () => {
|
||||
const callId = socketDetails[socket.id];
|
||||
console.log('transport close from consumer', callId);
|
||||
closeCall();
|
||||
closeCall(callId);
|
||||
});
|
||||
|
||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
||||
videoCalls[callId].consumer.on('producerclose', () => {
|
||||
const callId = socketDetails[socket.id];
|
||||
console.log('producer of consumer closed', callId);
|
||||
closeCall();
|
||||
closeCall(callId);
|
||||
});
|
||||
|
||||
// From the consumer extract the following params to send back to the Client
|
||||
@ -300,9 +323,10 @@ 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 ${callId} | ${error.message}`)
|
||||
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
|
||||
callback({ params: { error } });
|
||||
}
|
||||
});
|
||||
@ -376,7 +400,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 } });
|
||||
}
|
||||
}
|
48
build.sh
Executable file
48
build.sh
Executable 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 .env.example .env
|
||||
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 -
|
||||
rm .env
|
BIN
doc/[video] Workflow.png
Normal file
BIN
doc/[video] Workflow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 571 KiB |
Reference in New Issue
Block a user