I appreciate the feedback, my problem is summed up like this.
A brief explanation of how the code works.
When I access the video conference room, I have a screen that defines the username forwarding to the room using the access link already with the room defined, for example,
xyz.com/room/room123at this moment I use socketIO to inform the nodejs server which room is the user, do the treatment in the database and return the socketIO data informing which room is connected
and the socket_id, at this moment with the data formatted, start to add the socket id to the RTC, generating an array of each socket_id for the RTCPeerConnection.
// my code commented with some explanations
socket.on('join room', async (data) => {
// add socketID connections
connections[sid] = new RTCPeerConnection(configuration)
// send the candidate event data
connections[sid].onicacandidate
// I use it to add new video entries
connections[sid].ontrack
// I use it to renegotiate the session
connections[sid].onnegotiationneeded
// start the stream
mystreamvideo = await navigator.mediaDevices.getUserMedia(mediaConstraints)
// do a for to get the connections and add the video tracks
for(key in connections) {
let senders = connections[sid].getSenders();
let audio_sender = (senders && senders.length) ? senders.filter(sender => {return sender && sender.track && sender.track.kind == 'audio'}) : null;
console.log('audio sender', audio_sender);
if((!audio_sender || !audio_sender.length) && audio_track) {
audio_track_stream = await connections[sid].addTrack(audio_track, mystreamvideo);
console.log('startStream: add audio');
senders = connections[sid].getSenders();
}
let video_sender = (senders && senders.length) ? senders.filter(sender => {return sender && sender.track && sender.track.kind == 'video'}) : null;
if(video_sender && video_sender.length) {
if(!video_track) {
await connections[sid].removeTrack(video_sender[0]);
console.log('startStream: remove video');
} else if(
video_track.id != video_sender[0].
track.id) {
await video_sender[0].replaceTrack(video_track);
console.log('startStream: switch video');
}
} else if(video_track) {
// await connections[sid].addTrack(video_track, myscreenshare ? myscreenshare : mystreamvideo);
if(myscreenshare) {
await connections[sid].addTrack(video_track); //streamless addtrack -> add track to remote peer stream manually, to add along with audio
console.log('startStream: add screen video');
} else {
await connections[sid].addTrack(video_track, mystreamvideo);
console.log('startStream: add webcam video');
}
}
} // close for()
})
I think this, thank you.