define(["exports","../utils/events.js","../utils/logger.js","../../browser.js","./mse-events.js","./media-segment-info.js","../utils/exception.js"],function(_exports,_events,_logger,_browser,_mseEvents,_mediaSegmentInfo,_exception){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0;var MSEController=function(){function MSEController(config){babelHelpers.classCallCheck(this,MSEController),this.TAG="MSEController",this._config=config,this._emitter=new _events.default,this._config.isLive&&!this._config.autoCleanupSourceBuffer&&(this._config.autoCleanupSourceBuffer=!0),this.e={onSourceOpen:this._onSourceOpen.bind(this),onSourceEnded:this._onSourceEnded.bind(this),onSourceClose:this._onSourceClose.bind(this),onSourceBufferError:this._onSourceBufferError.bind(this),onSourceBufferUpdateEnd:this._onSourceBufferUpdateEnd.bind(this)},this._mediaSource=null,this._mediaSourceObjectURL=null,this._mediaElement=null,this._isBufferFull=!1,this._hasPendingEos=!1,this._requireSetMediaDuration=!1,this._pendingMediaDuration=0,this._pendingSourceBufferInit=[],this._mimeTypes={video:null,audio:null},this._sourceBuffers={video:null,audio:null},this._lastInitSegments={video:null,audio:null},this._pendingSegments={video:[],audio:[]},this._pendingRemoveRanges={video:[],audio:[]},this._idrList=new _mediaSegmentInfo.IDRSampleList}return babelHelpers.createClass(MSEController,[{key:"destroy",value:function(){(this._mediaElement||this._mediaSource)&&this.detachMediaElement(),this.e=null,this._emitter.removeAllListeners(),this._emitter=null}},{key:"on",value:function(event,listener){this._emitter.addListener(event,listener)}},{key:"off",value:function(event,listener){this._emitter.removeListener(event,listener)}},{key:"attachMediaElement",value:function(mediaElement){if(this._mediaSource)throw new _exception.IllegalStateException("MediaSource has been attached to an HTMLMediaElement!");var ms=this._mediaSource=new window.MediaSource;ms.addEventListener("sourceopen",this.e.onSourceOpen),ms.addEventListener("sourceended",this.e.onSourceEnded),ms.addEventListener("sourceclose",this.e.onSourceClose),this._mediaElement=mediaElement,this._mediaSourceObjectURL=window.URL.createObjectURL(this._mediaSource),mediaElement.src=this._mediaSourceObjectURL}},{key:"detachMediaElement",value:function(){if(this._mediaSource){var type,ms=this._mediaSource;for(type in this._sourceBuffers){var ps=this._pendingSegments[type],ps=(ps.splice(0,ps.length),this._pendingSegments[type]=null,this._pendingRemoveRanges[type]=null,this._lastInitSegments[type]=null,this._sourceBuffers[type]);if(ps){if("closed"!==ms.readyState){try{ms.removeSourceBuffer(ps)}catch(error){_logger.default.e(this.TAG,error.message)}ps.removeEventListener("error",this.e.onSourceBufferError),ps.removeEventListener("updateend",this.e.onSourceBufferUpdateEnd)}this._mimeTypes[type]=null,this._sourceBuffers[type]=null}}if("open"===ms.readyState)try{ms.endOfStream()}catch(error){_logger.default.e(this.TAG,error.message)}ms.removeEventListener("sourceopen",this.e.onSourceOpen),ms.removeEventListener("sourceended",this.e.onSourceEnded),ms.removeEventListener("sourceclose",this.e.onSourceClose),this._pendingSourceBufferInit=[],this._isBufferFull=!1,this._idrList.clear(),this._mediaSource=null}this._mediaElement&&(this._mediaElement.src="",this._mediaElement.removeAttribute("src"),this._mediaElement=null),this._mediaSourceObjectURL&&(window.URL.revokeObjectURL(this._mediaSourceObjectURL),this._mediaSourceObjectURL=null)}},{key:"appendInitSegment",value:function(initSegment,deferred){if(this._mediaSource&&"open"===this._mediaSource.readyState){var is=initSegment,mimeType="".concat(is.container),firstInitSegment=(is.codec&&0=this._config.autoCleanupMaxBackwardDuration)return!0}}}return!1}},{key:"_doCleanupSourceBuffer",value:function(){var type,currentTime=this._mediaElement.currentTime;for(type in this._sourceBuffers){var sb=this._sourceBuffers[type];if(sb){for(var buffered=sb.buffered,doRemove=!1,i=0;i=this._config.autoCleanupMaxBackwardDuration&&(doRemove=!0,removeEnd=currentTime-this._config.autoCleanupMinBackwardDuration,this._pendingRemoveRanges[type].push({start:start,end:removeEnd})):end