LH-276: Refactor consume(consumeAudio/consumeVideo); Format code
This commit is contained in:
parent
bde02fe250
commit
4d8adf9eac
149
app.js
149
app.js
@ -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,
|
||||
});
|
||||
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);
|
||||
}
|
||||
callback({
|
||||
videoParams: await consumeVideo({ callId, socketId, rtpCapabilities }),
|
||||
audioParams: await consumeAudio({ callId, socketId, rtpCapabilities }),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`[consume] | ERROR | callId: ${socketDetails[socket.id]} | error: ${error.message}`);
|
||||
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
|
||||
- For the initiator we resume the initiatorConsumerAUDIO/VIDEO and for receiver the receiverConsumerAUDIO/VIDEO
|
||||
*/
|
||||
socket.on('consumer-resume', () => {
|
||||
try {
|
||||
const callId = socketDetails[socket.id];
|
||||
const isInitiatorValue = isInitiator(callId, socket.id);
|
||||
console.log(`[consumer-resume] callId: ${callId} | isInitiator: ${isInitiatorValue}`);
|
||||
if (isInitiatorValue) {
|
||||
videoCalls[callId].initiatorConsumerVideo && videoCalls[callId].initiatorConsumerVideo.resume();
|
||||
videoCalls[callId].initiatorConsumerAudio && videoCalls[callId].initiatorConsumerAudio.resume();
|
||||
} else {
|
||||
videoCalls[callId].receiverConsumerVideo && videoCalls[callId].receiverConsumerVideo.resume();
|
||||
videoCalls[callId].receiverConsumerAudio && videoCalls[callId].receiverConsumerAudio.resume();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[consumer-resume] | ERROR | callId: ${socketDetails[socket.id]} | isInitiator: ${isInitiator} | error: ${error.message}`);
|
||||
}
|
||||
});
|
||||
socket.on('consumer-resume', () => {
|
||||
try {
|
||||
const callId = socketDetails[socket.id];
|
||||
const isInitiatorValue = isInitiator(callId, socket.id);
|
||||
console.log(`[consumer-resume] callId: ${callId} | isInitiator: ${isInitiatorValue}`);
|
||||
if (isInitiatorValue) {
|
||||
videoCalls[callId].initiatorConsumerVideo && videoCalls[callId].initiatorConsumerVideo.resume();
|
||||
videoCalls[callId].initiatorConsumerAudio && videoCalls[callId].initiatorConsumerAudio.resume();
|
||||
} else {
|
||||
videoCalls[callId].receiverConsumerVideo && videoCalls[callId].receiverConsumerVideo.resume();
|
||||
videoCalls[callId].receiverConsumerAudio && videoCalls[callId].receiverConsumerAudio.resume();
|
||||
}
|
||||
} catch (error) {
|
||||
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,
|
||||
});
|
||||
|
||||
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 } });
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user