openvidu-js-screen-share: Updated code

This commit is contained in:
Carlos Santos 2023-10-26 12:16:08 +02:00
parent eb73715e76
commit 1c7fa1b8ec

View File

@ -94,53 +94,59 @@ function leaveRoom() {
document.getElementById('room').style.display = 'none'; document.getElementById('room').style.display = 'none';
} }
//
async function toggleScreenShare() { async function toggleScreenShare() {
console.log('Toggling screen share'); console.log('Toggling screen share');
const enabled = !isScreenShared; const enabled = !isScreenShared;
if (enabled) { if (enabled) {
// Enable screen sharing // Enable screen sharing
screenSharePublication = await room.localParticipant?.setScreenShareEnabled(enabled); try {
screenSharePublication =
await room.localParticipant?.setScreenShareEnabled(enabled);
} catch (error) {
console.error('Error enabling screen sharing', error);
}
if (screenSharePublication) { if (screenSharePublication) {
console.log('Screen sharing enabled', screenSharePublication); console.log('Screen sharing enabled', screenSharePublication);
isScreenShared = enabled; isScreenShared = enabled;
// Attach the screen share track to the video container // Attach the screen share track to the video container
const element = screenSharePublication.track.attach(); const element = screenSharePublication.track.attach();
element.id = screenSharePublication.trackSid; element.id = screenSharePublication.trackSid;
element.className = 'removable'; element.className = 'removable';
document.getElementById('video-container').appendChild(element); document.getElementById('video-container').appendChild(element);
// Add user data for the screen share // Add user data for the screen share
appendUserData(element, `${myUserName}_SCREEN`); appendUserData(element, `${myUserName}_SCREEN`);
// Listen for the 'ended' event to handle screen sharing stop // Listen for the 'ended' event to handle screen sharing stop
screenSharePublication.addListener('ended', async () => { screenSharePublication.addListener('ended', async () => {
console.debug('Clicked native stop button. Stopping screen sharing'); console.debug('Clicked native stop button. Stopping screen sharing');
await stopScreenSharing(); await stopScreenSharing();
}); });
} }
} else { } else {
// Disable screen sharing // Disable screen sharing
await stopScreenSharing(); await stopScreenSharing();
} }
} }
async function stopScreenSharing() { async function stopScreenSharing() {
isScreenShared = false; try {
await room.localParticipant?.setScreenShareEnabled(false); await room.localParticipant?.setScreenShareEnabled(false);
const trackSid = screenSharePublication?.trackSid; isScreenShared = false;
const trackSid = screenSharePublication?.trackSid;
if (trackSid) { if (trackSid) {
document.getElementById(trackSid)?.remove(); document.getElementById(trackSid)?.remove();
removeUserData({ identity: `${myUserName}_SCREEN` }); removeUserData({ identity: `${myUserName}_SCREEN` });
}
screenSharePublication = undefined;
} catch (error) {
console.error('Error stopping screen sharing', error);
} }
screenSharePublication = undefined; }
}
window.onbeforeunload = function () { window.onbeforeunload = function () {
if (room) room.disconnect(); if (room) room.disconnect();