UNPKG

@ktt45678/vidstack

Version:

UI component library for building high-quality, accessible video and audio experiences on the web.

2 lines (1 loc) 7.98 kB
import{l as u,e as b,d as E,ae as T,Z as l,o as C,D as m,p as o}from"../chunks/vidstack-DgnIG4vb.js";import{T as n}from"../chunks/vidstack-ERkdI6Km.js";import{R as v}from"../chunks/vidstack-C9ZutcHE.js";import{L as k}from"../chunks/vidstack-ClIUVQwo.js";import{g as f,a as y,b as A,h as g,l as S,c as j}from"../chunks/vidstack-ERagmYfS.js";import"../chunks/vidstack-nZNas-dv.js";class w{constructor(t){this.$a=new chrome.cast.media.MediaInfo(t.src,t.type)}build(){return this.$a}lj(t){return t.includes("live")?this.$a.streamType=chrome.cast.media.StreamType.LIVE:this.$a.streamType=chrome.cast.media.StreamType.BUFFERED,this}mj(t){return this.$a.tracks=t.map(this.nj),this}oj(t,s){return this.$a.metadata=new chrome.cast.media.GenericMediaMetadata,this.$a.metadata.title=t,this.$a.metadata.images=[{url:s}],this}nj(t,s){const e=new chrome.cast.media.Track(s,chrome.cast.media.TrackType.TEXT);return e.name=t.label,e.trackContentId=t.src,e.trackContentType="text/vtt",e.language=t.language,e.subtype=t.kind.toUpperCase(),e}}const c=chrome.cast.media.TrackType.TEXT,p=chrome.cast.media.TrackType.AUDIO;class L{constructor(t,s,e){this.od=t,this.b=s,this.Ae=e}he(){const t=this.ug.bind(this);u(this.b.audioTracks,"change",t),u(this.b.textTracks,"mode-change",t),b(this.pj.bind(this))}nd(){return this.b.$state.textTracks().filter(t=>t.src&&t.type==="vtt")}vg(){return this.b.$state.audioTracks()}xc(t){const s=this.od.mediaInfo?.tracks??[];return t?s.filter(e=>e.type===t):s}qj(){const t=[],s=this.vg().find(i=>i.selected),e=this.nd().filter(i=>i.mode==="showing");if(s){const i=this.xc(p),a=this.ze(i,s);a&&t.push(a.trackId)}if(e?.length){const i=this.xc(c);if(i.length)for(const a of e){const r=this.ze(i,a);r&&t.push(r.trackId)}}return t}pj(){const t=this.nd();if(!this.od.isMediaLoaded)return;const s=this.xc(c);for(const e of t)if(!this.ze(s,e)){E(()=>this.Ae?.());break}}rj(t){if(!this.od.isMediaLoaded)return;const s=this.vg(),e=this.nd(),i=this.xc(p),a=this.xc(c);for(const r of i){if(this.wg(s,r))continue;const h={id:r.trackId.toString(),label:r.name,language:r.language,kind:r.subtype??"main",selected:!1};this.b.audioTracks[k.da](h,t)}for(const r of a){if(this.wg(e,r))continue;const h={id:r.trackId.toString(),src:r.trackContentId,label:r.name,language:r.language,kind:r.subtype.toLowerCase()};this.b.textTracks.add(h,t)}}ug(t){if(!this.od.isMediaLoaded)return;const s=this.qj(),e=new chrome.cast.media.EditTracksInfoRequest(s);this.sj(e).catch(i=>{})}sj(t){const s=f();return new Promise((e,i)=>s?.editTracksInfo(t,e,i))}wg(t,s){return t.find(e=>this.xg(e,s))}ze(t,s){return t.find(e=>this.xg(s,e))}xg(t,s){return s.name===t.label&&s.language===t.language&&s.subtype.toLowerCase()===t.kind.toLowerCase()}}class D{constructor(t,s){this.f=t,this.b=s,this.$$PROVIDER_TYPE="GOOGLE_CAST",this.scope=T(),this.K=null,this.za="disconnected",this.ua=0,this.ga=0,this.Aa=new n(0,0),this.fa=new v(this.kc.bind(this)),this.Pa=null,this.Be=!1,this.va=new L(this.f,this.b,this.Ae.bind(this))}get c(){return this.b.delegate.c}get type(){return"google-cast"}get currentSrc(){return this.K}get player(){return this.f}get cast(){return y()}get session(){return A()}get media(){return f()}get hasActiveSession(){return g(this.K)}setup(){this.tj(),this.uj(),this.va.he(),this.c("provider-setup",this)}tj(){S(cast.framework.CastContextEventType.CAST_STATE_CHANGED,this.zg.bind(this))}uj(){const t=cast.framework.RemotePlayerEventType,s={[t.IS_CONNECTED_CHANGED]:this.zg,[t.IS_MEDIA_LOADED_CHANGED]:this.Ag,[t.CAN_CONTROL_VOLUME_CHANGED]:this.Bg,[t.CAN_SEEK_CHANGED]:this.Cg,[t.DURATION_CHANGED]:this.de,[t.IS_MUTED_CHANGED]:this.Na,[t.VOLUME_LEVEL_CHANGED]:this.Na,[t.IS_PAUSED_CHANGED]:this.vj,[t.LIVE_SEEKABLE_RANGE_CHANGED]:this.nb,[t.PLAYER_STATE_CHANGED]:this.wj};this.yg=s;const e=this.xj.bind(this);for(const i of l(s))this.f.controller.addEventListener(i,e);C(()=>{for(const i of l(s))this.f.controller.removeEventListener(i,e)})}async play(){if(!(!this.f.isPaused&&!this.Be)){if(this.Be){await this.Dg(!1,0);return}this.f.controller?.playOrPause()}}async pause(){this.f.isPaused||this.f.controller?.playOrPause()}getMediaStatus(t){return new Promise((s,e)=>{this.media?.getStatus(t,s,e)})}setMuted(t){(t&&!this.f.isMuted||!t&&this.f.isMuted)&&this.f.controller?.muteOrUnmute()}setCurrentTime(t){this.f.currentTime=t,this.c("seeking",t),this.f.controller?.seek()}setVolume(t){this.f.volumeLevel=t,this.f.controller?.setVolumeLevel()}async loadSource(t){if(this.Pa?.src!==t&&(this.Pa=null),g(t)){this.yj(),this.K=t;return}this.c("load-start");const s=this.zj(t),e=await this.session.loadMedia(s);if(e){this.K=null,this.c("error",Error(j(e)));return}this.K=t}destroy(){this.z(),this.Eg()}z(){this.Pa||(this.ga=0,this.Aa=new n(0,0)),this.fa.$(),this.ua=0,this.Pa=null}yj(){const t=new m("resume-session",{detail:this.session});this.Ag(t);const{muted:s,volume:e,savedState:i}=this.b.$state,a=i();this.setCurrentTime(Math.max(this.f.currentTime,a?.currentTime??0)),this.setMuted(s()),this.setVolume(e()),a?.paused===!1&&this.play()}Eg(){this.cast.endCurrentSession(!0);const{remotePlaybackLoader:t}=this.b.$state;t.set(null)}Aj(){const{savedState:t}=this.b.$state;t.set({paused:this.f.isPaused,currentTime:this.f.currentTime}),this.Eg()}kc(){this.Bj()}xj(t){this.yg[t.type].call(this,t)}zg(t){const s=this.cast.getCastState(),e=s===cast.framework.CastState.CONNECTED?"connected":s===cast.framework.CastState.CONNECTING?"connecting":"disconnected";if(this.za===e)return;const i={type:"google-cast",state:e},a=this.ab(t);this.za=e,this.c("remote-playback-change",i,a),e==="disconnected"&&this.Aj()}Ag(t){if(!this.f.isMediaLoaded)return;const s=o(this.b.$state.source);Promise.resolve().then(()=>{if(s!==o(this.b.$state.source)||!this.f.isMediaLoaded)return;this.z();const e=this.f.duration;this.Aa=new n(0,e);const i={provider:this,duration:e,buffered:new n(0,0),seekable:this.Fg()},a=this.ab(t);this.c("loaded-metadata",void 0,a),this.c("loaded-data",void 0,a),this.c("can-play",i,a),this.Bg(),this.Cg(t);const{volume:r,muted:h}=this.b.$state;this.setVolume(r()),this.setMuted(h()),this.fa.Xa(),this.va.rj(a),this.va.ug(a)})}Bg(){this.b.$state.canSetVolume.set(this.f.canControlVolume)}Cg(t){const s=this.ab(t);this.c("stream-type-change",this.Cj(),s)}Cj(){return this.f.mediaInfo?.streamType===chrome.cast.media.StreamType.LIVE?this.f.canSeek?"live:dvr":"live":"on-demand"}Bj(){if(this.Pa)return;const t=this.f.currentTime;t!==this.ua&&(this.c("time-change",t),t>this.ga&&(this.ga=t,this.nb()),this.b.$state.seeking()&&this.c("seeked",t),this.ua=t)}de(t){if(!this.f.isMediaLoaded||this.Pa)return;const s=this.f.duration,e=this.ab(t);this.Aa=new n(0,s),this.c("duration-change",s,e)}Na(t){if(!this.f.isMediaLoaded)return;const s={muted:this.f.isMuted,volume:this.f.volumeLevel},e=this.ab(t);this.c("volume-change",s,e)}vj(t){const s=this.ab(t);this.f.isPaused?this.c("pause",void 0,s):this.c("play",void 0,s)}nb(t){const s={seekable:this.Fg(),buffered:new n(0,this.ga)},e=t?this.ab(t):void 0;this.c("progress",s,e)}wj(t){const s=this.f.playerState,e=chrome.cast.media.PlayerState;if(this.Be=s===e.IDLE,s===e.PAUSED)return;const i=this.ab(t);switch(s){case e.PLAYING:this.c("playing",void 0,i);break;case e.BUFFERING:this.c("waiting",void 0,i);break;case e.IDLE:this.fa.$(),this.c("pause"),this.c("end");break}}Fg(){return this.f.liveSeekableRange?new n(this.f.liveSeekableRange.start,this.f.liveSeekableRange.end):this.Aa}ab(t){return t instanceof Event?t:new m(t.type,{detail:t})}Dj(t){const{streamType:s,title:e,poster:i}=this.b.$state;return new w(t).oj(e(),i()).lj(s()).mj(this.va.nd()).build()}zj(t){const s=this.Dj(t),e=new chrome.cast.media.LoadRequest(s),i=this.b.$state.savedState();return e.autoplay=(this.Pa?.paused??i?.paused)===!1,e.currentTime=this.Pa?.time??i?.currentTime??0,e}async Dg(t,s){const e=o(this.b.$state.source);this.Pa={src:e,paused:t,time:s},await this.loadSource(e)}Ae(){this.Dg(this.f.isPaused,this.f.currentTime).catch(t=>{})}}export{D as GoogleCastProvider};