Add new-producer event; Update client to consume when receives new-producer event #24

Merged
sergiu merged 4 commits from LAPI-675-generate-new-producer-in-mediasoup-and-client into develop 2023-01-16 15:40:06 +00:00
3 changed files with 172 additions and 143 deletions

199
app.js
View File

@ -1,4 +1,4 @@
require('dotenv').config()
require('dotenv').config();
const express = require('express');
const app = express();
@ -28,8 +28,8 @@ let worker;
* initiatorConsumerVideo: Consumer,
* initiatorConsumerAudio: Consumer,
* initiatorConsumerTransport: Consumer Transport
* initiatorSockerId
* receiverSocketId
* initiatorSocket
* receiverSocket
* }
*
**/
@ -37,7 +37,7 @@ let videoCalls = {};
let socketDetails = {};
app.get('/', (_req, res) => {
res.send('Hello from mediasoup app!')
res.send('Hello from mediasoup app!');
});
app.use('/sfu', express.static(path.join(__dirname, 'public')));
@ -52,7 +52,7 @@ const httpsServer = https.createServer(options, app);
const io = new Server(httpsServer, {
allowEIO3: true,
origins: ["*:*"]
origins: ['*:*'],
});
httpsServer.listen(process.env.PORT, () => {
@ -66,19 +66,19 @@ const createWorker = async () => {
worker = await mediasoup.createWorker({
rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
})
});
console.log(`[createWorker] worker pid ${worker.pid}`);
worker.on('died', error => {
worker.on('died', (error) => {
// This implies something serious happened, so kill the application
console.error('mediasoup worker has died', error);
setTimeout(() => process.exit(1), 2000); // exit in 2 seconds
})
});
return worker;
} catch (error) {
console.log(`ERROR | createWorker | ${error.message}`);
}
}
};
// We create a Worker as soon as our application starts
worker = createWorker();
@ -92,52 +92,48 @@ const mediaCodecs = [
kind: 'audio',
mimeType: 'audio/opus',
clockRate: 48000,
channels : 2
channels: 2,
},
{
kind: 'video',
mimeType: 'video/VP8',
clockRate: 90000,
parameters :
{
'x-google-start-bitrate' : 1000
parameters: {
'x-google-start-bitrate': 1000,
},
channels : 2
channels: 2,
},
{
kind: 'video',
mimeType: 'video/VP9',
clockRate: 90000,
parameters :
{
parameters: {
'profile-id': 2,
'x-google-start-bitrate' : 1000
}
'x-google-start-bitrate': 1000,
},
},
{
kind: 'video',
mimeType: 'video/h264',
clockRate: 90000,
parameters :
{
parameters: {
'packetization-mode': 1,
'profile-level-id': '4d0032',
'level-asymmetry-allowed': 1,
'x-google-start-bitrate' : 1000
}
'x-google-start-bitrate': 1000,
},
},
{
kind: 'video',
mimeType: 'video/h264',
clockRate: 90000,
parameters :
{
parameters: {
'packetization-mode': 1,
'profile-level-id': '42e01f',
'level-asymmetry-allowed': 1,
'x-google-start-bitrate' : 1000
}
}
'x-google-start-bitrate': 1000,
},
},
];
const closeCall = (callId) => {
@ -158,18 +154,18 @@ const closeCall = (callId) => {
} catch (error) {
console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`);
}
}
};
/*
- Handlers for WS events
- These are created only when we have a connection with a peer
*/
peers.on('connection', async socket => {
peers.on('connection', async (socket) => {
console.log('[connection] socketId:', socket.id);
// After making the connection successfully, we send the client a 'connection-success' event
socket.emit('connection-success', {
socketId: socket.id
socketId: socket.id,
});
// It is triggered when the peer is disconnected
@ -192,18 +188,17 @@ peers.on('connection', async socket => {
if (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 }) }
videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) };
console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
videoCalls[callId].receiverSocketId = socket.id
videoCalls[callId].receiverSocket = socket;
} else {
videoCalls[callId].initiatorSockerId = socket.id
videoCalls[callId].initiatorSocket = socket;
}
socketDetails[socket.id] = callId;
// rtpCapabilities is set for callback
console.log('[getRtpCapabilities] callId', callId);
callbackResponse = {
rtpCapabilities :videoCalls[callId].router.rtpCapabilities
rtpCapabilities: videoCalls[callId].router.rtpCapabilities,
};
} else {
console.log(`[createRoom] missing callId ${callId}`);
@ -244,7 +239,9 @@ peers.on('connection', async socket => {
}
}
} 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);
}
});
@ -259,11 +256,16 @@ peers.on('connection', async socket => {
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
console.log(`[transport-connect] socket ${socket.id} | callId ${callId}`);
if (!isInitiator(callId, socket.id)) {
await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters });
} else {
await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters });
}
isInitiator(callId, socket.id)
? await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters })
: await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters });
const socketToEmit = isInitiator(callId, socket.id)
? videoCalls[callId].receiverSocket
: videoCalls[callId].initiatorSocket;
socketToEmit.emit('new-producer', { callId });
} catch (error) {
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
}
sergiu marked this conversation as resolved Outdated

We can refactor so that we only have one emit and connect instructions. Will be better when the new-producer messages changes.

We can refactor so that we only have one `emit` and `connect` instructions. Will be better when the `new-producer` messages changes.
@ -280,6 +282,7 @@ peers.on('connection', async socket => {
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
console.log(`[transport-produce] kind: ${kind} | socket: ${socket.id} | callId: ${callId}`);
if (kind === 'video') {
if (!isInitiator(callId, socket.id)) {
videoCalls[callId].receiverVideoProducer = await videoCalls[callId].receiverProducerTransport.produce({
@ -287,17 +290,20 @@ peers.on('connection', async socket => {
rtpParameters,
});
console.log(`[transport-produce] receiverVideoProducer Producer ID: ${videoCalls[callId].receiverVideoProducer.id} | kind: ${videoCalls[callId].receiverVideoProducer.kind}`);
console.log(
`[transport-produce] receiverVideoProducer Producer ID: ${videoCalls[callId].receiverVideoProducer.id} | kind: ${videoCalls[callId].receiverVideoProducer.kind}`
);
videoCalls[callId].receiverVideoProducer.on('transportclose', () => {
const callId = socketDetails[socket.id];
console.log('transport for this producer closed', callId)
console.log('transport for this producer closed', callId);
closeCall(callId);
});
// Send back to the client the Producer's id
callback && callback({
id: videoCalls[callId].receiverVideoProducer.id
callback &&
callback({
id: videoCalls[callId].receiverVideoProducer.id,
});
} else {
videoCalls[callId].initiatorVideoProducer = await videoCalls[callId].initiatorProducerTransport.produce({
@ -305,16 +311,19 @@ peers.on('connection', async socket => {
rtpParameters,
});
console.log(`[transport-produce] initiatorVideoProducer Producer ID: ${videoCalls[callId].initiatorVideoProducer.id} | kind: ${videoCalls[callId].initiatorVideoProducer.kind}`);
console.log(
`[transport-produce] initiatorVideoProducer Producer ID: ${videoCalls[callId].initiatorVideoProducer.id} | kind: ${videoCalls[callId].initiatorVideoProducer.kind}`
);
videoCalls[callId].initiatorVideoProducer.on('transportclose', () => {
const callId = socketDetails[socket.id];
console.log('transport for this producer closed', callId)
console.log('transport for this producer closed', callId);
closeCall(callId);
});
callback && callback({
id: videoCalls[callId].initiatorVideoProducer.id
callback &&
callback({
id: videoCalls[callId].initiatorVideoProducer.id,
});
}
} else if (kind === 'audio') {
@ -324,17 +333,20 @@ peers.on('connection', async socket => {
rtpParameters,
});
console.log(`[transport-produce] receiverAudioProducer Producer ID: ${videoCalls[callId].receiverAudioProducer.id} | kind: ${videoCalls[callId].receiverAudioProducer.kind}`);
console.log(
`[transport-produce] receiverAudioProducer Producer ID: ${videoCalls[callId].receiverAudioProducer.id} | kind: ${videoCalls[callId].receiverAudioProducer.kind}`
);
videoCalls[callId].receiverAudioProducer.on('transportclose', () => {
const callId = socketDetails[socket.id];
console.log('transport for this producer closed', callId)
console.log('transport for this producer closed', callId);
closeCall(callId);
});
// Send back to the client the Producer's id
callback && callback({
id: videoCalls[callId].receiverAudioProducer.id
callback &&
callback({
id: videoCalls[callId].receiverAudioProducer.id,
});
} else {
videoCalls[callId].initiatorAudioProducer = await videoCalls[callId].initiatorProducerTransport.produce({
@ -342,17 +354,20 @@ peers.on('connection', async socket => {
rtpParameters,
});
console.log(`[transport-produce] initiatorAudioProducer Producer ID: ${videoCalls[callId].initiatorAudioProducer.id} | kind: ${videoCalls[callId].initiatorAudioProducer.kind}`);
console.log(
`[transport-produce] initiatorAudioProducer Producer ID: ${videoCalls[callId].initiatorAudioProducer.id} | kind: ${videoCalls[callId].initiatorAudioProducer.kind}`
);
videoCalls[callId].initiatorAudioProducer.on('transportclose', () => {
const callId = socketDetails[socket.id];
console.log('transport for this producer closed', callId)
console.log('transport for this producer closed', callId);
closeCall(callId);
});
// Send back to the client the Producer's id
callback && callback({
id: videoCalls[callId].initiatorAudioProducer.id
callback &&
callback({
id: videoCalls[callId].initiatorAudioProducer.id,
});
}
}
@ -379,7 +394,7 @@ peers.on('connection', async socket => {
} catch (error) {
console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
}
})
});
/*
- The customer consumes after successfully connecting to consumerTransport
@ -391,45 +406,54 @@ peers.on('connection', async socket => {
socket.on('consume', async ({ rtpCapabilities }, callback) => {
try {
const callId = socketDetails[socket.id];
console.log(`[consume] socket ${socket.id} | callId ${callId} | rtpCapabilities: ${JSON.stringify(rtpCapabilities)}`);
console.log(
`[consume] socket ${socket.id} | callId ${callId} | rtpCapabilities: ${JSON.stringify(rtpCapabilities)}`
);
if (typeof rtpCapabilities === 'string') rtpCapabilities = JSON.parse(rtpCapabilities);
console.log('[consume] callId', callId);
let canConsumeVideo, canConsumeAudio;
if (isInitiator(callId, socket.id)) {
canConsumeVideo = !!videoCalls[callId].receiverVideoProducer && !!videoCalls[callId].router.canConsume({
canConsumeVideo =
!!videoCalls[callId].receiverVideoProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].receiverVideoProducer.id,
rtpCapabilities
rtpCapabilities,
});
canConsumeAudio = !!videoCalls[callId].receiverAudioProducer && !!videoCalls[callId].router.canConsume({
canConsumeAudio =
!!videoCalls[callId].receiverAudioProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].receiverAudioProducer.id,
rtpCapabilities
rtpCapabilities,
});
} else {
canConsumeVideo = !!videoCalls[callId].initiatorVideoProducer && !!videoCalls[callId].router.canConsume({
canConsumeVideo =
!!videoCalls[callId].initiatorVideoProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].initiatorVideoProducer.id,
rtpCapabilities
rtpCapabilities,
});
canConsumeAudio = !!videoCalls[callId].initiatorAudioProducer && !!videoCalls[callId].router.canConsume({
canConsumeAudio =
!!videoCalls[callId].initiatorAudioProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].initiatorAudioProducer.id,
rtpCapabilities
rtpCapabilities,
});
}
console.log('[consume] canConsumeVideo', canConsumeVideo);
console.log('[consume] canConsumeAudio', canConsumeAudio);
if (canConsumeVideo && !canConsumeAudio) {
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities)
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities);
callback({ videoParams, audioParams: null });
} else if (canConsumeVideo && canConsumeAudio) {
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities)
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities)
const videoParams = await consumeVideo(callId, socket.id, rtpCapabilities);
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities);
callback({ videoParams, audioParams });
} else if (!canConsumeVideo && canConsumeAudio) {
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities)
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities);
const data = { videoParams: null, audioParams };
callback(data);
} else {
@ -437,7 +461,7 @@ peers.on('connection', async socket => {
callback(null);
}
} catch (error) {
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`);
callback({ params: { error } });
}
});
@ -450,7 +474,7 @@ peers.on('connection', async socket => {
socket.on('consumer-resume', () => {
try {
const callId = socketDetails[socket.id];
console.log(`[consumer-resume] callId ${callId}`)
console.log(`[consumer-resume] callId ${callId}`);
if (isInitiator(callId, socket.id)) {
videoCalls[callId]?.initiatorConsumerVideo?.resume();
videoCalls[callId]?.initiatorConsumerAudio?.resume();
@ -465,7 +489,6 @@ peers.on('connection', async socket => {
});
const consumeVideo = async (callId, socketId, rtpCapabilities) => {
if (isInitiator(callId, socketId)) {
videoCalls[callId].initiatorConsumerVideo = await videoCalls[callId].initiatorConsumerTransport.consume({
producerId: videoCalls[callId].receiverVideoProducer.id,
@ -492,7 +515,7 @@ const consumeVideo = async (callId, socketId, rtpCapabilities) => {
producerId: videoCalls[callId].receiverVideoProducer.id,
kind: 'video',
rtpParameters: videoCalls[callId].initiatorConsumerVideo.rtpParameters,
}
};
} else {
videoCalls[callId].receiverConsumerVideo = await videoCalls[callId].receiverConsumerTransport.consume({
producerId: videoCalls[callId].initiatorVideoProducer.id,
@ -517,9 +540,9 @@ const consumeVideo = async (callId, socketId, rtpCapabilities) => {
producerId: videoCalls[callId].initiatorVideoProducer.id,
kind: 'video',
rtpParameters: videoCalls[callId].receiverConsumerVideo.rtpParameters,
};
}
}
}
};
const consumeAudio = async (callId, socketId, rtpCapabilities) => {
if (isInitiator(callId, socketId)) {
@ -548,7 +571,7 @@ const consumeAudio = async (callId, socketId, rtpCapabilities) => {
producerId: videoCalls[callId].receiverAudioProducer.id,
kind: 'audio',
rtpParameters: videoCalls[callId].initiatorConsumerAudio.rtpParameters,
}
};
} else {
videoCalls[callId].receiverConsumerAudio = await videoCalls[callId].receiverConsumerTransport.consume({
producerId: videoCalls[callId].initiatorAudioProducer.id,
@ -573,13 +596,13 @@ const consumeAudio = async (callId, socketId, rtpCapabilities) => {
producerId: videoCalls[callId].initiatorAudioProducer.id,
kind: 'audio',
rtpParameters: videoCalls[callId].receiverConsumerAudio.rtpParameters,
};
}
}
}
};
const isInitiator = (callId, socketId) => {
return (videoCalls[callId].initiatorSockerId === socketId);
}
return videoCalls[callId]?.initiatorSocket?.id === socketId;
};
/*
- Called from at event 'createWebRtcTransport' and assigned to the consumer or producer transport
@ -597,7 +620,7 @@ const createWebRtcTransportLayer = async (callId, callback) => {
{
ip: process.env.IP, // Listening IPv4 or IPv6.
announcedIp: process.env.ANNOUNCED_IP, // Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).
}
},
],
enableUdp: true,
enableTcp: true,
@ -605,11 +628,10 @@ const createWebRtcTransportLayer = async (callId, callback) => {
};
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options)
console.log(`callId: ${callId} | transport id: ${transport.id}`)
let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options);
// Handler for when DTLS(Datagram Transport Layer Security) changes
transport.on('dtlsstatechange', dtlsState => {
transport.on('dtlsstatechange', (dtlsState) => {
console.log(`transport | dtlsstatechange | calldId ${callId} | dtlsState ${dtlsState}`);
if (dtlsState === 'closed') {
transport.close();
@ -634,9 +656,8 @@ const createWebRtcTransportLayer = async (callId, callback) => {
// Set transport to producerTransport or consumerTransport
return transport;
} catch (error) {
console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
callback({ params: { error } });
}
}
};

View File

@ -20449,10 +20449,14 @@ setTimeout(() => {
console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`)
if (!IS_PRODUCER && existsProducer && consumer === undefined) {
goConnect()
// document.getElementById('btnRecvSendTransport').click();
}
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() }
})
socket.on('new-producer', ({ callId }) => {
console.log(`🟢 new-producer | callId: ${callId} | Ready to consume`);
consume()
})
}
if (IS_PRODUCER === true) {

View File

@ -91,10 +91,14 @@ setTimeout(() => {
console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`)
if (!IS_PRODUCER && existsProducer && consumer === undefined) {
goConnect()
// document.getElementById('btnRecvSendTransport').click();
}
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() }
})
socket.on('new-producer', ({ callId }) => {
console.log(`🟢 new-producer | callId: ${callId} | Ready to consume`);
consume()
})
}
if (IS_PRODUCER === true) {