Compare commits
7 Commits
4.02
...
fix-branch
Author | SHA1 | Date | |
---|---|---|---|
2e336a429e | |||
b14e82fd87 | |||
c4f72eddd5 | |||
6e4ceb9977 | |||
2c00de1dd0 | |||
f96fd24e03 | |||
fce2f30648 |
49
app.js
49
app.js
@ -5,12 +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;
|
const https = require('https');
|
||||||
try {
|
|
||||||
https = require('node:https');
|
|
||||||
} catch (err) {
|
|
||||||
console.log('https support is disabled!');
|
|
||||||
}
|
|
||||||
const mediasoup = require('mediasoup');
|
const mediasoup = require('mediasoup');
|
||||||
|
|
||||||
let worker
|
let worker
|
||||||
@ -42,8 +37,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(process.env.SERVER_KEY, 'utf-8'),
|
key: fs.readFileSync('./server/ssl/key.pem', 'utf-8'),
|
||||||
cert: fs.readFileSync(process.env.SERVER_CERT, 'utf-8'),
|
cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8'),
|
||||||
}
|
}
|
||||||
|
|
||||||
const httpsServer = https.createServer(options, app);
|
const httpsServer = https.createServer(options, app);
|
||||||
@ -63,12 +58,13 @@ httpsServer.listen(process.env.PORT, () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const peers = io.of('/');
|
const peers = io.of('/');
|
||||||
|
console.log('process.env.RTC_MIN_PORT', process.env.RTC_MIN_PORT);
|
||||||
|
console.log('process.env.RTC_MAX_PORT', process.env.RTC_MAX_PORT, process.env.RTC_MAX_PORT.length);
|
||||||
const createWorker = async () => {
|
const createWorker = async () => {
|
||||||
try {
|
try {
|
||||||
worker = await mediasoup.createWorker({
|
worker = await mediasoup.createWorker({
|
||||||
rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
|
rtcMinPort: process.env.RTC_MIN_PORT,
|
||||||
rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
|
rtcMaxPort: process.env.RTC_MAX_PORT,
|
||||||
})
|
})
|
||||||
console.log(`[createWorker] worker pid ${worker.pid}`);
|
console.log(`[createWorker] worker pid ${worker.pid}`);
|
||||||
|
|
||||||
@ -124,6 +120,16 @@ 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
|
||||||
@ -150,9 +156,7 @@ 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]) {
|
||||||
@ -161,19 +165,12 @@ 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);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -194,19 +191,16 @@ 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 ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
|
console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
|
||||||
callback(error);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -250,9 +244,9 @@ peers.on('connection', async socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Send back to the client the Producer's id
|
// Send back to the client the Producer's id
|
||||||
callback && callback({
|
// callback({
|
||||||
id: videoCalls[callId].producer.id
|
// 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}`);
|
||||||
}
|
}
|
||||||
@ -323,7 +317,6 @@ 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 ${socketDetails[socket.id]} | ${error.message}`)
|
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
|
||||||
|
Reference in New Issue
Block a user