LH-276: Add close-producer event handler; Update client #30

Merged
sergiu merged 9 commits from LH-276-close-producer into develop 2023-02-23 09:46:42 +00:00
Showing only changes of commit 4d8adf9eac - Show all commits

149
app.js
View File

@ -237,7 +237,11 @@ peers.on('connection', async (socket) => {
} }
} }
} catch (error) { } catch (error) {
console.error(`[createWebRtcTransport] | ERROR | callId: ${socketDetails[socket.id]} | sender: ${sender} | error: ${error.message}`); console.error(
`[createWebRtcTransport] | ERROR | callId: ${socketDetails[socket.id]} | sender: ${sender} | error: ${
error.message
}`
);
callback(error); callback(error);
} }
}); });
@ -384,60 +388,16 @@ peers.on('connection', async (socket) => {
socket.on('consume', async ({ rtpCapabilities }, callback) => { socket.on('consume', async ({ rtpCapabilities }, callback) => {
try { try {
const callId = socketDetails[socket.id]; const callId = socketDetails[socket.id];
const socketId = socket.id;
console.log(`[consume] socket ${socket.id} | callId: ${callId}`);
if (typeof rtpCapabilities === 'string') rtpCapabilities = JSON.parse(rtpCapabilities); if (typeof rtpCapabilities === 'string') rtpCapabilities = JSON.parse(rtpCapabilities);
let canConsumeVideo, canConsumeAudio; callback({
try { videoParams: await consumeVideo({ callId, socketId, rtpCapabilities }),
if (isInitiator(callId, socket.id)) { audioParams: await consumeAudio({ callId, socketId, rtpCapabilities }),
canConsumeVideo = });
!!videoCalls[callId].receiverVideoProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].receiverVideoProducer.id,
rtpCapabilities,
});
canConsumeAudio =
!!videoCalls[callId].receiverAudioProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].receiverAudioProducer.id,
rtpCapabilities,
});
} else {
canConsumeVideo =
!!videoCalls[callId].initiatorVideoProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].initiatorVideoProducer.id,
rtpCapabilities,
});
canConsumeAudio =
!!videoCalls[callId].initiatorAudioProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].initiatorAudioProducer.id,
rtpCapabilities,
});
}
} catch (error) {
console.error(`[consume] | ERROR | callId: ${callId} | error: ${error.message}`);
}
console.log(`[consume] socket ${socket.id} | callId: ${callId} | canConsumeVideo: ${canConsumeVideo} | canConsumeAudio: ${canConsumeAudio}`);
if (canConsumeVideo && !canConsumeAudio) {
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);
callback({ videoParams, audioParams });
} else if (!canConsumeVideo && canConsumeAudio) {
const audioParams = await consumeAudio(callId, socket.id, rtpCapabilities);
const data = { videoParams: null, audioParams };
callback(data);
} else {
console.log(`[consume] Can't consume | callId ${callId}`);
callback(null);
}
} catch (error) { } catch (error) {
console.error(`[consume] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`); console.error(`[consume] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`);
callback({ params: { error } }); callback({ params: { error } });
@ -449,24 +409,28 @@ peers.on('connection', async (socket) => {
- When consuming on consumerTransport, it is initially done with paused: true, here we will resume - When consuming on consumerTransport, it is initially done with paused: true, here we will resume
- For the initiator we resume the initiatorConsumerAUDIO/VIDEO and for receiver the receiverConsumerAUDIO/VIDEO - For the initiator we resume the initiatorConsumerAUDIO/VIDEO and for receiver the receiverConsumerAUDIO/VIDEO
*/ */
socket.on('consumer-resume', () => { socket.on('consumer-resume', () => {
try { try {
const callId = socketDetails[socket.id]; const callId = socketDetails[socket.id];
const isInitiatorValue = isInitiator(callId, socket.id); const isInitiatorValue = isInitiator(callId, socket.id);
console.log(`[consumer-resume] callId: ${callId} | isInitiator: ${isInitiatorValue}`); console.log(`[consumer-resume] callId: ${callId} | isInitiator: ${isInitiatorValue}`);
if (isInitiatorValue) { if (isInitiatorValue) {
videoCalls[callId].initiatorConsumerVideo && videoCalls[callId].initiatorConsumerVideo.resume(); videoCalls[callId].initiatorConsumerVideo && videoCalls[callId].initiatorConsumerVideo.resume();
  • we should not use the same variables for both receiver and initiator, we can improve the naming with something like canConsumeAudioReceiver and canConsumeAudioInitiator
  • the assignments for canConsumeAudio and canConsumeVideo are very similar and very hard to distinguish if something is wrong, extract it in a function and use it in all 4 places
    • something like: initCanConsume(producer, callId, rtpCapabilities)
  • not sure if !! is necessary in all places
* we should not use the same variables for both receiver and initiator, we can improve the naming with something like `canConsumeAudioReceiver` and `canConsumeAudioInitiator` * the assignments for `canConsumeAudio` and `canConsumeVideo` are very similar and very hard to distinguish if something is wrong, extract it in a function and use it in all 4 places * something like: `initCanConsume(producer, callId, rtpCapabilities)` * not sure if `!!` is necessary in all places
videoCalls[callId].initiatorConsumerAudio && videoCalls[callId].initiatorConsumerAudio.resume(); videoCalls[callId].initiatorConsumerAudio && videoCalls[callId].initiatorConsumerAudio.resume();
} else { } else {
videoCalls[callId].receiverConsumerVideo && videoCalls[callId].receiverConsumerVideo.resume(); videoCalls[callId].receiverConsumerVideo && videoCalls[callId].receiverConsumerVideo.resume();
videoCalls[callId].receiverConsumerAudio && videoCalls[callId].receiverConsumerAudio.resume(); videoCalls[callId].receiverConsumerAudio && videoCalls[callId].receiverConsumerAudio.resume();
} }
} catch (error) { } catch (error) {
console.error(`[consumer-resume] | ERROR | callId: ${socketDetails[socket.id]} | isInitiator: ${isInitiator} | error: ${error.message}`); console.error(
} `[consumer-resume] | ERROR | callId: ${socketDetails[socket.id]} | isInitiator: ${isInitiator} | error: ${
}); error.message
}`
);
}
});
socket.on('close-producer', ({ callId, kind}) => { socket.on('close-producer', ({ callId, kind }) => {
try { try {
if (isInitiator(callId, socket.id)) { if (isInitiator(callId, socket.id)) {
console.log(`[close-producer] initiator --EMIT--> receiver | callId: ${callId} | kind: ${kind}`); console.log(`[close-producer] initiator --EMIT--> receiver | callId: ${callId} | kind: ${kind}`);
@ -479,64 +443,83 @@ peers.on('connection', async (socket) => {
console.error(`[close-producer] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`); console.error(`[close-producer] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`);
} }
}); });
}); });
const consumeVideo = async (callId, socketId, rtpCapabilities) => { const canConsume = ({ callId, producerId, rtpCapabilities }) => {
// Handlers for transports https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose return !!videoCalls[callId].router.canConsume({
producerId,
rtpCapabilities,
});
};
const consumeVideo = async ({ callId, socketId, rtpCapabilities }) => {
// Handlers for consumer transport https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
if (isInitiator(callId, socketId)) { if (isInitiator(callId, socketId)) {
const producerId = videoCalls[callId].receiverVideoProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].initiatorConsumerVideo = await videoCalls[callId].initiatorConsumerTransport.consume({ videoCalls[callId].initiatorConsumerVideo = await videoCalls[callId].initiatorConsumerTransport.consume({
producerId: videoCalls[callId].receiverVideoProducer.id, producerId,
rtpCapabilities, rtpCapabilities,
paused: true, paused: true,
sergiu marked this conversation as resolved Outdated
  • optional chaining
  • based on isInitiatorValue we can assign consumerVideo and consumerAudio and call resume on them
consumerVideo = isInitiatorValue?  videoCalls[callId].initiatorConsumerVideo : videoCalls[callId].receiverConsumerVideo
consumerAudio = ...
consumerVideo?.resume();
* optional chaining * based on `isInitiatorValue` we can assign `consumerVideo` and `consumerAudio` and call resume on them ```javascript consumerVideo = isInitiatorValue? videoCalls[callId].initiatorConsumerVideo : videoCalls[callId].receiverConsumerVideo consumerAudio = ... consumerVideo?.resume(); ```
}); });
return { return {
id: videoCalls[callId].initiatorConsumerVideo.id, id: videoCalls[callId].initiatorConsumerVideo.id,
producerId: videoCalls[callId].receiverVideoProducer.id, producerId,
kind: 'video', kind: 'video',
rtpParameters: videoCalls[callId].initiatorConsumerVideo.rtpParameters, rtpParameters: videoCalls[callId].initiatorConsumerVideo.rtpParameters,
}; };
} else { } else {
const producerId = videoCalls[callId].initiatorVideoProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].receiverConsumerVideo = await videoCalls[callId].receiverConsumerTransport.consume({ videoCalls[callId].receiverConsumerVideo = await videoCalls[callId].receiverConsumerTransport.consume({
producerId: videoCalls[callId].initiatorVideoProducer.id, producerId,
rtpCapabilities, rtpCapabilities,
paused: true, paused: true,
}); });
return { return {
id: videoCalls[callId].receiverConsumerVideo.id, id: videoCalls[callId].receiverConsumerVideo.id,
producerId: videoCalls[callId].initiatorVideoProducer.id, producerId,
kind: 'video', kind: 'video',
rtpParameters: videoCalls[callId].receiverConsumerVideo.rtpParameters, rtpParameters: videoCalls[callId].receiverConsumerVideo.rtpParameters,
}; };
} }
}; };
const consumeAudio = async (callId, socketId, rtpCapabilities) => { const consumeAudio = async ({ callId, socketId, rtpCapabilities }) => {
// Handlers for consumer transport https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
if (isInitiator(callId, socketId)) { if (isInitiator(callId, socketId)) {
const producerId = videoCalls[callId].receiverAudioProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].initiatorConsumerAudio = await videoCalls[callId].initiatorConsumerTransport.consume({ videoCalls[callId].initiatorConsumerAudio = await videoCalls[callId].initiatorConsumerTransport.consume({
producerId: videoCalls[callId].receiverAudioProducer.id, producerId,
rtpCapabilities, rtpCapabilities,
paused: true, paused: true,
}); });
return { return {
id: videoCalls[callId].initiatorConsumerAudio.id, id: videoCalls[callId].initiatorConsumerAudio.id,
producerId: videoCalls[callId].receiverAudioProducer.id, producerId,
kind: 'audio', kind: 'audio',
rtpParameters: videoCalls[callId].initiatorConsumerAudio.rtpParameters, rtpParameters: videoCalls[callId].initiatorConsumerAudio.rtpParameters,
}; };
} else { } else {
const producerId = videoCalls[callId].initiatorAudioProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].receiverConsumerAudio = await videoCalls[callId].receiverConsumerTransport.consume({ videoCalls[callId].receiverConsumerAudio = await videoCalls[callId].receiverConsumerTransport.consume({
producerId: videoCalls[callId].initiatorAudioProducer.id, producerId,
rtpCapabilities, rtpCapabilities,
paused: true, paused: true,
}); });
return { return {
id: videoCalls[callId].receiverConsumerAudio.id, id: videoCalls[callId].receiverConsumerAudio.id,
producerId: videoCalls[callId].initiatorAudioProducer.id, producerId,
kind: 'audio', kind: 'audio',
rtpParameters: videoCalls[callId].receiverConsumerAudio.rtpParameters, rtpParameters: videoCalls[callId].receiverConsumerAudio.rtpParameters,
}; };
@ -599,7 +582,9 @@ const createWebRtcTransportLayer = async (callId, callback) => {
// Set transport to producerTransport or consumerTransport // Set transport to producerTransport or consumerTransport
return transport; return transport;
} catch (error) { } catch (error) {
console.error(`[createWebRtcTransportLayer] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`); console.error(
`[createWebRtcTransportLayer] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`
);
callback({ params: { error } }); callback({ params: { error } });
} }
}; };