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

This commit is contained in:
Sergiu Toma 2023-01-13 20:11:24 +02:00
parent ad4c1f27e7
commit 69c167e4e9
2 changed files with 20 additions and 9 deletions

23
app.js
View File

@ -28,8 +28,8 @@ let worker;
* initiatorConsumerVideo: Consumer, * initiatorConsumerVideo: Consumer,
* initiatorConsumerAudio: Consumer, * initiatorConsumerAudio: Consumer,
* initiatorConsumerTransport: Consumer Transport * initiatorConsumerTransport: Consumer Transport
* initiatorSockerId * initiatorSocket
* receiverSocketId * receiverSocket
* } * }
* *
**/ **/
@ -192,12 +192,11 @@ peers.on('connection', async socket => {
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]) {
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}`); console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
videoCalls[callId].receiverSocketId = socket.id videoCalls[callId].receiverSocket = socket;
} else { } else {
videoCalls[callId].initiatorSockerId = socket.id videoCalls[callId].initiatorSocket = socket;
} }
socketDetails[socket.id] = callId; socketDetails[socket.id] = callId;
// rtpCapabilities is set for callback // rtpCapabilities is set for callback
@ -259,11 +258,19 @@ peers.on('connection', async socket => {
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters); if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
console.log(`[transport-connect] socket ${socket.id} | callId ${callId}`); console.log(`[transport-connect] socket ${socket.id} | callId ${callId}`);
if (!isInitiator(callId, socket.id)) { if (!isInitiator(callId, socket.id)) {
await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters }); await videoCalls[callId].receiverProducerTransport.connect({ dtlsParameters });
videoCalls[callId].initiatorSocket.emit('new-producer', {
callId
});
} else { } else {
await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters }); await videoCalls[callId].initiatorProducerTransport.connect({ dtlsParameters });
videoCalls[callId].receiverSocket.emit('new-producer', {
callId
});
} }
} 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.message}`);
} }
@ -280,6 +287,7 @@ peers.on('connection', async socket => {
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters); if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
console.log(`[transport-produce] kind: ${kind} | socket: ${socket.id} | callId: ${callId}`); console.log(`[transport-produce] kind: ${kind} | socket: ${socket.id} | callId: ${callId}`);
if (kind === 'video') { if (kind === 'video') {
if (!isInitiator(callId, socket.id)) { if (!isInitiator(callId, socket.id)) {
videoCalls[callId].receiverVideoProducer = await videoCalls[callId].receiverProducerTransport.produce({ videoCalls[callId].receiverVideoProducer = await videoCalls[callId].receiverProducerTransport.produce({
@ -578,7 +586,7 @@ const consumeAudio = async (callId, socketId, rtpCapabilities) => {
} }
const isInitiator = (callId, socketId) => { const isInitiator = (callId, socketId) => {
return (videoCalls[callId].initiatorSockerId === socketId); return (videoCalls[callId]?.initiatorSocket?.id === socketId);
} }
/* /*
@ -606,7 +614,6 @@ const createWebRtcTransportLayer = async (callId, callback) => {
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport // https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options) let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options)
console.log(`callId: ${callId} | transport id: ${transport.id}`)
// Handler for when DTLS(Datagram Transport Layer Security) changes // Handler for when DTLS(Datagram Transport Layer Security) changes
transport.on('dtlsstatechange', dtlsState => { transport.on('dtlsstatechange', dtlsState => {

View File

@ -91,10 +91,14 @@ setTimeout(() => {
console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`) console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`)
if (!IS_PRODUCER && existsProducer && consumer === undefined) { if (!IS_PRODUCER && existsProducer && consumer === undefined) {
goConnect() goConnect()
// document.getElementById('btnRecvSendTransport').click();
} }
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() } 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) { if (IS_PRODUCER === true) {