Compare commits
6 Commits
fix-branch
...
dtls-test
Author | SHA1 | Date | |
---|---|---|---|
0c713ed286 | |||
817781085f | |||
a95c29659e | |||
5d451d961f | |||
1d47d02792 | |||
8ac58f0d9d |
4
.env
4
.env
@ -1,7 +1,3 @@
|
|||||||
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"
|
|
46
app.js
46
app.js
@ -5,7 +5,12 @@ 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');
|
||||||
const https = require('https');
|
let 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
|
||||||
@ -54,17 +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('/');
|
|
||||||
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: process.env.RTC_MIN_PORT,
|
rtcMinPort: 2000,
|
||||||
rtcMaxPort: process.env.RTC_MAX_PORT,
|
rtcMaxPort: 2020,
|
||||||
})
|
})
|
||||||
console.log(`[createWorker] worker pid ${worker.pid}`);
|
console.log(`[createWorker] worker pid ${worker.pid}`);
|
||||||
|
|
||||||
@ -80,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
|
||||||
@ -101,11 +105,11 @@ const mediaCodecs = [
|
|||||||
'x-google-start-bitrate': 1000,
|
'x-google-start-bitrate': 1000,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
];
|
]
|
||||||
|
|
||||||
const closeCall = (callId) => {
|
const closeCall = (callId) => {
|
||||||
try {
|
try {
|
||||||
if (callId && videoCalls[callId]) {
|
if (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();
|
||||||
@ -211,12 +215,14 @@ 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('🔴 typeof dtlsParameters', typeof dtlsParameters);
|
||||||
|
// console.log('🟢 dtlsParameters', JSON.parse(dtlsParameters));
|
||||||
|
// console.log('🟡 dtlsParameters', dtlsParameters);
|
||||||
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
|
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
|
||||||
|
|
||||||
console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId}`);
|
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.stack}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -225,11 +231,9 @@ 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 }, callback) => {
|
socket.on('transport-produce', async ({ kind, rtpParameters, appData }) => {
|
||||||
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,11 +246,6 @@ 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({
|
|
||||||
// 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}`);
|
||||||
}
|
}
|
||||||
@ -295,14 +294,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(callId);
|
closeCall();
|
||||||
});
|
});
|
||||||
|
|
||||||
// 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(callId);
|
closeCall();
|
||||||
});
|
});
|
||||||
|
|
||||||
// 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
|
||||||
@ -386,6 +385,7 @@ const createWebRtcTransportLayer = async (callId, callback) => {
|
|||||||
dtlsParameters: transport.dtlsParameters,
|
dtlsParameters: transport.dtlsParameters,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(`createWebRtcTransportLayer | params.dtlsParameters ${params.dtlsParameters}`);
|
||||||
// Send back to the client the params
|
// Send back to the client the params
|
||||||
callback({ params });
|
callback({ params });
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user