import * as audio from "./audio" ; import * as config from "./config" ; import * as log from "./log" ; import * as net from "./net" ; import { prot } from "./protocol" ; import * as ui from "./ui" ; import * as util from "./util" ; export let userMediaVideo = null ; export let userMediaVideoID = null ; export let videoLatency = 0 ; export function getVideo ( id , res ) { return Promise . all ( [ ] ) . then ( function ( ) { if ( id === "-screen" ) { return ( < any > navigator . mediaDevices ) . getDisplayMedia ( { video : { cursor : { ideal : "motion" } } } ) ; } else if ( id === "-none" ) { return null ; } else { const opts = { deviceId : id , aspectRatio : { ideal : 16 / 9 } , facingMode : { ideal : "user" } , frameRate : { ideal : 30 } , height : < any > { ideal : res , max : res } } ; if ( res === 0 ) opts . height = { ideal : 17280 } ; return navigator . mediaDevices . getUserMedia ( { video : opts } ) . then ( function ( ret ) { if ( ret ) { return ret ; } else { opts . height = { ideal : res } ; return navigator . mediaDevices . getUserMedia ( { video : opts } ) ; } } ) ; } } ) ; } export function shareVideo ( id , res ) { return Promise . all ( [ ] ) . then ( ( ) => { if ( userMediaVideo ) { userMediaVideo . getTracks ( ) . forEach ( function ( track ) { track . stop ( ) ; } ) ; userMediaVideo = null ; util . dispatchEvent ( "usermediavideostopped" , { } ) ; } return getVideo ( id , res ) ; } ) . then ( userMediaIn => { userMediaVideo = userMediaIn ; ui . videoAdd ( net . selfId , config . username ) ; const v = ui . ui . video . users [ net . selfId ] . video ; const s = ui . ui . video . users [ net . selfId ] . standin ; if ( userMediaVideo ) { userMediaVideoID = id ; videoLatency = ( ( < any > userMediaVideo . getVideoTracks ( ) [ 0 ] . getSettings ( ) ) . latency * 1000 ) || 0 ; util . dispatchEvent ( "usermediavideoready" , { } ) ; v . srcObject = userMediaVideo ; v . play ( ) . catch ( function ( ) { } ) ; s . style . display = "none" ; net . updateAdminPerm ( { videoDevice : userMediaVideoID } , true ) ; } else { userMediaVideoID = null ; videoLatency = 0 ; v . srcObject = audio . userMedia ; v . srcObject = null ; s . style . display = "" ; net . updateAdminPerm ( { videoDevice : "-none" } , true ) ; } updateVideoButtons ( ) ; if ( ! config . useRTC ) { ui . ui . video . mainWrapper . style . display = userMediaVideo ? "" : "none" ; ui . updateVideoUI ( net . selfId ) ; ui . resizeUI ( ) ; } } ) . catch ( ( ) => { log . pushStatus ( "video" , "Failed to capture video!" ) ; setTimeout ( function ( ) { log . popStatus ( "video" ) ; } , 10000 ) ; updateVideoButtons ( ) ; } ) ; } export function updateVideoButtons ( ) { const per = ui . ui . persistent ; const cam = per . camera ; const scr = per . shareScreen ; const videoConfig = ui . ui . panels . videoConfig ; cam . setAttribute ( "aria-label" , "Camera" ) ; cam . innerHTML = '<i class="fas fa-video-slash"></i><span class="menu-extra">Camera</span>' ; cam . onclick = function ( ) { if ( videoConfig . device . value !== "-none" ) { shareVideo ( videoConfig . device . value , + videoConfig . res . value ) ; } else { ui . showPanel ( videoConfig , videoConfig . device ) ; } } ; scr . setAttribute ( "aria-label" , "Share your screen" ) ; scr . innerHTML = '<i class="fas fa-desktop" style="position: relative;"><i class="fas fa-slash" style="position: absolute; left: -0.1em;"></i></i><span class="menu-extra">Share your screen</span>' ; scr . onclick = function ( ) { shareVideo ( "-screen" , 0 ) ; } ; if ( userMediaVideoID === "-screen" ) { scr . setAttribute ( "aria-label" , "Stop sharing your screen" ) ; scr . innerHTML = '<i class="fas fa-desktop"></i><span class="menu-extra">Stop sharing your screen</span>' ; scr . onclick = function ( ) { shareVideo ( "-none" , 0 ) ; } ; } else if ( userMediaVideoID !== null ) { cam . setAttribute ( "aria-label" , "Disable camera" ) ; cam . innerHTML = '<i class="fas fa-video"></i><span class="menu-extra">Disable camera</span>' ; cam . onclick = function ( ) { shareVideo ( "-none" , 0 ) ; } ; } } util . events . addEventListener ( "net.admin.video" , function ( ev ) { const action = ev . detail . action ; const arg = ev . detail . arg ; const acts = prot . flags . admin . actions ; switch ( action ) { case acts . videoInput : ui . ui . panels . videoConfig . device . value = arg ; net . updateAdminPerm ( { videoDevice : arg } ) ; shareVideo ( arg , + ui . ui . panels . videoConfig . res . value ) ; break ; case acts . videoRes : ui . ui . panels . videoConfig . res . value = arg ; net . updateAdminPerm ( { videoRes : + arg } ) ; shareVideo ( ui . ui . panels . videoConfig . device . value , + arg ) ; break ; } } ) ;