I’m working on a web application that uses PeerJS for real-time communication. Initially, I was rendering video and name information for 2 participants simultaneously, and that was working well. However, I recently attempted to expand the application to accommodate 3 participants at the same time, and I’m encountering an issue when sharing screens.
Problem: When I initiate screen sharing, not all of the participants can see the shared screen. Although I have successfully rendered the video and name for the 3rd participant, but candidate’s shared screen is visible to only one interviewer.
`const generateRoom = (room,randomNum)=>{
room_id = "DELTA" + room + randomNum + "MEET";
console.log("generateRoom start--->")
peer = new Peer(room_id)
peer.on('open', (id) => {
getUserMediaScreen({ video: true, audio: true },
(stream) => {
// Success: Store the stream
local_stream = stream;
console.log('User media obtained successfully.');
},
(err) => {
console.error('Error obtaining user media:', err);
if (err.name === 'NotAllowedError') {
console.log('User denied permission for media access.');
} else if (err.name === 'NotFoundError') {
console.log('Requested media devices not found.');
} else {
console.log('Other getUserMedia error:', err);
}
}
);
});
peer.on('call', (call) => {
console.log(call);
currentPeer = call;
call.answer(local_stream);
console.log(local_stream);
call.on('stream', (stream) => {
console.log(stream);
var sharedproUserRoom = "${Client}"
if (sharedproUserRoom == "Client") {
setTimeout(() => {
setRemoteStream(stream)
}, 1000);
}
})
currentPeer = call;
console.log("---> on call local_stream",local_stream)
})
peer.on('error', (err) => {
console.error('PeerJS error:', err);
$('#startScreenShare').show();
$('#stopScreenShare').hide();
$('#questionCodeBox').hide();
$('#modal-background').hide();
screenSharing = false;
if (!screenSharing) return;
let videoTrack = local_stream.getVideoTracks()[0];
if (peer && currentPeer) {
let sender = currentPeer.peerConnection.getSenders().find(function (s) {
return s.track.kind == videoTrack.kind;
})
sender.replaceTrack(videoTrack)
}
screenStream.getTracks().forEach(function (track) {
track.stop();
});
if(peer){
peer.destroy();
}
});
peer.on('close', () => {
console.log("close function called >>>")
// Find and remove the user's ID from the participantsInRoom array
const index = participantsInRoom.indexOf(peer.id);
if (index !== -1) {
participantsInRoom.splice(index, 1);
}
});
console.log('generateRoom end>>')
}
function startScreenShare(room) {
let randomNumber = Math.floor((Math.random() * 100000) + 1);
stompClient.send(‘/app/application’, {}, JSON.stringify({ jobId : jobId, candidateId : candidateId, roomId:room,activity :”shareScreenstartedTrue”,randomNumber:randomNumber}))
joinRoom(room,randomNumber);
if (screenSharing) {
stopScreenSharing()
}
navigator.mediaDevices.getDisplayMedia({video:true}).then((stream) => {
screenStream = stream;
let videoTrack = screenStream.getVideoTracks()[0];
// Check if the user is sharing the entire screen
let displaySurface = videoTrack.getSettings().displaySurface;
const url = window.location.href
let segments = url.split('/');
let jobId = segments[segments.length - 2];
let candidateId = segments[segments.length - 1];
if(displaySurface == 'window' || displaySurface== 'browser'){
stompClient.send('/app/application', {}, JSON.stringify({jobId : jobId, candidateId : candidateId,userAlertMessage:displaySurface }))
}
if (displaySurface !== 'monitor') {
screenSharing = true
stopScreenSharing();
$.notify("Please share entire screen!", { className: "failure" });
return;
}
saveUserScreenSharedActivity(room,true);
videoTrack.onended = () => {
stopScreenSharing()
}
if(peer && currentPeer) {
let sender = currentPeer.peerConnection.getSenders().find(function (s) {
return s.track.kind == videoTrack.kind;
})
if (sender) {
sender.replaceTrack(videoTrack)
screenSharing = true
setTimeout(() => {
stompClient.send('/app/application', {}, JSON.stringify({ jobId : jobId, candidateId : candidateId, shareScreen: "true",activity :"shareScreenTrue" }))
}, 1000);
$('#stopScreenShare').show();
$('#questionCodeBox').show();
$('#startScreenShare').hide();
}
else {
console.error("Sender not found for screen sharing.");
}
}
// Share screen with all participants in the room
peer.on('open', (id) => {
// Loop through all participants in the room and initiate screen sharing calls
participantsInRoom.forEach((participantId) => {
if (participantId !== id) {
const screenSharingCall = peer.call(participantId, screenStream, { metadata: { type: 'screenshare' } });
console.log("screen sharing call>>>-->>",scre)
screenSharingCall.on('close', () => {
// Handle screen sharing call closure
console.log("screen sharing call closed.......", )
});
}
});
});
function setupPeerConnection() {
// peerConnection = new RTCPeerConnection();
currentPeer.peerConnection.oniceconnectionstatechange = handleICEConnectionStateChange;
screenStream.getTracks().forEach(track => {
currentPeer.peerConnection.addTrack(track, screenStream);
});
}
if (currentPeer.peerConnection.iceConnectionState === 'disconnected') {
$('#startScreenShare').show();
$('#stopScreenShare').hide();
$('#questionCodeBox').hide();
$('#modal-background').hide();
screenSharing = false;
if (!screenSharing) return;
let videoTrack = local_stream.getVideoTracks()[0];
if (peer && currentPeer) {
let sender = currentPeer.peerConnection.getSenders().find(function (s) {
return s.track.kind == videoTrack.kind;
})
sender.replaceTrack(videoTrack)
setupPeerConnection();
}
screenStream.getTracks().forEach(function (track) {
track.stop();
});
}
})
}`
Error screenshot:
console error screenshot