import debug from "debug" ; import RootStore from "../stores" ; import { getUserDevices , getUserVideoTrack , getUserAudioTrack , getDisplayVideoTrack , } from "../utils/webrtc" ; import { joinRoom } from "./room" ; const log = debug ( "effect:settings" ) ; export const changeDispName = ( { client } ) => ( name ) => { log ( "changeDispName()" , `${ client . displayName } => ${ name }` ) ; client . displayName = name ; } ; export const enableUserVideo = ( { media , ui } ) => async ( ) => { log ( "enableUserVideo()" ) ; const { videoInDevices } = await getUserDevices ( { video : true } ) . catch ( ( err ) => { throw ui . showError ( err ) ; } ) ; if ( videoInDevices === null ) { throw ui . showError ( new Error ( "getUserDevices() returns null" ) ) ; } if ( videoInDevices . length === 0 ) { throw ui . showError ( new Error ( "At leaset one video input device needed!" ) ) ; } const [ { deviceId } ] = videoInDevices ; const videoTrack = await getUserVideoTrack ( deviceId ) . catch ( ( err ) => { throw ui . showError ( err ) ; } ) ; media . releaseVideoDevice ( ) ; media . setVideoTrack ( videoTrack , "camera" , deviceId ) ; const devices = await getUserDevices ( { video : true } ) . catch ( ( err ) => { throw ui . showError ( err ) ; } ) ; media . setVideoDevices ( devices ) ; log ( "video devices" , devices . videoInDevices ) ; } ; export const enableDisplayVideo = ( store ) => async ( ) => { log ( "enableDisplayVideo()" ) ; const { media , ui , notification } = store ; const videoTrack = await getDisplayVideoTrack ( ) . catch ( ( err ) => { if ( err . name === "NotAllowedError" ) { } else { throw ui . showError ( err ) ; } } ) ; if ( ! ( videoTrack instanceof MediaStreamTrack ) ) { notification . showInfo ( "Display selection was cancelled" ) ; notification . showInfo ( "Or your device does not support display sharing" ) ; log ( "selection was cancelled or not supported" ) ; return ; } videoTrack . addEventListener ( "ended" , disableDisplayVideo ( store ) , { once : true , } ) ; media . releaseVideoDevice ( ) ; media . setVideoTrack ( videoTrack , "display" , videoTrack . label ) ; } ; export const disableUserVideo = ( { media } ) => ( ) => { log ( "disableUserVideo()" ) ; media . deleteVideoTrack ( ) ; } ; export const disableDisplayVideo = ( { media } ) => ( ) => { log ( "disableDisplayVideo()" ) ; media . deleteVideoTrack ( ) ; } ; export const changeAudioDeviceId = ( { media , ui } ) => async ( deviceId ) => { log ( "changeAudioDeviceId" , deviceId ) ; media . releaseAudioDevice ( ) ; const audioTrack = await getUserAudioTrack ( deviceId ) . catch ( ( err ) => { throw ui . showError ( err ) ; } ) ; media . setAudioTrack ( audioTrack , deviceId ) ; } ; export const changeVideoDeviceId = ( { media , ui } ) => async ( deviceId ) => { log ( "changeVideoDeviceId" , deviceId ) ; media . releaseVideoDevice ( ) ; const videoTrack = await getUserVideoTrack ( deviceId ) . catch ( ( err ) => { throw ui . showError ( err ) ; } ) ; media . setVideoTrack ( videoTrack , "camera" , deviceId ) ; } ; export const toggleAudioMuted = ( { media } ) => ( ) => { log ( "toggleAudioMuted()" ) ; media . toggleMuted ( "audio" ) ; } ; export const toggleVideoMuted = ( { media } ) => ( ) => { log ( "toggleVideoMuted()" ) ; media . toggleMuted ( "video" ) ; } ; export const closeSettings = ( { ui } ) => ( ) => { log ( "closeSettings()" ) ; ui . isSettingsOpen = false ; } ; export const joinConference = ( store ) => ( ) => { log ( "joinConference()" ) ; const { ui , room } = store ; if ( room . isJoined ) { throw ui . showError ( new Error ( "Already in the room!" ) ) ; } joinRoom ( store ) ; ui . isSettingsOpen = false ; } ;