import AudioVideoController from '../audiovideocontroller/AudioVideoController' ; import DefaultBrowserBehavior from '../browserbehavior/DefaultBrowserBehavior' ; import DefaultDeviceController from '../devicecontroller/DefaultDeviceController' ; import Logger from '../logger/Logger' ; import MediaStreamBroker from '../mediastreambroker/MediaStreamBroker' ; export default class ContentShareMediaStreamBroker implements MediaStreamBroker { private static defaultFrameRate = 15 ; private _mediaStream : MediaStream ; constructor ( private logger ) { } get mediaStream ( ) : MediaStream { return this . _mediaStream ; } set mediaStream ( mediaStream ) { this . _mediaStream = mediaStream ; } async acquireAudioInputStream ( ) { if ( this . _mediaStream . getAudioTracks ( ) . length === 0 ) { this . logger . info ( 'No audio stream available. Synthesizing an audio stream.' ) ; return DefaultDeviceController . synthesizeAudioDevice ( 0 ) as MediaStream ; } return this . _mediaStream ; } async acquireVideoInputStream ( ) { return this . _mediaStream ; } releaseMediaStream ( _mediaStreamToRelease ) { this . logger . warn ( 'release media stream called' ) ; return ; } async acquireDisplayInputStream ( streamConstraints ) { if ( streamConstraints && streamConstraints . video && streamConstraints . video . mandatory && streamConstraints . video . mandatory . chromeMediaSource && streamConstraints . video . mandatory . chromeMediaSourceId ) { return navigator . mediaDevices . getUserMedia ( streamConstraints ) ; } return navigator . mediaDevices . getDisplayMedia ( streamConstraints ) ; } bindToAudioVideoController ( _audioVideoController ) { throw new Error ( 'unsupported' ) ; } async acquireScreenCaptureDisplayInputStream ( sourceId ? , frameRate ? ) { return this . acquireDisplayInputStream ( this . screenCaptureDisplayMediaConstraints ( sourceId , frameRate ) ) ; } private screenCaptureDisplayMediaConstraints ( sourceId ? , frameRate ? ) { return { audio : ! sourceId && new DefaultBrowserBehavior ( ) . getDisplayMediaAudioCaptureSupport ( ) ? true : false , video : { ... ( ! sourceId && { frameRate : { max : frameRate ? frameRate : ContentShareMediaStreamBroker . defaultFrameRate , } , } ) , ... ( sourceId && { mandatory : { chromeMediaSource : 'desktop' , chromeMediaSourceId : sourceId , maxFrameRate : frameRate ? frameRate : ContentShareMediaStreamBroker . defaultFrameRate , } , } ) , } , } ; } toggleMediaStream ( enable ) { let changed = false ; if ( this . mediaStream ) { for ( let i = 0 ; i < this . mediaStream . getTracks ( ) . length ; i ++ ) { if ( this . mediaStream . getTracks ( ) [ i ] . enabled !== enable ) { this . mediaStream . getTracks ( ) [ i ] . enabled = enable ; changed = true ; } } } return changed ; } cleanup ( ) { if ( this . mediaStream ) { for ( let i = 0 ; i < this . mediaStream . getTracks ( ) . length ; i ++ ) { const track = this . mediaStream . getTracks ( ) [ i ] ; track . stop ( ) ; } } this . mediaStream = null ; } }