import { findElementByType } from '../helpers' ; import logger from '../logger' ; const RTCPeerConnection = ( config ) => new window . RTCPeerConnection ( config ) ; const getUserMedia = ( constraints ) => navigator . mediaDevices . getUserMedia ( constraints ) ; const getDisplayMedia = ( constraints ) => navigator . mediaDevices . getDisplayMedia ( constraints ) ; const enumerateDevices = ( ) => navigator . mediaDevices . enumerateDevices ( ) ; const enumerateDevicesByKind = async ( filterByKind = null ) => { let devices = await enumerateDevices ( ) . catch ( ( error ) => [ ] ) ; if ( filterByKind ) { devices = devices . filter ( ( { kind } ) => kind === filterByKind ) ; } return devices ; } ; const getSupportedConstraints = ( ) => navigator . mediaDevices . getSupportedConstraints ( ) ; const streamIsValid = ( stream ) => stream && stream instanceof MediaStream ; const attachMediaStream = ( tag , stream ) => { const element = findElementByType ( tag ) ; if ( element === null ) { return ; } if ( ! element . getAttribute ( 'autoplay' ) ) { element . setAttribute ( 'autoplay' , 'autoplay' ) ; } if ( ! element . getAttribute ( 'playsinline' ) ) { element . setAttribute ( 'playsinline' , 'playsinline' ) ; } element . srcObject = stream ; } ; const detachMediaStream = ( tag ) => { const element = findElementByType ( tag ) ; if ( element ) { element . srcObject = null ; } } ; const muteMediaElement = ( tag ) => { const element = findElementByType ( tag ) ; if ( element ) { element . muted = true ; } } ; const unmuteMediaElement = ( tag ) => { const element = findElementByType ( tag ) ; if ( element ) { element . muted = false ; } } ; const toggleMuteMediaElement = ( tag ) => { const element = findElementByType ( tag ) ; if ( element ) { element . muted = ! element . muted ; } } ; const setMediaElementSinkId = async ( tag , deviceId ) : Promise < boolean > => { const element = findElementByType ( tag ) ; if ( element === null ) { logger . info ( 'No HTMLMediaElement to attach the speakerId' ) ; return false ; } if ( typeof deviceId !== 'string' ) { logger . info ( `Invalid speaker deviceId: '${ deviceId }'` ) ; return false ; } try { await element . setSinkId ( deviceId ) ; return true ; } catch ( error ) { return false ; } } ; const sdpToJsonHack = ( sdp ) => sdp ; const stopTrack = ( track ) => { if ( track && track . readyState === 'live' ) { track . stop ( ) ; } } ; const stopStream = ( stream ) => { if ( streamIsValid ( stream ) ) { stream . getTracks ( ) . forEach ( stopTrack ) ; } stream = null ; } ; export { RTCPeerConnection , getUserMedia , getDisplayMedia , enumerateDevices , enumerateDevicesByKind , getSupportedConstraints , streamIsValid , attachMediaStream , detachMediaStream , sdpToJsonHack , stopStream , stopTrack , muteMediaElement , unmuteMediaElement , toggleMuteMediaElement , setMediaElementSinkId , } ;