2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
|
import f from"events";import h from"../utils/logger.js";import m from"../utils/browser.js";import n from"./player-events.js";import c from"../core/transmuxer.js";import a from"../core/transmuxing-events.js";import E from"../core/mse-controller.js";import o from"../core/mse-events.js";import{ErrorTypes as _,ErrorDetails as g}from"./player-errors.js";import{createDefaultConfig as p}from"../config.js";import{InvalidArgumentException as T,IllegalStateException as u}from"../utils/exception.js";class v{constructor(e,t){if(this.TAG="FlvPlayer",this._type="FlvPlayer",this._emitter=new f,this._config=p(),typeof t=="object"&&Object.assign(this._config,t),e.type.toLowerCase()!=="flv")throw new T("FlvPlayer requires an flv MediaDataSource input!");e.isLive===!0&&(this._config.isLive=!0),this.e={onvLoadedMetadata:this._onvLoadedMetadata.bind(this),onvSeeking:this._onvSeeking.bind(this),onvCanPlay:this._onvCanPlay.bind(this),onvStalled:this._onvStalled.bind(this),onvProgress:this._onvProgress.bind(this)},self.performance&&self.performance.now?this._now=self.performance.now.bind(self.performance):this._now=Date.now,this._pendingSeekTime=null,this._requestSetTime=!1,this._seekpointRecord=null,this._progressChecker=null,this._mediaDataSource=e,this._mediaElement=null,this._msectl=null,this._transmuxer=null,this._mseSourceOpened=!1,this._hasPendingLoad=!1,this._receivedCanPlay=!1,this._mediaInfo=null,this._statisticsInfo=null;let i=m.chrome&&(m.version.major<50||m.version.major===50&&m.version.build<2661);this._alwaysSeekKeyframe=!!(i||m.msedge||m.msie),this._alwaysSeekKeyframe&&(this._config.accurateSeek=!1)}destroy(){this._progressChecker!=null&&(window.clearInterval(this._progressChecker),this._progressChecker=null),this._transmuxer&&this.unload(),this._mediaElement&&this.detachMediaElement(),this.e=null,this._mediaDataSource=null,this._emitter.removeAllListeners(),this._emitter=null}on(e,t){e===n.MEDIA_INFO?this._mediaInfo!=null&&Promise.resolve().then(()=>{this._emitter.emit(n.MEDIA_INFO,this.mediaInfo)}):e===n.STATISTICS_INFO&&this._statisticsInfo!=null&&Promise.resolve().then(()=>{this._emitter.emit(n.STATISTICS_INFO,this.statisticsInfo)}),this._emitter.addListener(e,t)}off(e,t){this._emitter.removeListener(e,t)}attachMediaElement(e){if(this._mediaElement=e,e.addEventListener("loadedmetadata",this.e.onvLoadedMetadata),e.addEventListener("seeking",this.e.onvSeeking),e.addEventListener("canplay",this.e.onvCanPlay),e.addEventListener("stalled",this.e.onvStalled),e.addEventListener("progress",this.e.onvProgress),this._msectl=new E(this._config),this._msectl.on(o.UPDATE_END,this._onmseUpdateEnd.bind(this)),this._msectl.on(o.BUFFER_FULL,this._onmseBufferFull.bind(this)),this._msectl.on(o.SOURCE_OPEN,()=>{this._mseSourceOpened=!0,this._hasPendingLoad&&(this._hasPendingLoad=!1,this.load())}),this._msectl.on(o.ERROR,t=>{this._emitter.emit(n.ERROR,_.MEDIA_ERROR,g.MEDIA_MSE_ERROR,t)}),this._msectl.attachMediaElement(e),this._pendingSeekTime!=null)try{e.currentTime=this._pendingSeekTime,this._pendingSeekTime=null}catch(t){}}detachMediaElement(){this._mediaElement&&(this._msectl.detachMediaElement(),this._mediaElement.removeEventListener("loadedmetadata",this.e.onvLoadedMetadata),this._mediaElement.removeEventListener("seeking",this.e.onvSeeking),this._mediaElement.removeEventListener("canplay",this.e.onvCanPlay),this._mediaElement.removeEventListener("stalled",this.e.onvStalled),this._mediaElement.removeEventListener("progress",this.e.onvProgress),this._mediaElement=null),this._msectl&&(this._msectl.destroy(),this._msectl=null)}load(){if(!this._mediaElement)throw new u("HTMLMediaElement must be attached before load()!");if(this._transmuxer)throw new u("FlvPlayer.load() has been called, please call unload() first!");if(!this._hasPendingLoad){if(this._config.deferLoadAfterSourceOpen&&this._mseSourceOpened===!1){this._hasPendingLoad=!0;return}this._mediaElement.readyState>0&&(this._requestSetTime=!0,this._mediaElement.currentTime=0),this._transmuxer=new c(this._mediaDataSource,this._config),this._transmuxer.on(a.INIT_SEGMENT,(e,t)=>{this._msectl
|