import{ag as a}from"./index-DWRYsVdq.js";import{B as h}from"./base-player-BWJBhwy4.js";import{H as d,M as l}from"./http-client-BKzSvxm0.js";class c extends h{constructor(e={}){super("VODPlayer",e),this.mediaSource=null,this.httpClient=new d,this.segments=[],this.totalDuration=0,this.loadingSegments=new Set,this.destroyed=!1,this.minBufferLength=e.minBufferLength??30}async load(e){this.setState("loading"),this._error=null;try{let t;try{t=new URL(e)}catch{t=new URL(e,window.location.href)}t.pathname.split(".").pop()?.toLowerCase()==="m3u8"?await this.loadM3U8(t):await this.loadDirectVideo(e),this.logger.info("Loaded",{url:e,segments:this.segments.length,duration:this.totalDuration})}catch(t){throw this._error=t.message,this.setState("error"),this.emit("error",t),t}}async loadM3U8(e){const t=await this.httpClient.get(e.toString()),i=e.origin+e.pathname.split("/").slice(0,-1).join("/");this.parseM3U8(t,i),this.video&&(await this.initMediaSource(),this.options.autoplay?await this.play():await this.loadSegment(0))}async loadDirectVideo(e){this.video&&(this.video.src=e,await this.video.load(),this.options.autoplay&&await this.play())}parseM3U8(e,t){const i=e.split(`
`).map(s=>s.trim()).filter(s=>s);this.segments=[],this.totalDuration=0;let r=0;for(let s=0;s<i.length;s++){const o=i[s];if(o.startsWith("#EXTINF:")){const n=o.match(/#EXTINF:([\d.]+)/);n&&(r=parseFloat(n[1]))}else if(!o.startsWith("#")&&o.length>0){const n=o.startsWith("http")?o:`${t}/${o}`;this.segments.push({index:this.segments.length,url:n,duration:r,startTime:this.totalDuration,endTime:this.totalDuration+r,loaded:!1}),this.totalDuration+=r,r=0}}}async initMediaSource(){this.video&&(this.mediaSource=new l,this.mediaSource.attachToVideo(this.video),await this.mediaSource.ready(),this.logger.debug("MediaSource initialized"))}async loadSegment(e){if(this.destroyed||e<0||e>=this.segments.length||this.loadingSegments.has(e))return;const t=this.segments[e];if(!t.loaded){this.loadingSegments.add(e);try{const i=await this.httpClient.getArrayBuffer(t.url);if(this.destroyed)return;if(this.mediaSource){let s=this.mediaSource.getSourceBuffer("video");if(!s){const o=this.detectMimeType(new Uint8Array(i));s=this.mediaSource.addSourceBuffer("video",{mimeType:o})}await s.appendBuffer(i)}t.loaded=!0,this.emit("segment-loaded",{index:e,duration:t.duration});const r=this.segments.filter(s=>s.loaded).length;this.emit("progress",{loaded:r,total:this.segments.length,percent:r/this.segments.length*100}),this.logger.debug("Segment loaded",{index:e,duration:t.duration})}catch(i){throw this.logger.error("Failed to load segment",{index:e,error:i}),i}finally{this.loadingSegments.delete(e)}}}detectMimeType(e){return e[4]===102&&e[5]===116&&e[6]===121&&e[7]===112?'video/mp4; codecs="avc1.64001f,mp4a.40.2"':"video/mp4"}seek(e){super.seek(e);const t=this.segments.findIndex(i=>i.startTime<=e&&i.endTime>e);t>=0&&this.loadSegmentsFrom(t)}async loadSegmentsFrom(e){for(let t=e;t<this.segments.length&&!this.destroyed;t++){if(this.getBufferedLength()>=this.minBufferLength){await new Promise(r=>{a.instance.setTimeout(()=>r(),1e3)});continue}await this.loadSegment(t)}}getBufferedLength(){return!this.video||!this.video.buffered.length?0:this.video.buffered.end(this.video.buffered.length-1)-this.video.currentTime}getSegments(){return[...this.segments]}getTotalDuration(){return this.totalDuration}setMinBufferLength(e){this.minBufferLength=Math.max(0,e)}dispose(){this.destroyed=!0,this.httpClient.dispose(),this.mediaSource?.dispose(),this.mediaSource=null,this.segments=[],super.dispose()}}export{c as V};
