i am using mediasoup v2
It was working fine with 40-50 users but today users increased upto and 80 and all streams were crashed after 4-5 mins…
This is server side code.
const fs = require('fs');
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/domainhere/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/Domain/cert.pem')
};
const app = require('https').createServer(options);
const io = require('socket.io')(app);
const config = require('./config');
const mediasoup = require('mediasoup');
const port = config.server.port;
// Map of Room instances indexed by roomId.
const rooms = new Map();
app.listen(port, () => console.log(`MediaSoup server is listening on port ${port}!`));
// MediaSoup server
const mediaServer = mediasoup.Server({
numWorkers: null, // Use as many CPUs as available.
logLevel: config.mediasoup.logLevel,
logTags: config.mediasoup.logTags,
rtcIPv4: config.mediasoup.rtcIPv4,
rtcIPv6: config.mediasoup.rtcIPv6,
rtcAnnouncedIPv4: config.mediasoup.rtcAnnouncedIPv4,
rtcAnnouncedIPv6: config.mediasoup.rtcAnnouncedIPv6,
rtcMinPort: config.mediasoup.rtcMinPort,
rtcMaxPort: config.mediasoup.rtcMaxPort
});
// Handle socket connection and its messages
io.on('connection', (socket) => {
// console.log('New socket connection:', socket.handshake.query);
// Used for mediaSoup room
let room = null;
// Used for mediaSoup peer
let mediaPeer = null;
const { roomId, peerName } = socket.handshake.query;
if (rooms.has(roomId)) {
room = rooms.get(roomId);
} else {
room = mediaServer.Room(config.mediasoup.mediaCodecs);
rooms.set(roomId, room);
room.on('close', () => {
rooms.delete(roomId);
});
/*
room.on('audiolevels', (audioLevelInfos) => {
console.log(JSON.stringify(audioLevelInfos));
});
*/
let activeSpeakerDetector=room.createActiveSpeakerDetector();
activeSpeakerDetector.on('activespeakerchange', (peer, producer) => { })
}
socket.on('mediasoup-request', (request, cb) => {
switch (request.method) {
case 'queryRoom':
room.receiveRequest(request)
.then((response) => cb(null, response))
.catch((error) => cb(error.toString()));
break;
case 'join':
room.receiveRequest(request)
.then((response) => {
// Get the newly created mediasoup Peer
mediaPeer = room.getPeerByName(peerName);
handleMediaPeer(mediaPeer);
// Send response back
cb(null, response);
})
.catch((error) => cb(error.toString()));
break;
default:
if (mediaPeer) {
mediaPeer.receiveRequest(request)
.then((response) => cb(null, response))
.catch((error) => cb(error.toString()));
}
}
});
socket.on('mediasoup-notification', (notification) => {
// console.debug('Got notification from client peer', notification);
// NOTE: mediasoup-client just sends notifications with target 'peer'
if (!mediaPeer) {
// console.error('Cannot handle mediaSoup notification, no mediaSoup Peer');
return;
}
mediaPeer.receiveNotification(notification);
});
socket.on('handle-consumer', (peerName,cMode,consumeId) => {
// console.debug('Got notification from client peer', notification);
// NOTE: mediasoup-client just sends notifications with target 'peer'
if (!mediaPeer) {
// console.error('Cannot handle mediaSoup notification, no mediaSoup Peer');
return;
}
let peerd=room.getPeerByName(peerName);
try{
peerd.consumers.forEach((consumer) => {
if(cMode=="setProfile-auto"){
if(consumeId == consumer.id){
consumer.setPreferredProfile('auto');
}
}
if(cMode=="setProfile-low"){
if(consumeId == consumer.id){
consumer.setPreferredProfile('low');
}
}
if(cMode=="setProfile-medium"){
if(consumeId == consumer.id){
consumer.setPreferredProfile('medium');
}
}
if(cMode=="setProfile-high"){
if(consumeId == consumer.id){
consumer.setPreferredProfile('high');
}
}
if(cMode=="pauseForMe"){
if(consumeId == consumer.id){
consumer.pause();
}
}
if(cMode=="resumeForMe"){
if(consumeId == consumer.id){
consumer.resume();
}
}
if(consumer.kind=="video" && cMode=="pause"){
if(consumeId != consumer.id){
consumer.pause();
// console.log(peerName+" stopped for "+consumer.id+ " "+consumer.kind);
}else{
consumer.resume();
// console.log(peerName+" resumed for "+consumer.id);
}
}
if(consumer.kind=="video" && cMode=="resume"){
if(consumeId =="all"){
consumer.resume();
}
}
});
}catch(ex){ console.log(ex); }
});
// Invokes when connection lost on a client side
socket.on('disconnect', () => {
if (mediaPeer) {
mediaPeer.close();
}
});
/**
* Handles all mediaPeer events
*
* @param mediaPeer
*/
const handleMediaPeer = (mediaPeer) => {
mediaPeer.on('notify', (notification) => {
// console.log('New notification for mediaPeer received:', notification);
socket.emit('mediasoup-notification', notification);
});
mediaPeer.on('newtransport', (transport) => {
// console.log('New mediaPeer transport:', transport.direction);
if (transport.direction === 'send'){
// console.log(transport.appData);
if(transport.appData == "sendScr"){
transport.setMaxBitrate(9900000);
}else{
transport.setMaxBitrate(150000);
}
}
transport.on('close', (originator) => {
// console.log('Transport closed from originator:', originator);
});
});
mediaPeer.on('newproducer', (producer) => {
console.log('New mediaPeer producer:', producer.kind);
producer.on('close', (originator) => {
// console.log('Producer closed from originator:', originator);
});
});
mediaPeer.on('newconsumer', (consumer) => {
// console.log('New mediaPeer consumer:', consumer.kind);
consumer.on('close', (originator) => {
// console.log('Consumer closed from originator', originator);
});
});
// Also handle already existing Consumers.
mediaPeer.consumers.forEach((consumer) => {
// console.log('mediaPeer existing consumer:', consumer.kind);
consumer.on('close', (originator) => {
// console.log('Existing consumer closed from originator', originator);
});
});
}
});