mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 19:06:15 -08:00
8 lines
9.9 KiB
JavaScript
8 lines
9.9 KiB
JavaScript
/*!
|
|
* artplayer-plugin-dash-control.js v1.1.0
|
|
* Github: https://github.com/zhw2590582/ArtPlayer
|
|
* (c) 2017-2026 Harvey Zhao
|
|
* Released under the MIT License.
|
|
*/
|
|
!function(e,t){"object"===typeof exports&&"undefined"!==typeof module?module.exports=t():"function"==typeof define&&define.amd?(e.artplayerPluginDashControl=t(),define(function(){return e.artplayerPluginDashControl})):(e="undefined"!==typeof globalThis?globalThis:e||self).artplayerPluginDashControl=t()}(this,function(){"use strict";var e=Object.defineProperty,t=Object.getOwnPropertySymbols,n=Object.prototype.hasOwnProperty,o=Object.prototype.propertyIsEnumerable,r=(t,n,o)=>n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[n]=o,i=(e,i)=>{for(var l in i||(i={}))n.call(i,l)&&r(e,l,i[l]);if(t)for(var l of t(i))o.call(i,l)&&r(e,l,i[l]);return e};function l(e,t=()=>!0){let n,o=!1;for(const i of e){if(!t())break;try{i()}catch(r){o||(o=!0,n=r)}}if(o)throw n}function c(e){const t=new Map;return e.filter(e=>{if(void 0===e.html)return!0;const n=t.get(e.html);return n?(e.default&&Object.assign(n,e),!1):(t.set(e.html,e),!0)})}function a(e,t,n){let o;const r=new Map;function i(n,o=r.get(n)){var i,l,c,a;if(!o||r.get(n)!==o)return;r.delete(n);const s=e[n];"controls"===n&&(null==(i=s.cache)?void 0:i.get)&&(null==(c=null==(l=s.cache.get(t))?void 0:l.option)?void 0:c.onSelect)!==o||"setting"===n&&s.find&&(null==(a=s.find(t))?void 0:a.onSelect)!==o||s.remove(t)}function c(){o=void 0,l([...r].map(([e,t])=>()=>i(e,t)))}return{update:function(l,a,s){if(!a)return void c();const u={};o=u;const f=()=>u===o&&s(),d=t=>f()?(a.select(t,f),f()?(e.notice.show=`${a.title}: ${t.html}`,f()&&l.control&&e.controls.check(t),f()&&l.setting&&e.setting.check(t),t.html):t.html):t.html;l.control?(r.set("controls",d),e.controls.update({name:t,position:"right",html:a.html,style:{padding:"0 10px"},selector:a.selector,onSelect:d})):i("controls"),f()&&(l.setting?(r.set("setting",d),e.setting.update({name:t,tooltip:a.html,html:a.title,icon:n,width:200,selector:a.selector,onSelect:d})):i("setting"))},clear:c,captureNavigation:function(n){var o;const i=e.setting,l=null==(o=i.find)?void 0:o.call(i,t),c=r.get("setting");if(n()&&c&&!0===i.show&&(null==l?void 0:l.onSelect)===c&&l.selector&&i.active===l.selector&&"function"===typeof i.render)return()=>{var o,l;if(!n()||e.setting!==i||!0!==i.show||!i.option||i.active!==i.option)return;const c=null==(o=i.find)?void 0:o.call(i,t),a=r.get("setting");n()&&a&&(null==c?void 0:c.onSelect)===a&&c.selector&&(null==(l=i.render)||l.call(i,c.selector))}}}}function s(e){let t;const n=n=>t===n&&e.active(n.dash),o=e=>e.dash.getSettings().streaming.abr.autoSwitchBitrate.video;function r(){const e=t;e&&(t=void 0,e.epoch++,l(e.callbacks.splice(0).map(([t,n])=>()=>e.off.call(e.dash,t,n))))}function i(n,o){if(t===n){try{r()}catch(i){console.warn("ArtPlayer DASH subscription cleanup failed:",i)}if(!t&&e.active(n.dash))try{e.reset()}catch(i){console.warn("ArtPlayer DASH cleanup failed:",i)}}console.warn("ArtPlayer DASH refresh failed:",o)}function c(t){if(!n(t)||t.suspended||t.queued||t.running)return;t.queued=!0;const r=t.epoch;Promise.resolve().then(()=>{if(n(t)&&t.epoch===r&&!t.suspended){t.queued=!1,t.running=!0;try{t.automatic=o(t),n(t)&&e.refresh()}catch(l){i(t,l)}finally{t.running=!1}}})}return{bind:function(l){const a=t;if((null==a?void 0:a.dash)===l){const e=o(a);return void(n(a)&&(a.automatic=e,a.suspended=!1))}if(r(),t||!e.active(l)||"function"!==typeof l.on||"function"!==typeof l.off)return;const s={dash:l,off:l.off,callbacks:[],epoch:0,queued:!1,running:!1,suspended:!1,automatic:!1};t=s;const u=l.on;try{s.automatic=o(s);const t=["qualityChangeRequested","qualityChangeRendered","trackChangeRendered","streamUpdated","streamInitialized"].map(e=>[e,()=>{"streamUpdated"!==e&&"streamInitialized"!==e||(s.suspended=!1),c(s)}]),r=function(e,t){const n=e;try{if("function"!==typeof n.getVersion||"4.5.2"!==n.getVersion())return;if("function"!==typeof n.getVideoElement||"function"!==typeof n.getActiveStream||"function"!==typeof n.getDashMetrics)return}catch(r){return}let o=!1;return()=>{var e,r,i,l,c,a,s,u,f;if(!o&&t()){o=!0;try{const o=null==(e=n.getVideoElement)?void 0:e.call(n);if(!(null==o?void 0:o.seeking)||!Number.isFinite(o.currentTime))return;const d=o.currentTime,g=null==(r=n.getActiveStream)?void 0:r.call(n),h=null==(i=n.getDashMetrics)?void 0:i.call(n),p=null==(l=null==g?void 0:g.getProcessors)?void 0:l.call(g);if(!Array.isArray(p)||"function"!==typeof(null==h?void 0:h.getCurrentBufferLevel)||"function"!==typeof h.addBufferLevel)return;for(const e of p){if(!t())return;const r=null==(c=e.getType)?void 0:c.call(e);if("audio"!==r&&"video"!==r)continue;const i=null==(a=e.getBufferController)?void 0:a.call(e);if("function"!==typeof(null==i?void 0:i.getRangeAt)||"function"!==typeof i.getIsPruningInProgress||"function"!==typeof i.getAllRangesWithSafetyFactor)continue;const l=h.getCurrentBufferLevel(r),p=i.getRangeAt(d),v=null===p||p&&Number.isFinite(p.end)&&p.end<=d;if(!Number.isFinite(l)||l<=0||!v||!1!==i.getIsPruningInProgress())continue;const y=i.getAllRangesWithSafetyFactor(d);if(Array.isArray(y)&&0===y.length){if(!t()||(null==(s=n.getActiveStream)?void 0:s.call(n))!==g||(null==(u=n.getDashMetrics)?void 0:u.call(n))!==h||(null==(f=n.getVideoElement)?void 0:f.call(n))!==o||o.currentTime!==d||!o.seeking||!t())return;h.addBufferLevel(r,new Date,0)}}}catch(d){console.warn("ArtPlayer DASH seek buffer refresh failed:",d)}finally{o=!1}}}}(l,()=>n(s)&&!s.suspended);r&&t.push(["playbackSeeking",r]),t.push(["playbackTimeUpdated",()=>{if(n(s)&&!s.suspended)try{const e=o(s);n(s)&&e!==s.automatic&&(s.automatic=e,c(s))}catch(e){i(s,e)}}]),t.push(["streamTeardownComplete",()=>{if(n(s)){s.epoch++,s.queued=!1,s.suspended=!0;try{e.reset()}catch(t){i(s,t)}}}]);for(const e of t){if(!n(s))break;s.callbacks.push(e),u.call(l,...e)}}catch(f){if(t===s)try{r()}catch(d){console.warn("ArtPlayer DASH subscription cleanup failed:",d)}throw f}},release:r}}return function(e={}){return t=>{const n=t,{$video:o}=n.template,{errorHandle:r}=n.constructor.utils;let u=!1,f=0;const d=a(n,"dash-quality",'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" height="18"><path fill="#fff" d="M0 96C0 60.7 28.7 32 64 32l384 0c35.3 0 64 28.7 64 64l0 320c0 35.3-28.7 64-64 64L64 480c-35.3 0-64-28.7-64-64L0 96zM323.8 202.5c-4.5-6.6-11.9-10.5-19.8-10.5s-15.4 3.9-19.8 10.5l-87 127.6L170.7 297c-4.6-5.7-11.5-9-18.7-9s-14.2 3.3-18.7 9l-64 80c-5.8 7.2-6.9 17.1-2.9 25.4s12.4 13.6 21.6 13.6l96 0 32 0 208 0c8.9 0 17.1-4.9 21.2-12.8s3.6-17.4-1.4-24.7l-120-176zM112 192a48 48 0 1 0 0-96 48 48 0 1 0 0 96z"/></svg>'),g=a(n,"dash-audio",'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" height="18"><path fill="#fff" d="M256 80C149.9 80 62.4 159.4 49.6 262c9.4-3.8 19.6-6 30.4-6c26.5 0 48 21.5 48 48l0 128c0 26.5-21.5 48-48 48c-44.2 0-80-35.8-80-80l0-16 0-48 0-48C0 146.6 114.6 32 256 32s256 114.6 256 256l0 48 0 48 0 16c0 44.2-35.8 80-80 80c-26.5 0-48-21.5-48-48l0-128c0-26.5 21.5-48 48-48c10.8 0 21 2.1 30.4 6C449.6 159.4 362.1 80 256 80z"/></svg>'),h=[],p=s({active:e=>!u&&!n.isDestroy&&n.dash===e,refresh(){const e=n.dash;let t=f;const o=()=>!u&&!n.isDestroy&&n.dash===e&&f===t,r=d.captureNavigation(o)||g.captureNavigation(o);o()&&(t++,y(),o()&&(null==r||r()))},reset(){const e=++f;v(()=>e===f)}});function v(e=()=>!0){l([d.clear,g.clear],e)}function y(){if(u||n.isDestroy)return;const t=++f,l=n.dash,a=()=>!u&&!n.isDestroy&&t===f&&n.dash===l,s=()=>a()&&l.getVideoElement()===o&&a();try{if(r(l.getVideoElement()===o,'Cannot find instance of DASH from "art.dash"'),!a())return;if(p.bind(l),!s())return;const t=e.quality||{},n=function(e,t,n){const o=function(e){const t="function"===typeof e.getRepresentationsByType;return{levels:()=>t?e.getRepresentationsByType("video"):e.getBitrateInfoListFor("video"),current(n){if(t)return e.getCurrentRepresentationForType("video");const o=e.getQualityFor("video");return n.find(e=>e.qualityIndex===o)},item:(e,n,o,r)=>t?{value:n,id:e.id,default:!r&&null!=o&&e.id===o.id}:{value:e.qualityIndex,default:!r&&null!=o&&e.qualityIndex===o.qualityIndex},select(n,o){const r="auto"===n.value;e.updateSettings({streaming:{abr:{autoSwitchBitrate:{video:r}}}}),!r&&o()&&(t?e.setRepresentationForTypeById("video",n.id):e.setQualityFor("video",n.value))}}}(e),r=o.levels();if(!n()||!(null==r?void 0:r.length))return null;const l=t.auto||"Auto",a=t.getName||(e=>`${e.height}p`),s=o.current(r);if(!n())return null;const u=e.getSettings().streaming.abr.autoSwitchBitrate.video;if(!n())return null;const f=!u&&s?a(s):l;if(!n())return null;const d=[];for(const[c,h]of r.entries()){const e=a(h);if(!n())return null;d.push(i({html:e},o.item(h,c,s,u)))}const g=c(d).sort((e,t)=>t.value-e.value);return g.push({html:l,value:"auto",default:u}),{html:f,title:t.title||"Quality",selector:g,select:o.select}}(l,t,s);if(!s())return;if(d.update(t,n,s),!s())return;const u=e.audio||{},f=function(e,t,n){const o=e.getTracksFor("audio");if(!n()||!(null==o?void 0:o.length))return null;const r=t.auto||"Auto",i=t.getName||(e=>e.lang||e.id),l=e.getCurrentTrackFor("audio")||o[0];if(!n())return null;const a=l?i(l):r;if(!n())return null;const s=function(e,t){if(e.includes(t))return t;if(null==t.id&&null==t.index)return;const n=["id","index","lang"],o=e.filter(e=>n.every(n=>null==t[n]||e[n]===t[n]));return 1===o.length?o[0]:void 0}(o,l),u=[];for(const c of o){const e=i(c);if(!n())return null;u.push({html:e,value:c,default:c===s})}const f=c(u);return{html:a,title:t.title||"Audio",selector:f,select:t=>e.setCurrentTrack(t.value)}}(l,u,s);s()&&g.update(u,f,s)}catch(h){if(a()){const e=++f;for(const t of[p.release,()=>v(()=>f===e)])try{t()}catch(y){console.warn("ArtPlayer DASH cleanup failed:",y)}}throw h}}function m(){if(u)return;u=!0,f++;l([p.release,v,...h.splice(0).map(([e,t])=>()=>n.off(e,t))])}try{const e=[["ready",y],["restart",y],["destroy",m]];for(const t of e){if(u)break;h.push(t),n.on(...t)}}catch(w){try{m()}catch(b){console.warn("ArtPlayer DASH subscription cleanup failed:",b)}throw w}return{name:"artplayerPluginDashControl",update:y}}}});
|