@oplayer/shaka
Version:
shaka-player plugin for oplayer
9 lines (8 loc) • 7.2 kB
JavaScript
/**
* name: @oplayer/shaka
* version: v1.2.26-beta.5
* description: shaka-player plugin for oplayer
* author: shiyiya
* homepage: https://github.com/shiyiya/oplayer
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@oplayer/core")):"function"==typeof define&&define.amd?define(["@oplayer/core"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).OShaka=t(e.OPlayer)}(this,(function(e){"use strict";const t=e=>!(!e.format||!["m3u8","mpd","shaka"].includes(e.format))||("auto"===e.format||void 0===e.format)&&/(m3u8|mpd|shaka)(#|\?|$)/i.test(e.src);class i{constructor(e){this.key="shaka",this.name="oplayer-plugin-shaka",this.version="1.2.26-beta.5",this.options={matcher:t,qualityControl:!0,audioControl:!0,textControl:!0,qualityControlType:"menu"},this.setupQuality=(e,t,n)=>{let a=[];t.getLoadMode()!=i.library.Player.LoadMode.SRC_EQUALS&&(a=t.getVariantTracks());const s=a.find((e=>e.active));if(s&&(a=a.filter((e=>e.language==s.language&&((!e.channelsCount||!s.channelsCount||e.channelsCount==s.channelsCount)&&JSON.stringify(e.audioRoles)==JSON.stringify(s.audioRoles))))),t.isAudioOnly())a=a.filter(((e,t)=>a.findIndex((t=>t.bandwidth==e.bandwidth))==t));else{const e=[].concat(new Set(a.map((e=>e.audioId)))).filter((e=>null!==e));a=e.length>1?a.filter(((e,t)=>a.findIndex((t=>t.height==e.height&&t.videoBandwidth==e.videoBandwidth&&t.frameRate==e.frameRate&&t.hdr==e.hdr&&t.videoLayout==e.videoLayout))==t)):a.filter(((e,t)=>a.findIndex((t=>t.height==e.height&&t.bandwidth==e.bandwidth&&t.frameRate==e.frameRate&&t.hdr==e.hdr&&t.videoLayout==e.videoLayout))==t))}if(!(a.length>1))return;t.isAudioOnly()?a.sort(((e,t)=>t.bandwidth-e.bandwidth)):a.sort(((e,t)=>t.height==e.height||null==e.height||null==t.height?t.bandwidth-e.bandwidth:t.height-e.height));const o=t.getConfiguration().abr.enabled,r=a.map((e=>({name:!t.isAudioOnly()&&e.height&&e.width?this.getResolutionLabel_(e,a):e.bandwidth?Math.round(e.bandwidth/1e3)+" kbits/s":"Unknown",default:!o&&e==s,value:e}))),l="menu"==n?e.context.ui.menu:e.context.ui.setting,d=e.locales.get("Auto");l.unregister(i.name+"-Quality"),l.register({icon:"setting"==n?e.context.ui.icons.quality:void 0,name:"setting"==n?"Quality":!o&&s?this.getResolutionLabel_(s,[]):d,type:"selector",key:i.name+"-Quality",children:[{name:e.locales.get("Auto"),default:o,value:-1}].concat(r),onChange:(e,i)=>{let n=e.value;const a=-1==n;t.configure({abr:{enabled:a}}),a?i.textContent=d:(i.textContent=this.getResolutionLabel_(n,[]),t.selectVariantTrack(n,!0))}})},this.setupAudioSelection=(e,t)=>{const n=t.getAudioTracks();if(!(n.length>1))return;const a=n.sort(((e,t)=>e.language.localeCompare(t.language))).map((e=>{var t,n;return{name:e.language+" "+((null==(n=(t=i.library.util.MimeUtils).getNormalizedCodec)?void 0:n.call(t,e.codecs))||e.codecs),default:e.active,value:e}}));this.settingUpdater({player:e,name:"Language",icon:e.context.ui.icons.lang,settings:a,onChange(e){let i=e.value;t.selectAudioTrack(i)}})},this.setupTextSelection=(e,t)=>{const i=t.getTextTracks();if(!(i.length>1))return;const n=t.isTextTrackVisible(),a=[{name:e.locales.get("Off"),default:!n,value:-1}].concat(i.sort(((e,t)=>e.language.localeCompare(t.language))).map((e=>({name:e.language,default:n&&e.active,value:e}))));this.settingUpdater({player:e,name:"Subtitle",icon:e.context.ui.icons.lang,settings:a,onChange(e){let i=e.value;-1!=i&&t.selectTextTrack(i),t.setTextTrackVisibility(-1!=i)}})},Object.assign(this.options,e)}apply(e){return this.player=e,this}async load(t,n){var a,s;if(!this.options.matcher(n))return!1;const o=this.options,r=o.library,l=o.config,d=o.requestFilter,u=o.qualityControl,c=o.audioControl,h=o.textControl,g=o.qualityControlType;i.library||(i.library=globalThis.shaka||(r?await e.loadSDK(r,"shaka"):(await import("shaka-player/dist/shaka-player.compiled")).default),i.library.polyfill.installAll());const m=i.library.Player;if(!m.isBrowserSupported())return!1;this.instance=new m,await this.instance.attach(t.$video),l&&this.instance.configure(l),d&&(null==(a=this.instance.getNetworkingEngine())||a.registerRequestFilter(d));const y=this.instance.eventManager=new i.library.util.EventManager;y.listen(this.instance,"loading",(e=>{t.emit("loading",e)})),y.listen(this.instance,"loaded",(e=>{t.emit("loaded",e)})),y.listen(this.instance,"error",(e=>{t.emit("error",Object.assign({pluginName:i.name},e))})),y.listenOnce(t.$video,"seeking",(()=>{setTimeout((()=>{t.emit("seeked")}))}));try{await this.instance.load(n.src)}catch(f){t.emit("error",Object.assign({pluginName:i.name},f))}if(t.options.isLive){y.listenOnce(t.$video,"loadedmetadata",(()=>{t.$video.currentTime=this.seekRange.end}));const e=null==(s=t.$root.querySelector('[aria-label="time"'))?void 0:s.parentElement,i=null==e?void 0:e.firstElementChild;if(e&&i){y.listen(e,"click",(()=>{t.$video.currentTime=this.seekRange.end}));const n=t.locales.get("Back to Live"),a=()=>{this.seekRange.end-t.$video.currentTime>5?(i.style.backgroundColor="#ccc",e.ariaLabel=n):(i.style.cssText="",e.removeAttribute("aria-label"))};this.instance.eventManager.listen(t.$video,"timeupdate",a)}Object.defineProperty(t.$video,"duration",{get:()=>this.instance?this._duration:t.$video.duration}),Object.defineProperty(t,"currentTime",{get:()=>this.instance?this.getCurrentTime():t.$video.currentTime}),Object.defineProperty(t,"seek",{value:e=>{this.instance?t.$video.currentTime=this.seekRange.start+e:t.$video.currentTime=e}})}return t.context.ui&&(u&&this.setupQuality(t,this.instance,g),c&&this.setupAudioSelection(t,this.instance),h&&this.setupTextSelection(t,this.instance)),this}getCurrentTime(){if(!this.instance)return 0;const e=this.instance.getMediaElement();return e?e.currentTime-this.seekRange.start:0}get seekRange(){return this.instance?this.instance.seekRange():{start:0,end:0}}get _duration(){return this.instance?this.seekRange.end-this.seekRange.start:0}async destroy(){var e,t,n;["Quality","Language","Subtitle"].forEach((e=>this.player.context.ui.setting.unregister(i.name+"-"+e))),this.player.context.ui.menu.unregister(i.name+"-Quality"),null==(e=this.instance)||e.eventManager.removeAll(),await(null==(t=this.instance)?void 0:t.unload()),await(null==(n=this.instance)?void 0:n.destroy()),this.instance=void 0}settingUpdater(e){const t=e.name,n=e.icon,a=e.onChange,s=e.player,o=e.settings;s.context.ui.setting.unregister(i.name+"-"+t),s.context.ui.setting.register({name:s.locales.get(t),icon:n,onChange:a,type:"selector",key:i.name+"-"+t,children:o})}getResolutionLabel_(e,t){const i=e.height||0,n=e.width||0;let a=i;n/i>16/9&&(a=Math.round(9*n/16));let s=a+"p";2160==a&&(s="4K");const o=new Set;for(const r of t)r.frameRate&&o.add(Math.round(r.frameRate));if(o.size>1){const t=e.frameRate;t&&(t>=50||t<=20)&&(s+=Math.round(t))}"PQ"!=e.hdr&&"HLG"!=e.hdr||(s+=" (HDR)"),"CH-STEREO"==e.videoLayout&&(s+=" (3D)");if(t.some((t=>t!=e&&t.height==e.height))&&"setting"==this.options.qualityControlType){if(!t.some((t=>t!=e&&t.height==e.height&&(t.videoBandwidth||t.bandwidth)==(e.videoBandwidth||e.bandwidth)))){const t=e.videoBandwidth||e.bandwidth;s+=" ("+Math.round(t/1e3)+" kbits/s)"}}return s}}return function(e){return new i(e)}}));