import { desktopCapturer , DesktopCapturerSource } from 'electron' ; import Logger from '../../utils/LoggerWithFilePrefix' ; import DesktopCapturerSourceType from './DesktopCapturerSourceType' ; export function getSourceTypeFromSourceID ( id ) { if ( id . includes ( DesktopCapturerSourceType . SCREEN ) ) { return DesktopCapturerSourceType . SCREEN ; } return DesktopCapturerSourceType . WINDOW ; } type SourcesDisappearListener = ( ids : string [ ] ) => void ; type SharingSessionID = string ; class DesktopCapturerSources { sources : Map < string , DesktopCapturerSourceWithType > ; lastAvailableScreenIDs : string [ ] ; lastAvailableWindowIDs : string [ ] ; onWindowClosedListeners : Map < SharingSessionID , SourcesDisappearListener [ ] > ; onScreenDisconnectedListeners : Map < SharingSessionID , SourcesDisappearListener [ ] > ; log = new Logger ( __filename ) ; constructor ( ) { this . sources = new Map < string , DesktopCapturerSourceWithType > ( ) ; this . lastAvailableScreenIDs = [ ] ; this . lastAvailableWindowIDs = [ ] ; this . onWindowClosedListeners = new Map < SharingSessionID , SourcesDisappearListener [ ] > ( ) ; this . onScreenDisconnectedListeners = new Map < SharingSessionID , SourcesDisappearListener [ ] > ( ) ; this . startRefreshDesktopCapturerSourcesLoop ( ) ; this . startPollForInactiveListenersLoop ( ) ; } getSourcesMap ( ) { return this . sources ; } startRefreshDesktopCapturerSourcesLoop ( ) { setInterval ( ( ) => { this . refreshDesktopCapturerSources ( ) ; } , 5000 ) ; } getScreenSources ( ) { const screenSources = [ ] ; [ ... this . sources . keys ( ) ] . forEach ( ( key ) => { const source = this . sources . get ( key ) ; if ( ! source ) return ; if ( source . type === DesktopCapturerSourceType . SCREEN ) { screenSources . push ( source . source ) ; } } ) ; return screenSources ; } getAppWindowSources ( ) { const appWindowSources = [ ] ; [ ... this . sources . keys ( ) ] . forEach ( ( key ) => { const source = this . sources . get ( key ) ; if ( ! source ) return ; if ( source . type === DesktopCapturerSourceType . WINDOW ) { appWindowSources . push ( source . source ) ; } } ) ; return appWindowSources ; } getSourceDisplayIDBySourceID ( sourceID ) { let displayID = '' ; [ ... this . sources . keys ( ) ] . forEach ( ( key ) => { const source = this . sources . get ( key ) ; if ( ! source ) return ; if ( source . source . id === sourceID ) { displayID = source . source . display_id ; } } ) ; return displayID ; } addWindowClosedListener ( _sharingSessionID , _callback ) { } addScreenDisconnectedListener ( _sharingSessionID , _callback ) { } async updateDesktopCapturerSources ( ) { this . sources = await this . getDesktopCapturerSources ( ) ; } getDesktopCapturerSources ( ) { return new Promise < Map < string , DesktopCapturerSourceWithType > > ( async ( resolve , reject ) => { const newSources = new Map < string , DesktopCapturerSourceWithType > ( ) ; try { const capturerSources = await desktopCapturer . getSources ( { types : [ DesktopCapturerSourceType . WINDOW , DesktopCapturerSourceType . SCREEN , ] , thumbnailSize : { width : 500 , height : 500 } , fetchWindowIcons : true , } ) ; capturerSources . forEach ( ( source ) => { newSources . set ( source . id , { type : getSourceTypeFromSourceID ( source . id ) , source , } ) ; } ) ; resolve ( newSources ) ; } catch ( e ) { reject ( ) ; } } ) ; } async refreshDesktopCapturerSources ( ) { try { await this . updateDesktopCapturerSources ( ) ; this . checkForClosedWindows ( ) ; this . checkForScreensDisconnected ( ) ; } catch ( e ) { this . log . error ( e ) ; } } startPollForInactiveListenersLoop ( ) { setInterval ( ( ) => { } , 1000 * 60 * 60 ) ; } checkForClosedWindows ( ) { } notifyOnWindowsClosedListeners ( _closedWindowsIDs ) { } checkForScreensDisconnected ( ) { } notifyOnScreensDisconnectedListeners ( _disconnectedScreensIDs ) { } } export default DesktopCapturerSources ;