UNPKG

@oplayer/plugins

Version:
9 lines (8 loc) 3.84 kB
/** * name: @oplayer/chromecast * version: v1.0.15-beta.3 * description: oplayer's plugin * author: shiyiya * homepage: https://github.com/shiyiya/oplayer */ (function(s,o){typeof exports=="object"&&typeof module<"u"?module.exports=o(require("@oplayer/core")):typeof define=="function"&&define.amd?define(["@oplayer/core"],o):(s=typeof globalThis<"u"?globalThis:s||self,s.OChromecast=o(s.OPlayer))})(this,function(s){"use strict";const o=!!globalThis.chrome,p='<svg viewBox="0 0 1024 1024" style="scale: 0.9;"><path d="M895.66 128H128a85.44 85.44 0 0 0-85.44 85.44v127.84H128v-127.84h767.66v597.12H597.28V896H896a85.44 85.44 0 0 0 85.44-85.44V213.44A85.44 85.44 0 0 0 896 128zM42.56 767.16v127.84h127.82a127.82 127.82 0 0 0-127.82-127.84z m0-170.56V682a213.26 213.26 0 0 1 213.28 213.32v0.68h85.44a298.38 298.38 0 0 0-298-298.72h-0.66z m0-170.54v85.44c212-0.2 384 171.5 384.16 383.5v1h85.44c-0.92-258.92-210.68-468.54-469.6-469.28z"></path></svg>';class u{constructor(e){this.options=e,this.name="oplayer-plugin-chromecast",this.version="1.0.15-beta.3"}apply(e){if(this.canPlay())return this.player=e,this.registerUI(),this}get cast(){return cast.framework.CastContext.getInstance()}get castSessionMedia(){var e;return(e=this.cast.getCurrentSession())==null?void 0:e.getSessionObj().media[0]}get isCastConnected(){return this.cast.getCastState()===cast.framework.CastState.CONNECTED}get device(){var e;return(e=this.cast.getCurrentSession())==null?void 0:e.getCastDevice()}hasActiveCastSession(e){var t,r;return((r=(t=this.castSessionMedia)==null?void 0:t.media)==null?void 0:r.contentId)===(e==null?void 0:e.src)}canPlay(){return o&&!s.isIOS}async __requestChromeCast(){var a;if(this._player||(this._player=new cast.framework.RemotePlayer,new cast.framework.RemotePlayerController(this._player)),this.hasActiveCastSession(this.player.options.source))return;this.cast.setOptions({receiverApplicationId:window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,autoJoinPolicy:chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED,resumeSavedSession:!0,androidReceiverCompatible:!0,...this.options});const e=await this.cast.requestSession();if(e)throw new Error(`Chrome Cast Error Code: ${e}`);return(a=this.cast.getCurrentSession())==null?void 0:a.loadMedia(this.__buildRequest())}__buildRequest(){var l,m,d;const{source:e,isLive:a}=this.player.options,t=new chrome.cast.media.MediaInfo(e.src,((l=this.player.loader)==null?void 0:l.key)=="hls"?"application/x-mpegurl":"video/mp4");t.contentUrl=e.src,t.streamType=a?chrome.cast.media.StreamType.LIVE:chrome.cast.media.StreamType.BUFFERED;const r=new chrome.cast.media.GenericMediaMetadata;e.title&&(r.title=e.title),e.poster&&(r.images=[{url:e.poster,height:null,width:null}]),t.metadata=r;const h=(d=(m=this.player.context.ui)==null?void 0:m.config.subtitle)==null?void 0:d.source;h&&(t.tracks=h.map((n,y)=>{const i=new chrome.cast.media.Track(y,chrome.cast.media.TrackType.TEXT);return i.name=n.name,i.trackContentId=n.src,i.trackContentType=n.type||"text/vtt",i.language=n.language||n.name,i.subtype=chrome.cast.media.TextTrackType.CAPTIONS,i}));const c=new chrome.cast.media.LoadRequest(t);return c.autoplay=this.player.isPlaying,c.currentTime=this.player.currentTime,c}async _loadCast(){var e;(e=window.cast)!=null&&e.framework||(await s.loadSDK("https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1","cast"),await customElements.whenDefined("google-cast-launcher"))}async start(){try{await this._loadCast();const e=await this.__requestChromeCast();if(e)throw new Error(`Chrome Cast Error Code: ${e}`)}catch(e){this.player.emit("cast-error",e)}}registerUI(){if(!this.player.context.ui)return;const{menu:e,icons:a}=this.player.context.ui;e==null||e.register({name:this.player.locales.get("Chromecast"),position:"top",icon:a.chromecast||p,onClick:()=>this.start()})}}return u});