// screenshare.js window.Kronick = window.Kronick || {}; /* * Screen-share-specific state. */ Kronick.screenShare = { room: null, initializedRoom: null, activeVideoTrack: null, activeAudioTrack: null, activeParticipantSid: null, activeParticipantName: '', isLocalSharing: false, listeners: null }; /* * Determine whether a publication is a screen-share * video track. */ Kronick.isScreenShareVideoPublication = function isScreenShareVideoPublication( publication ) { if (!publication) { return false; } const source = publication.source; const screenShareSource = window.LivekitClient ?.Track ?.Source ?.ScreenShare; return ( source === screenShareSource || source === 'screen_share' || source === 'screenshare' ); }; /* * Determine whether a publication is screen-share * audio. */ Kronick.isScreenShareAudioPublication = function isScreenShareAudioPublication( publication ) { if (!publication) { return false; } const source = publication.source; const screenShareAudioSource = window.LivekitClient ?.Track ?.Source ?.ScreenShareAudio; return ( source === screenShareAudioSource || source === 'screen_share_audio' || source === 'screenshareaudio' ); }; /* * Initialize screen-share listeners for a LiveKit room. * * Call this once each time a new Room instance is created. */ Kronick.initScreenShare = function initScreenShare(room) { if (!room) { return; } /* * Do not register the same listeners twice on the * same room. */ if ( Kronick.screenShare .initializedRoom === room ) { return; } Kronick.cleanupScreenShareListeners(); Kronick.screenShare.room = room; Kronick.screenShare.initializedRoom = room; const { RoomEvent } = LivekitClient; const handleTrackSubscribed = ( track, publication, participant ) => { if ( Kronick .isScreenShareVideoPublication( publication ) ) { Kronick.showScreenShareVideo( track, participant, false ); return; } if ( Kronick .isScreenShareAudioPublication( publication ) ) { Kronick.showScreenShareAudio( track ); } }; const handleTrackUnsubscribed = ( track, publication, participant ) => { if ( Kronick .isScreenShareVideoPublication( publication ) ) { Kronick.removeScreenShareVideo( track ); return; } if ( Kronick .isScreenShareAudioPublication( publication ) ) { Kronick.removeScreenShareAudio( track ); } }; const handleLocalTrackPublished = ( publication, participant ) => { const track = publication?.track; if (!track) { return; } if ( Kronick .isScreenShareVideoPublication( publication ) ) { Kronick.screenShare .isLocalSharing = true; Kronick.showScreenShareVideo( track, participant || room.localParticipant, true ); Kronick.updateScreenShareControls(); return; } if ( Kronick .isScreenShareAudioPublication( publication ) ) { Kronick.showScreenShareAudio( track ); } }; const handleLocalTrackUnpublished = ( publication ) => { if ( Kronick .isScreenShareVideoPublication( publication ) ) { Kronick.screenShare .isLocalSharing = false; Kronick.removeScreenShareVideo( publication.track ); Kronick.updateScreenShareControls(); return; } if ( Kronick .isScreenShareAudioPublication( publication ) ) { Kronick.removeScreenShareAudio( publication.track ); } }; const handleDisconnected = () => { Kronick.resetScreenShare(); }; room.on( RoomEvent.TrackSubscribed, handleTrackSubscribed ); room.on( RoomEvent.TrackUnsubscribed, handleTrackUnsubscribed ); room.on( RoomEvent.LocalTrackPublished, handleLocalTrackPublished ); room.on( RoomEvent.LocalTrackUnpublished, handleLocalTrackUnpublished ); room.on( RoomEvent.Disconnected, handleDisconnected ); Kronick.screenShare.listeners = { handleTrackSubscribed, handleTrackUnsubscribed, handleLocalTrackPublished, handleLocalTrackUnpublished, handleDisconnected }; Kronick.detectExistingScreenShares( room ); Kronick.updateScreenShareControls(); }; /* * Remove listeners from the previous room. */ Kronick.cleanupScreenShareListeners = function cleanupScreenShareListeners() { const room = Kronick.screenShare .initializedRoom; const listeners = Kronick.screenShare.listeners; if ( !room || !listeners ) { Kronick.screenShare .initializedRoom = null; Kronick.screenShare .listeners = null; return; } const { RoomEvent } = LivekitClient; room.off( RoomEvent.TrackSubscribed, listeners.handleTrackSubscribed ); room.off( RoomEvent.TrackUnsubscribed, listeners.handleTrackUnsubscribed ); room.off( RoomEvent.LocalTrackPublished, listeners.handleLocalTrackPublished ); room.off( RoomEvent.LocalTrackUnpublished, listeners.handleLocalTrackUnpublished ); room.off( RoomEvent.Disconnected, listeners.handleDisconnected ); Kronick.screenShare .initializedRoom = null; Kronick.screenShare .listeners = null; }; /* * Check whether anyone was already sharing before this * file initialized or before the local user joined. */ Kronick.detectExistingScreenShares = function detectExistingScreenShares(room) { if (!room) { return; } const participants = [ room.localParticipant, ...room.remoteParticipants.values() ]; participants.forEach(participant => { if (!participant) { return; } participant .getTrackPublications() .forEach(publication => { const track = publication.track; if (!track) { return; } if ( Kronick .isScreenShareVideoPublication( publication ) ) { const isLocal = participant === room.localParticipant; Kronick.showScreenShareVideo( track, participant, isLocal ); } if ( Kronick .isScreenShareAudioPublication( publication ) ) { Kronick.showScreenShareAudio( track ); } }); }); }; /* * Start or stop the local user's screen share. */ Kronick.toggleScreenShare = async function toggleScreenShare() { const room = Kronick.state.room; if (!room?.localParticipant) { alert( 'Join a room before sharing ' + 'your screen.' ); return; } const button = document.getElementById( 'screenShareBtn' ); if (button) { button.disabled = true; } try { const currentlySharing = Kronick.screenShare .isLocalSharing || Kronick .localParticipantIsScreenSharing( room.localParticipant ); await room.localParticipant .setScreenShareEnabled( !currentlySharing, { audio: true, resolution: { width: 1920, height: 1080, frameRate: 30 } } ); Kronick.screenShare .isLocalSharing = !currentlySharing; if ( Kronick.screenShare .isLocalSharing ) { Kronick.openScreenShareWindow(); } else { Kronick.removeLocalScreenShare(); } } catch (err) { console.error( 'Could not change screen-share state:', err ); /* * NotAllowedError normally means the user * cancelled the browser share picker. */ if ( err.name !== 'NotAllowedError' ) { alert( 'Screen sharing failed. ' + 'Check the browser console.' ); } } finally { if (button) { button.disabled = false; } Kronick.updateScreenShareControls(); } }; /* * Check the local participant's current publications. */ Kronick.localParticipantIsScreenSharing = function localParticipantIsScreenSharing( participant ) { if (!participant) { return false; } let sharing = false; participant .getTrackPublications() .forEach(publication => { if ( publication.track && Kronick .isScreenShareVideoPublication( publication ) ) { sharing = true; } }); return sharing; }; /* * Stop screen sharing explicitly. */ Kronick.stopScreenShare = async function stopScreenShare() { const room = Kronick.state.room; if (!room?.localParticipant) { return; } try { await room.localParticipant .setScreenShareEnabled( false ); } catch (err) { console.error( 'Could not stop screen sharing:', err ); } Kronick.screenShare .isLocalSharing = false; Kronick.removeLocalScreenShare(); Kronick.updateScreenShareControls(); }; /* * Attach a screen-share video track to the popup. */ Kronick.showScreenShareVideo = function showScreenShareVideo( track, participant, isLocal = false ) { if (!track) { return; } const container = document.getElementById( 'screenShareVideoContainer' ); const emptyMessage = document.getElementById( 'screenShareEmpty' ); const title = document.getElementById( 'screenShareTitle' ); if (!container) { console.error( 'Screen-share container ' + 'was not found.' ); return; } Kronick.detachCurrentScreenShareVideo(); const attachedElement = track.attach(); attachedElement.classList.add( 'screen-share-video' ); attachedElement.autoplay = true; attachedElement.playsInline = true; attachedElement.controls = false; if (isLocal) { attachedElement.muted = true; } container.innerHTML = ''; container.appendChild( attachedElement ); if (emptyMessage) { emptyMessage.style.display = 'none'; } const participantName = participant?.identity || ( isLocal ? 'You' : 'Unknown participant' ); Kronick.screenShare .activeVideoTrack = track; Kronick.screenShare .activeParticipantSid = participant?.sid || null; Kronick.screenShare .activeParticipantName = participantName; if (title) { title.textContent = isLocal ? 'Your Screen' : `${participantName}'s Screen`; } Kronick.openScreenShareWindow(); }; /* * Attach screen-share audio separately. */ Kronick.showScreenShareAudio = function showScreenShareAudio(track) { if (!track) { return; } Kronick.detachCurrentScreenShareAudio(); const audioElement = track.attach(); audioElement.autoplay = true; audioElement.controls = false; audioElement.classList.add( 'screen-share-audio' ); document.body.appendChild( audioElement ); Kronick.screenShare .activeAudioTrack = track; }; /* * Remove a specific video track. */ Kronick.removeScreenShareVideo = function removeScreenShareVideo(track) { if ( track && Kronick.screenShare .activeVideoTrack && track !== Kronick.screenShare .activeVideoTrack ) { return; } Kronick.detachCurrentScreenShareVideo(); const container = document.getElementById( 'screenShareVideoContainer' ); const emptyMessage = document.getElementById( 'screenShareEmpty' ); const title = document.getElementById( 'screenShareTitle' ); if (container) { container.innerHTML = ''; } if (emptyMessage) { emptyMessage.style.display = 'flex'; } if (title) { title.textContent = '🖥️ Screen Share 🖥️'; } Kronick.screenShare .activeParticipantSid = null; Kronick.screenShare .activeParticipantName = ''; }; /* * Remove a specific audio track. */ Kronick.removeScreenShareAudio = function removeScreenShareAudio(track) { if ( track && Kronick.screenShare .activeAudioTrack && track !== Kronick.screenShare .activeAudioTrack ) { return; } Kronick.detachCurrentScreenShareAudio(); }; /* * Detach the active screen-share video element. */ Kronick.detachCurrentScreenShareVideo = function detachCurrentScreenShareVideo() { const track = Kronick.screenShare .activeVideoTrack; if (track) { try { track.detach().forEach( element => { element.remove(); } ); } catch (err) { console.debug( 'Could not detach ' + 'screen-share video:', err ); } } Kronick.screenShare .activeVideoTrack = null; }; /* * Detach the active screen-share audio element. */ Kronick.detachCurrentScreenShareAudio = function detachCurrentScreenShareAudio() { const track = Kronick.screenShare .activeAudioTrack; if (track) { try { track.detach().forEach( element => { element.remove(); } ); } catch (err) { console.debug( 'Could not detach ' + 'screen-share audio:', err ); } } document .querySelectorAll( '.screen-share-audio' ) .forEach(element => { element.remove(); }); Kronick.screenShare .activeAudioTrack = null; }; /* * Remove only the local preview after stopping sharing. */ Kronick.removeLocalScreenShare = function removeLocalScreenShare() { const room = Kronick.state.room; const activeSid = Kronick.screenShare .activeParticipantSid; if ( room?.localParticipant && activeSid === room.localParticipant.sid ) { Kronick.removeScreenShareVideo(); Kronick.removeScreenShareAudio(); } }; /* * Show the popup window. */ Kronick.openScreenShareWindow = function openScreenShareWindow() { Kronick.showToolWindow( 'screenShareFrameWrap' ); }; /* * Hide the popup without stopping someone else's share. * * If the local user is sharing, this only hides the * preview. The Stop Sharing button actually stops it. */ Kronick.closeScreenShareWindow = function closeScreenShareWindow() { Kronick.hideToolWindow( 'screenShareFrameWrap' ); }; /* * Update buttons and status text. */ Kronick.updateScreenShareControls = function updateScreenShareControls() { const shareButton = document.getElementById( 'screenShareBtn' ); const stopButton = document.getElementById( 'screenShareStopBtn' ); const room = Kronick.state.room; const isSharing = Kronick.screenShare .isLocalSharing || Kronick .localParticipantIsScreenSharing( room?.localParticipant ); Kronick.screenShare .isLocalSharing = isSharing; if (shareButton) { shareButton.textContent = isSharing ? 'Stop Sharing' : '🖥️🖥️'; shareButton.classList.toggle( 'active', isSharing ); shareButton.disabled = !room?.localParticipant; } if (stopButton) { stopButton.hidden = !isSharing; } }; /* * Reset everything when leaving the room. */ Kronick.resetScreenShare = function resetScreenShare() { Kronick.detachCurrentScreenShareVideo(); Kronick.detachCurrentScreenShareAudio(); Kronick.screenShare.room = null; Kronick.screenShare .isLocalSharing = false; Kronick.screenShare .activeParticipantSid = null; Kronick.screenShare .activeParticipantName = ''; const container = document.getElementById( 'screenShareVideoContainer' ); const emptyMessage = document.getElementById( 'screenShareEmpty' ); const title = document.getElementById( 'screenShareTitle' ); if (container) { container.innerHTML = ''; } if (emptyMessage) { emptyMessage.style.display = 'flex'; } if (title) { title.textContent = '🖥️ Screen Share 🖥️'; } Kronick.closeScreenShareWindow(); Kronick.updateScreenShareControls(); };