export const commaString = ( words ) => { let result = '' ; if ( typeof words === 'undefined' ) return result ; for ( const word of words ) { result = result + word + ', ' ; } return result . slice ( 0 , - 2 ) ; } ; export const range = ( from , to , step = 1 ) => [ ... Array ( Math . floor ( ( to - from ) / step ) + 1 ) ] . map ( ( _ , i ) => from + i * step ) ; export const getVideoBoxSize = ( X , Y , n , aspect_ratio = 1 ) => { const tile_count = n ; const b = Y ; const a = X ; let sizeX = Math . sqrt ( ( b * a * aspect_ratio ) / tile_count ) ; let numberOfPossibleWholeTilesH = Math . floor ( ( b * aspect_ratio ) / sizeX ) ; let numberOfPossibleWholeTilesW = Math . floor ( a / sizeX ) ; let total = numberOfPossibleWholeTilesH * numberOfPossibleWholeTilesW ; while ( total < tile_count ) { sizeX -- ; numberOfPossibleWholeTilesH = Math . floor ( ( b * aspect_ratio ) / sizeX ) ; numberOfPossibleWholeTilesW = Math . floor ( a / sizeX ) ; total = numberOfPossibleWholeTilesH * numberOfPossibleWholeTilesW ; } return { x : sizeX , y : sizeX / aspect_ratio , } ; } ; export const transformSdp = ( sdp , bandwidth ) => { const modifier = 'TIAS' ; bandwidth = ( bandwidth >>> 0 ) * 1000 ; sdp = sdp . replace ( new RegExp ( 'b=' + modifier + ':.*\r\n' ) , '' ) ; sdp = sdp . replace ( /(m=video.*\r\n)/g , `$1b=${ modifier }:${ bandwidth }\r\n` ) ; return sdp ; } ; export const blankVideo = ( { width = 640 , height = 480 } = { } ) => { const canvas = Object . assign ( document . createElement ( 'canvas' ) , { width , height , } ) ; canvas . getContext ( '2d' ) ?. fillRect ( 0 , 0 , width , height ) ; const stream = ( canvas as any ) . captureStream ( ) as MediaStream ; return Object . assign ( stream . getVideoTracks ( ) [ 0 ] , { enabled : false } ) ; } ; export const silence = ( ) => { const ctx = new AudioContext ( ) ; const oscillator = ctx . createOscillator ( ) ; const dst = oscillator . connect ( ctx . createMediaStreamDestination ( ) ) ; oscillator . start ( ) ; const stream = ( dst as any ) . stream as MediaStream ; const track = Object . assign ( stream . getAudioTracks ( ) [ 0 ] , { enabled : false } ) ; track . stop ( ) ; return track ; } ; export const shuffleArray = < T > ( array_ ) => { const array = [ ... array_ ] ; let currentIndex = array . length ; let randomIndex ; while ( currentIndex !== 0 ) { randomIndex = Math . floor ( Math . random ( ) * currentIndex ) ; currentIndex -- ; [ array [ currentIndex ] , array [ randomIndex ] ] = [ array [ randomIndex ] , array [ currentIndex ] , ] ; } return array ; } ;