import AudioVideoControllerState from '../audiovideocontroller/AudioVideoControllerState' ; import SignalingClient from '../signalingclient/SignalingClient' ; import SignalingClientEvent from '../signalingclient/SignalingClientEvent' ; import SignalingClientEventType from '../signalingclient/SignalingClientEventType' ; import SignalingClientObserver from '../signalingclientobserver/SignalingClientObserver' ; import TaskCanceler from '../taskcanceler/TaskCanceler' ; import BaseTask from './BaseTask' ; export default class CleanStoppedSessionTask extends BaseTask { protected taskName = 'CleanStoppedSessionTask' ; private taskCanceler : TaskCanceler | null = null ; constructor ( private context ) { super ( context . logger ) ; } cancel ( ) { if ( this . taskCanceler ) { this . taskCanceler . cancel ( ) ; this . taskCanceler = null ; } } async run ( ) { try { if ( this . context . signalingClient . ready ( ) ) { this . context . signalingClient . closeConnection ( ) ; await this . receiveWebSocketClosedEvent ( ) ; } } catch ( error ) { throw error ; } finally { for ( const observer of this . context . removableObservers ) { observer . removeObserver ( ) ; } this . context . statsCollector . stop ( ) ; this . context . statsCollector = null ; this . context . connectionMonitor . stop ( ) ; this . context . connectionMonitor = null ; if ( this . context . peer ) { this . context . peer . close ( ) ; } this . context . peer = null ; this . context . localVideoSender = null ; this . context . sdpAnswer = null ; this . context . sdpOfferInit = null ; this . context . indexFrame = null ; this . context . videoDownlinkBandwidthPolicy . reset ( ) ; this . context . iceCandidateHandler = null ; this . context . iceCandidates = [ ] ; this . context . turnCredentials = null ; this . context . videoSubscriptions = null ; this . context . transceiverController . reset ( ) ; if ( this . context . videoUplinkBandwidthPolicy . setTransceiverController ) { this . context . videoUplinkBandwidthPolicy . setTransceiverController ( undefined ) ; } if ( this . context . videoDownlinkBandwidthPolicy . bindToTileController ) { this . context . videoDownlinkBandwidthPolicy . bindToTileController ( undefined ) ; } const tile = this . context . videoTileController . getLocalVideoTile ( ) ; if ( tile ) { tile . bindVideoStream ( '' , true , null , null , null , null ) ; } this . context . videoTileController . removeAllVideoTiles ( ) ; } } private receiveWebSocketClosedEvent ( ) { return new Promise ( ( resolve , reject ) => { class Interceptor implements SignalingClientObserver , TaskCanceler { constructor ( private signalingClient ) { } cancel ( ) { this . signalingClient . removeObserver ( this ) ; reject ( new Error ( `CleanStoppedSessionTask got canceled while waiting for the WebSocket closed event` ) ) ; } handleSignalingClientEvent ( event ) { if ( event . type === SignalingClientEventType . WebSocketClosed ) { this . signalingClient . removeObserver ( this ) ; resolve ( ) ; } } } const interceptor = new Interceptor ( this . context . signalingClient ) ; this . taskCanceler = interceptor ; this . context . signalingClient . registerObserver ( interceptor ) ; } ) ; } }