@aidenlx/player
Version:
Headless web components that make integrating media on the a web a breeze.
2 lines (1 loc) • 5.75 kB
JavaScript
import{a as m}from"./chunk.RJQHBSMR.js";import{a as E}from"./chunk.KCC3AAG2.js";import{$ as _,C as c,_ as H,a as u,ca as b,l as y,m as n,t as r,u as h,w as p,x as v}from"./chunk.PWTX2M5C.js";var a=new Map;function C(i){return a.has(i)}async function D(i,e={}){if(r(i))return;if(e.onLoadStart?.(),!v(i))return e.onLoaded?.(i),i;let t=String(i);if(a.has(t)){let s=a.get(t);return e.onLoaded?.(s),s}try{let s=(await i())?.default;if(s&&!!s.isSupported)e.onLoaded?.(s),a.set(t,s);else throw Error("");return s}catch(s){e.onLoadError?.(s)}}async function V(i,e={}){if(!!p(i)){if(a.has(i))return a.get(i);e.onLoadStart?.();try{if(await _.load(i),!v(window.Hls))throw Error("");let t=window.Hls;return e.onLoaded?.(t),a.set(i,t),t}catch(t){e.onLoadError?.(t)}}}function g(i){return b(i.replace("vds-",""))}var S="vds-hls-",w=["lib-load","instance","unsupported"];function L(i){return i.startsWith(S)&&!w.some(e=>i.startsWith(`${S}${e}`))}var N=/\.(m3u8)($|\?)/i,j=new Set(["application/x-mpegURL","application/vnd.apple.mpegurl"]),T="https://cdn.jsdelivr.net/npm/hls.js@^1.0.0/dist/hls.light",G=`${T}.js`,x=`${T}.min.js`,f=class extends m{constructor(){super();this.it=!1;this.hlsConfig={};this.hlsLibrary=x;this.Ae="";this.qe=[];Object.defineProperty(this,"hls-config",{set:e=>{this.hlsConfig=e}}),Object.defineProperty(this,"hls-library",{set:e=>{this.hlsLibrary=e}})}get Hls(){return this.st}get hlsEngine(){return this.Yt}get isHlsEngineAttached(){return this.it}get currentSrc(){return this.isHlsStream&&!this.shouldUseNativeHlsSupport?this.src:this.videoEngine?.currentSrc??""}connectedCallback(){super.connectedCallback(),this.Si()}async update(e){super.update(e),e.has("hlsLibrary")&&!this.shouldUseNativeHlsSupport&&c()&&this.Si(),e.has("hlsLibrary")&&this.hasUpdated&&this.canLoad&&!this.shouldUseNativeHlsSupport&&c()&&(await this.wi(!0),this.Ci(),this.xi())}disconnectedCallback(){this.Zt(),super.disconnectedCallback()}async handleMediaCanLoad(){await super.handleMediaCanLoad(),this.De||window.requestAnimationFrame(()=>{this.J()}),this.shouldUseNativeHlsSupport&&this.requestUpdate()}canPlayType(e){return j.has(e)&&(this.isHlsSupported?"probably":""),super.canPlayType(e)}get isHlsSupported(){return(this.Hls?.isSupported()??c())||this.hasNativeHlsSupport}get isHlsStream(){return N.test(this.src)}get hasNativeHlsSupport(){let e=super.canPlayType("application/vnd.apple.mpegurl");return e==="maybe"||e==="probably"}get shouldUseNativeHlsSupport(){return c()?!1:this.hasNativeHlsSupport}Xt(){return this.canLoad&&(this.shouldUseNativeHlsSupport||!this.isHlsStream)}Si(){this.canLoad||!p(this.hlsLibrary)||C(this.hlsLibrary)||H(this.hlsLibrary)}async wi(e=!1){if(h(this.videoEngine)&&!e&&!r(this.hlsEngine))return;r(this.hlsEngine)||this.Zt();let t={onLoadStart:()=>{this.dispatchEvent(n("vds-hls-lib-load-start"))},onLoaded:s=>{this.dispatchEvent(n("vds-hls-lib-loaded",{detail:s}))},onLoadError:s=>{this.dispatchEvent(n("vds-hls-lib-load-error",{detail:s})),this.dispatchEvent(n("vds-error",{detail:{message:s.message,code:4}}))}};if(this.st=await V(this.hlsLibrary,t),r(this.st)&&!p(this.hlsLibrary)&&(this.st=await D(this.hlsLibrary,t)),!!this.Hls){if(!this.Hls?.isSupported?.()){let s="[vds]: `hls.js` is not supported in this environment";this.dispatchEvent(n("vds-hls-unsupported")),this.dispatchEvent(n("vds-error",{detail:{message:s,code:4}}));return}this.Yt=new this.Hls(this.hlsConfig),this.dispatchEvent(n("vds-hls-instance",{detail:this.hlsEngine})),this.en()}}Zt(){this.hlsEngine?.destroy(),this.Ae="",this.Yt=void 0,this.it=!1}Qt(){return this.isHlsStream&&!this.shouldUseNativeHlsSupport}Ci(){this.isHlsEngineAttached||r(this.hlsEngine)||h(this.videoEngine)||(this.hlsEngine.attachMedia(this.videoEngine),this.it=!0)}tn(){!this.isHlsEngineAttached||(this.hlsEngine?.detachMedia(),this.it=!1,this.Ae="")}xi(){h(this.hlsEngine)||!this.isHlsStream||this.shouldUseNativeHlsSupport||this.src===this.Ae||(this.hlsEngine.loadSource(this.src),this.Ae=this.src)}Ve(){return this.mediaType==="live-video"?"live-video":this.isHlsStream?"video":super.Ve()}get yi(){return!0}async J(){if(this.Ae!==this.src&&(await super.J(),!(!this.hasUpdated||!this.canLoad))){if(!this.isHlsStream){this.tn();return}await this.updateComplete,!(h(this.hlsLibrary)||this.shouldUseNativeHlsSupport)&&(r(this.hlsEngine)&&await this.wi(),this.Ci(),this.xi())}}en(){r(this.hlsEngine)||r(this.Hls)||(this.hlsEngine.on(this.Hls.Events.LEVEL_LOADED,this.rn.bind(this)),this.qe.forEach(({type:e,listener:t,options:s})=>{this.hlsEngine?.[s?.once?"once":"on"](e,t,s?.context)}),this.hlsEngine.on(this.Hls.Events.ERROR,this.sn.bind(this)))}sn(e,t){if(!r(this.Hls)&&t.fatal)switch(t.type){case"networkError":this.nn();break;case"mediaError":this.an();break;default:this.dn();break}}nn(){this.hlsEngine?.startLoad()}an(){this.hlsEngine?.recoverMediaError()}dn(){this.Zt()}rn(e,t){this.canPlay||this.ln(e,t)}ln(e,t){let{live:s,totalduration:o}=t.details,d=new y(e,{detail:t}),l=s?"live-video":"video";this.mediaState.mediaType!==l&&this.dispatchEvent(n("vds-media-type-change",{detail:l,triggerEvent:d})),this.duration!==o&&this.dispatchEvent(n("vds-duration-change",{detail:o,triggerEvent:d}))}addEventListener(e,t,s){if(L(e)){let o=g(e);this.qe.some(l=>l.type===o&&l.listener===t)||(this.qe.push({type:o,listener:t,options:s}),this.hlsEngine?.[s?.once?"once":"on"](o,t,s?.context));return}return super.addEventListener(e,t,s)}removeEventListener(e,t,s){if(L(e)){let o=g(e);this.qe=this.qe.filter(d=>d.type===o&&d.listener===t),this.hlsEngine?.off(o,t,s?.context,s?.once);return}return super.removeEventListener(e,t,s)}};u([E({type:Object,attribute:"hls-config"})],f.prototype,"hlsConfig",2),u([E({attribute:"hls-library"})],f.prototype,"hlsLibrary",2);export{f as a};