import * as audio from "./audio" ; import * as avloader from "./avloader" ; import * as config from "./config" ; import * as net from "./net" ; import * as outproc from "./outproc" ; import { prot } from "./protocol" ; import * as ui from "./ui" ; import * as util from "./util" ; import * as vad from "./vad" ; import * as rtennui from "rtennui/rtennui.js" ; import * as wcp from "libavjs-webcodecs-polyfill" ; let inited = false ; declare let LibAVWebCodecs ; LibAVWebCodecs = wcp ; let connection ; let cap = null ; let idMap = null ; export async function initRTEnnui ( ) { if ( ! audio . userMediaRTC ) { util . events . addEventListener ( "usermediartcready" , initRTEnnui , { once : true } ) ; return ; } if ( ! inited ) { await avloader . loadLibAV ( ) ; await wcp . load ( ) ; await rtennui . load ( ) ; inited = true ; } if ( connection ) connection . disconnect ( ) ; const c = connection = new rtennui . Connection ( audio . ac ) ; idMap = Object . create ( null ) ; c . on ( "peer-joined" , ev => { if ( c !== connection ) return ; idMap [ ev . id ] = ev . info . uid ; } ) ; c . on ( "peer-left" , ev => { if ( c !== connection ) return ; delete idMap [ ev . id ] ; } ) ; c . on ( "track-started-audio" , ev => { if ( c !== connection ) return ; if ( ! ( ev . peer in idMap ) ) return ; rteTrackStarted ( idMap [ ev . peer ] , ev . node ) ; } ) ; c . on ( "*" , ev => { let str ; try { str = JSON . stringify ( ev ) ; } catch ( ex ) { str = "" + ev ; } console . log ( str ) ; } ) ; let connected = await new Promise < boolean > ( ( res ) => { let timeout = setTimeout ( ( ) => { timeout = null ; res ( false ) ; } , 30000 ) ; c . connect ( config . rtennuiUrl , { id : config . config . id , key : config . config . key , uid : net . selfId } ) . then ( ret => { if ( timeout ) { clearTimeout ( timeout ) ; res ( ret ) ; } else { if ( ret ) c . disconnect ( ) ; res ( false ) ; } } ) ; } ) ; if ( ! connected ) { connection = null ; return ; } rteAddAudioTrack ( ) ; util . events . addEventListener ( "usermediartcready" , rteAddAudioTrack ) ; } util . events . addEventListener ( "net.info." + prot . info . id , function ( ) { if ( config . useRTC ) initRTEnnui ( ) ; } ) ; async function rteAddAudioTrack ( ) { if ( cap ) { cap . close ( ) ; await connection . removeAudioTrack ( cap ) ; } cap = await rtennui . createAudioCapture ( audio . ac , audio . userMediaRTC ) ; connection . addAudioTrack ( cap , { frameSize : 5000 } ) ; cap . setVADState ( vad . rtcVadOn ? "yes" : "no" ) ; util . events . addEventListener ( "vad.rtc" , ( ) => { cap . setVADState ( vad . rtcVadOn ? "yes" : "no" ) ; } ) ; } function rteTrackStarted ( id , node ) { ui . videoAdd ( id , null ) ; const el = < any > ui . ui . video . users [ id ] . audio ; const msd = audio . ac . createMediaStreamDestination ( ) ; node . connect ( msd ) ; el . srcObject = msd . stream ; if ( el . paused ) el . play ( ) . catch ( net . promiseFail ( ) ) ; outproc . createCompressor ( id , audio . ac , msd . stream , ui . ui . video . users [ id ] . waveformWrapper ) ; } function rteTrackStopped ( id , node ) { if ( ui . ui . video . users [ id ] ) { const el = ui . ui . video . users [ id ] . audio ; el . srcObject = null ; } outproc . destroyCompressor ( id ) ; }