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

117
app.js
View File

@ -237,7 +237,11 @@ peers.on('connection', async (socket) => {
}
}
} 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);
}
});
@ -384,60 +388,16 @@ peers.on('connection', async (socket) => {
socket.on('consume', async ({ rtpCapabilities }, callback) => {
try {
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);
let canConsumeVideo, canConsumeAudio;
try {
if (isInitiator(callId, socket.id)) {
canConsumeVideo =
!!videoCalls[callId].receiverVideoProducer &&
!!videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].receiverVideoProducer.id,
rtpCapabilities,
callback({
videoParams: await consumeVideo({ callId, socketId, rtpCapabilities }),
audioParams: await consumeAudio({ callId, socketId, 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) {
console.error(`[consume] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`);
callback({ params: { error } });
@ -462,11 +422,15 @@ peers.on('connection', async (socket) => {
videoCalls[callId].receiverConsumerAudio && videoCalls[callId].receiverConsumerAudio.resume();
}
} 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 {
if (isInitiator(callId, socket.id)) {
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}`);
}
});
});
const consumeVideo = async (callId, socketId, rtpCapabilities) => {
// Handlers for transports https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
const canConsume = ({ callId, producerId, rtpCapabilities }) => {
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)) {
const producerId = videoCalls[callId].receiverVideoProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].initiatorConsumerVideo = await videoCalls[callId].initiatorConsumerTransport.consume({
producerId: videoCalls[callId].receiverVideoProducer.id,
producerId,
rtpCapabilities,
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 {
id: videoCalls[callId].initiatorConsumerVideo.id,
producerId: videoCalls[callId].receiverVideoProducer.id,
producerId,
kind: 'video',
rtpParameters: videoCalls[callId].initiatorConsumerVideo.rtpParameters,
};
} else {
const producerId = videoCalls[callId].initiatorVideoProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].receiverConsumerVideo = await videoCalls[callId].receiverConsumerTransport.consume({
producerId: videoCalls[callId].initiatorVideoProducer.id,
producerId,
rtpCapabilities,
paused: true,
});
return {
id: videoCalls[callId].receiverConsumerVideo.id,
producerId: videoCalls[callId].initiatorVideoProducer.id,
producerId,
kind: 'video',
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)) {
const producerId = videoCalls[callId].receiverAudioProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].initiatorConsumerAudio = await videoCalls[callId].initiatorConsumerTransport.consume({
producerId: videoCalls[callId].receiverAudioProducer.id,
producerId,
rtpCapabilities,
paused: true,
});
return {
id: videoCalls[callId].initiatorConsumerAudio.id,
producerId: videoCalls[callId].receiverAudioProducer.id,
producerId,
kind: 'audio',
rtpParameters: videoCalls[callId].initiatorConsumerAudio.rtpParameters,
};
} else {
const producerId = videoCalls[callId].initiatorAudioProducer.id;
if (!canConsume({ callId, producerId, rtpCapabilities })) return null;
videoCalls[callId].receiverConsumerAudio = await videoCalls[callId].receiverConsumerTransport.consume({
producerId: videoCalls[callId].initiatorAudioProducer.id,
producerId,
rtpCapabilities,
paused: true,
});
return {
id: videoCalls[callId].receiverConsumerAudio.id,
producerId: videoCalls[callId].initiatorAudioProducer.id,
producerId,
kind: 'audio',
rtpParameters: videoCalls[callId].receiverConsumerAudio.rtpParameters,
};
@ -599,7 +582,9 @@ const createWebRtcTransportLayer = async (callId, callback) => {
// Set transport to producerTransport or consumerTransport
return transport;
} 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 } });
}
};