Update server
This commit is contained in:
parent
cd84c534ce
commit
8444809910
140
app.js
140
app.js
@ -160,8 +160,11 @@ const mediaCodecs = [
|
|||||||
const closeCall = (callId) => {
|
const closeCall = (callId) => {
|
||||||
try {
|
try {
|
||||||
if (callId && videoCalls[callId]) {
|
if (callId && videoCalls[callId]) {
|
||||||
videoCalls[callId].producer?.close();
|
videoCalls[callId].producerVideo?.close();
|
||||||
videoCalls[callId].consumer?.close();
|
videoCalls[callId].producerAudio?.close();
|
||||||
|
videoCalls[callId].consumerVideo?.close();
|
||||||
|
videoCalls[callId].consumerAudio?.close();
|
||||||
|
|
||||||
videoCalls[callId]?.consumerTransport?.close();
|
videoCalls[callId]?.consumerTransport?.close();
|
||||||
videoCalls[callId]?.producerTransport?.close();
|
videoCalls[callId]?.producerTransport?.close();
|
||||||
videoCalls[callId]?.router?.close();
|
videoCalls[callId]?.router?.close();
|
||||||
@ -280,31 +283,54 @@ peers.on('connection', async socket => {
|
|||||||
- The event sent by the client (PRODUCER) after successfully connecting to producerTransport
|
- The event sent by the client (PRODUCER) after successfully connecting to producerTransport
|
||||||
- For the router with the id callId, we make produce on producerTransport
|
- For the router with the id callId, we make produce on producerTransport
|
||||||
- Create the handler on producer at the 'transportclose' event
|
- Create the handler on producer at the 'transportclose' event
|
||||||
*/
|
*/
|
||||||
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
|
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
|
||||||
try {
|
try {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
|
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
|
||||||
|
|
||||||
|
console.log(`[transport-produce] kind: ${kind} | socket.id: ${socket.id} | callId: ${callId}`);
|
||||||
|
console.log('kind', kind);
|
||||||
|
console.log('rtpParameters', rtpParameters);
|
||||||
|
|
||||||
console.log(`[transport-produce] kind: ${kind} | socket.id: ${socket.id} | callId: ${callId}`);
|
if (kind === 'video') {
|
||||||
console.log('kind', kind);
|
videoCalls[callId].producerVideo = await videoCalls[callId].producerTransport.produce({
|
||||||
console.log('rtpParameters', rtpParameters);
|
kind,
|
||||||
videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({
|
rtpParameters,
|
||||||
kind,
|
});
|
||||||
rtpParameters,
|
|
||||||
});
|
|
||||||
console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producer.id} | kind: ${videoCalls[callId].producer.kind}`);
|
|
||||||
|
|
||||||
videoCalls[callId].producer.on('transportclose', () => {
|
|
||||||
const callId = socketDetails[socket.id];
|
|
||||||
console.log('transport for this producer closed', callId)
|
|
||||||
closeCall(callId);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send back to the client the Producer's id
|
console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producerVideo.id} | kind: ${videoCalls[callId].producerVideo.kind}`);
|
||||||
callback && callback({
|
|
||||||
id: videoCalls[callId].producer.id
|
videoCalls[callId].producerVideo.on('transportclose', () => {
|
||||||
});
|
const callId = socketDetails[socket.id];
|
||||||
|
console.log('transport for this producer closed', callId)
|
||||||
|
closeCall(callId);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send back to the client the Producer's id
|
||||||
|
callback && callback({
|
||||||
|
id: videoCalls[callId].producerVideo.id
|
||||||
|
});
|
||||||
|
} else if (kind === 'audio') {
|
||||||
|
videoCalls[callId].producerAudio = await videoCalls[callId].producerTransport.produce({
|
||||||
|
kind,
|
||||||
|
rtpParameters,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producerAudio.id} | kind: ${videoCalls[callId].producerAudio.kind}`);
|
||||||
|
|
||||||
|
videoCalls[callId].producerAudio.on('transportclose', () => {
|
||||||
|
const callId = socketDetails[socket.id];
|
||||||
|
console.log('transport for this producer closed', callId)
|
||||||
|
closeCall(callId);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send back to the client the Producer's id
|
||||||
|
callback && callback({
|
||||||
|
id: videoCalls[callId].producerAudio.id
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
|
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
}
|
}
|
||||||
@ -339,43 +365,79 @@ peers.on('connection', async socket => {
|
|||||||
console.log('[consume] callId', callId);
|
console.log('[consume] callId', callId);
|
||||||
|
|
||||||
// Check if the router can consume the specified producer
|
// Check if the router can consume the specified producer
|
||||||
if (videoCalls[callId].router.canConsume({
|
if (
|
||||||
producerId: videoCalls[callId].producer.id,
|
videoCalls[callId].router.canConsume({
|
||||||
rtpCapabilities
|
producerId: videoCalls[callId].producerVideo.id,
|
||||||
})) {
|
rtpCapabilities
|
||||||
|
}) &&
|
||||||
|
videoCalls[callId].router.canConsume({
|
||||||
|
producerId: videoCalls[callId].producerAudio.id,
|
||||||
|
rtpCapabilities
|
||||||
|
})
|
||||||
|
) {
|
||||||
console.log('[consume] Can consume', callId);
|
console.log('[consume] Can consume', callId);
|
||||||
// Transport can now consume and return a consumer
|
// Transport can now consume and return a consumer
|
||||||
videoCalls[callId].consumer = await videoCalls[callId].consumerTransport.consume({
|
videoCalls[callId].consumerVideo = await videoCalls[callId].consumerTransport.consume({
|
||||||
producerId: videoCalls[callId].producer.id,
|
producerId: videoCalls[callId].producerVideo.id,
|
||||||
rtpCapabilities,
|
rtpCapabilities,
|
||||||
paused: true,
|
paused: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
|
||||||
videoCalls[callId].consumer.on('transportclose', () => {
|
videoCalls[callId].consumerVideo.on('transportclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('transport close from consumer', callId);
|
console.log('transport close from consumer', callId);
|
||||||
closeCall(callId);
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
||||||
videoCalls[callId].consumer.on('producerclose', () => {
|
videoCalls[callId].consumerVideo.on('producerclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('producer of consumer closed', callId);
|
console.log('producer of consumer closed', callId);
|
||||||
closeCall(callId);
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
videoCalls[callId].consumerAudio = await videoCalls[callId].consumerTransport.consume({
|
||||||
|
producerId: videoCalls[callId].producerAudio.id,
|
||||||
|
rtpCapabilities,
|
||||||
|
paused: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
|
||||||
|
videoCalls[callId].consumerAudio.on('transportclose', () => {
|
||||||
|
const callId = socketDetails[socket.id];
|
||||||
|
console.log('transport close from consumer', callId);
|
||||||
|
closeCall(callId);
|
||||||
|
});
|
||||||
|
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
||||||
|
videoCalls[callId].consumerAudio.on('producerclose', () => {
|
||||||
|
const callId = socketDetails[socket.id];
|
||||||
|
console.log('producer of consumer closed', callId);
|
||||||
|
closeCall(callId);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('🟠 videoCalls[callId].consumerVideo', videoCalls[callId].consumerVideo);
|
||||||
|
console.log('🟠 videoCalls[callId].consumerAudio', videoCalls[callId].consumerAudio);
|
||||||
|
|
||||||
// From the consumer extract the following params to send back to the Client
|
// From the consumer extract the following params to send back to the Client
|
||||||
const params = {
|
const videoParams = {
|
||||||
id: videoCalls[callId].consumer.id,
|
id: videoCalls[callId].consumerVideo.id,
|
||||||
producerId: videoCalls[callId].producer.id,
|
producerId: videoCalls[callId].producerVideo.id,
|
||||||
kind: videoCalls[callId].consumer.kind,
|
kind: 'video',
|
||||||
rtpParameters: videoCalls[callId].consumer.rtpParameters,
|
rtpParameters: videoCalls[callId].consumerVideo.rtpParameters,
|
||||||
|
};
|
||||||
|
|
||||||
|
const audioParams = {
|
||||||
|
id: videoCalls[callId].consumerAudio.id,
|
||||||
|
producerId: videoCalls[callId].producerAudio.id,
|
||||||
|
kind: 'audio',
|
||||||
|
rtpParameters: videoCalls[callId].consumerAudio.rtpParameters,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('[consume] params', params);
|
console.log('[consume] params', params);
|
||||||
// Send the parameters to the client
|
// Send the parameters to the client
|
||||||
callback({ params });
|
callback({ videoParams, audioParams });
|
||||||
} else {
|
} else {
|
||||||
console.log(`[canConsume] Can't consume | callId ${callId}`);
|
console.log(`[canConsume] Can't consume | callId ${callId}`);
|
||||||
callback(null);
|
callback(null);
|
||||||
|
Loading…
Reference in New Issue
Block a user