/*! * artplayer-plugin-dash-control.js v2.0.0 * Github: https://github.com/zhw2590582/ArtPlayer * (c) 2017-2026 Harvey Zhao * Released under the MIT License. */ !function(t,e){"object"===typeof exports&&"undefined"!==typeof module?module.exports=e():"function"==typeof define&&define.amd?(t.artplayerPluginDashControl=e(),define(function(){return t.artplayerPluginDashControl})):(t="undefined"!==typeof globalThis?globalThis:t||self).artplayerPluginDashControl=e()}(this,function(){"use strict";function t(t,e=()=>!0){let n,r=!1;for(const i of t){if(!e())break;try{i()}catch(o){r||(r=!0,n=o)}}if(r)throw n}function e(t){const e=new Map;return t.filter(t=>{if(void 0===t.html)return!0;const n=e.get(t.html);return n?(t.default&&Object.assign(n,t),!1):(e.set(t.html,t),!0)})}function n(e,n,r){let o;const i=new Map;function s(t,r=i.get(t)){if(!r||i.get(t)!==r)return;i.delete(t);const o=e[t];"controls"===t&&o.cache?.get&&o.cache.get(n)?.option?.onSelect!==r||"setting"===t&&o.find&&o.find(n)?.onSelect!==r||o.remove(n)}function c(){o=void 0,t([...i].map(([t,e])=>()=>s(t,e)))}return{update:function(t,a,l){if(!a)return void c();const u={};o=u;const f=()=>u===o&&l(),d=n=>f()?(a.select(n,f),f()?(e.notice.show=`${a.title}: ${n.html}`,f()&&t.control&&e.controls.check(n),f()&&t.setting&&e.setting.check(n),n.html):n.html):n.html;t.control?(i.set("controls",d),e.controls.update({name:n,position:"right",html:a.html,style:{padding:"0 10px"},selector:a.selector,onSelect:d})):s("controls"),f()&&(t.setting?(i.set("setting",d),e.setting.update({name:n,tooltip:a.html,html:a.title,icon:r,width:200,selector:a.selector,onSelect:d})):s("setting"))},clear:c,captureNavigation:function(t){const r=e.setting,o=r.find?.(n),s=i.get("setting");if(t()&&s&&!0===r.show&&o?.onSelect===s&&o.selector&&r.active===o.selector&&"function"===typeof r.render)return()=>{if(!t()||e.setting!==r||!0!==r.show||!r.option||r.active!==r.option)return;const o=r.find?.(n),s=i.get("setting");t()&&s&&o?.onSelect===s&&o.selector&&r.render?.(o.selector)}}}}function r(e){let n;const r=t=>n===t&&e.active(t.dash),o=t=>t.dash.getSettings().streaming.abr.autoSwitchBitrate.video;function i(){const e=n;e&&(n=void 0,e.epoch++,t(e.callbacks.splice(0).map(([t,n])=>()=>e.off.call(e.dash,t,n))))}function s(t,r){if(n===t){try{i()}catch(o){console.warn("ArtPlayer DASH subscription cleanup failed:",o)}if(!n&&e.active(t.dash))try{e.reset()}catch(o){console.warn("ArtPlayer DASH cleanup failed:",o)}}console.warn("ArtPlayer DASH refresh failed:",r)}function c(t){if(!r(t)||t.suspended||t.queued||t.running)return;t.queued=!0;const n=t.epoch;Promise.resolve().then(()=>{if(r(t)&&t.epoch===n&&!t.suspended){t.queued=!1,t.running=!0;try{t.automatic=o(t),r(t)&&e.refresh()}catch(i){s(t,i)}finally{t.running=!1}}})}return{bind:function(t){const a=n;if(a?.dash===t){const t=o(a);return void(r(a)&&(a.automatic=t,a.suspended=!1))}if(i(),n||!e.active(t)||"function"!==typeof t.on||"function"!==typeof t.off)return;const l={dash:t,off:t.off,callbacks:[],epoch:0,queued:!1,running:!1,suspended:!1,automatic:!1};n=l;const u=t.on;try{l.automatic=o(l);const n=["qualityChangeRequested","qualityChangeRendered","trackChangeRendered","streamUpdated","streamInitialized"].map(t=>[t,()=>{"streamUpdated"!==t&&"streamInitialized"!==t||(l.suspended=!1),c(l)}]),i=function(t,e){const n=t;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{return}let r=!1;return()=>{if(!r&&e()){r=!0;try{const t=n.getVideoElement?.();if(!t?.seeking||!Number.isFinite(t.currentTime))return;const r=t.currentTime,o=n.getActiveStream?.(),i=n.getDashMetrics?.(),s=o?.getProcessors?.();if(!Array.isArray(s)||"function"!==typeof i?.getCurrentBufferLevel||"function"!==typeof i.addBufferLevel)return;for(const c of s){if(!e())return;const s=c.getType?.();if("audio"!==s&&"video"!==s)continue;const a=c.getBufferController?.();if("function"!==typeof a?.getRangeAt||"function"!==typeof a.getIsPruningInProgress||"function"!==typeof a.getAllRangesWithSafetyFactor)continue;const l=i.getCurrentBufferLevel(s),u=a.getRangeAt(r),f=null===u||u&&Number.isFinite(u.end)&&u.end<=r;if(!Number.isFinite(l)||l<=0||!f||!1!==a.getIsPruningInProgress())continue;const d=a.getAllRangesWithSafetyFactor(r);if(Array.isArray(d)&&0===d.length){if(!e()||n.getActiveStream?.()!==o||n.getDashMetrics?.()!==i||n.getVideoElement?.()!==t||t.currentTime!==r||!t.seeking||!e())return;i.addBufferLevel(s,new Date,0)}}}catch(t){console.warn("ArtPlayer DASH seek buffer refresh failed:",t)}finally{r=!1}}}}(t,()=>r(l)&&!l.suspended);i&&n.push(["playbackSeeking",i]),n.push(["playbackTimeUpdated",()=>{if(r(l)&&!l.suspended)try{const t=o(l);r(l)&&t!==l.automatic&&(l.automatic=t,c(l))}catch(t){s(l,t)}}]),n.push(["streamTeardownComplete",()=>{if(r(l)){l.epoch++,l.queued=!1,l.suspended=!0;try{e.reset()}catch(t){s(l,t)}}}]);for(const e of n){if(!r(l))break;l.callbacks.push(e),u.call(t,...e)}}catch(f){if(n===l)try{i()}catch(d){console.warn("ArtPlayer DASH subscription cleanup failed:",d)}throw f}},release:i}}return function(o={}){return i=>{const s=i,{$video:c}=s.template,{errorHandle:a}=s.constructor.utils;let l=!1,u=0;const f=n(s,"dash-quality",''),d=n(s,"dash-audio",''),h=[],g=r({active:t=>!l&&!s.isDestroy&&s.dash===t,refresh(){const t=s.dash;let e=u;const n=()=>!l&&!s.isDestroy&&s.dash===t&&u===e,r=f.captureNavigation(n)||d.captureNavigation(n);n()&&(e++,y(),n()&&r?.())},reset(){const t=++u;p(()=>t===u)}});function p(e=()=>!0){t([f.clear,d.clear],e)}function y(){if(l||s.isDestroy)return;const t=++u,n=s.dash,r=()=>!l&&!s.isDestroy&&t===u&&s.dash===n,i=()=>r()&&n.getVideoElement()===c&&r();try{if(a(n.getVideoElement()===c,'Cannot find instance of DASH from "art.dash"'),!r())return;if(g.bind(n),!i())return;const t=o.quality||{},s=function(t,n,r){const o=function(t){const e="function"===typeof t.getRepresentationsByType;return{levels:()=>e?t.getRepresentationsByType("video"):t.getBitrateInfoListFor("video"),current(n){if(e)return t.getCurrentRepresentationForType("video");const r=t.getQualityFor("video");return n.find(t=>t.qualityIndex===r)},item:(t,n,r,o)=>e?{value:n,id:t.id,default:!o&&null!=r&&t.id===r.id}:{value:t.qualityIndex,default:!o&&null!=r&&t.qualityIndex===r.qualityIndex},select(n,r){const o="auto"===n.value;t.updateSettings({streaming:{abr:{autoSwitchBitrate:{video:o}}}}),!o&&r()&&(e?t.setRepresentationForTypeById("video",n.id):t.setQualityFor("video",n.value))}}}(t),i=o.levels();if(!r()||!i?.length)return null;const s=n.auto||"Auto",c=n.getName||(t=>`${t.height}p`),a=o.current(i);if(!r())return null;const l=t.getSettings().streaming.abr.autoSwitchBitrate.video;if(!r())return null;const u=!l&&a?c(a):s;if(!r())return null;const f=[];for(const[e,h]of i.entries()){const t=c(h);if(!r())return null;f.push({html:t,...o.item(h,e,a,l)})}const d=e(f).sort((t,e)=>e.value-t.value);return d.push({html:s,value:"auto",default:l}),{html:u,title:n.title||"Quality",selector:d,select:o.select}}(n,t,i);if(!i())return;if(f.update(t,s,i),!i())return;const l=o.audio||{},u=function(t,n,r){const o=t.getTracksFor("audio");if(!r()||!o?.length)return null;const i=n.auto||"Auto",s=n.getName||(t=>t.lang||t.id),c=t.getCurrentTrackFor("audio")||o[0];if(!r())return null;const a=c?s(c):i;if(!r())return null;const l=function(t,e){if(t.includes(e))return e;if(null==e.id&&null==e.index)return;const n=["id","index","lang"],r=t.filter(t=>n.every(n=>null==e[n]||t[n]===e[n]));return 1===r.length?r[0]:void 0}(o,c),u=[];for(const e of o){const t=s(e);if(!r())return null;u.push({html:t,value:e,default:e===l})}const f=e(u);return{html:a,title:n.title||"Audio",selector:f,select:e=>t.setCurrentTrack(e.value)}}(n,l,i);i()&&d.update(l,u,i)}catch(h){if(r()){const t=++u;for(const e of[g.release,()=>p(()=>u===t)])try{e()}catch(y){console.warn("ArtPlayer DASH cleanup failed:",y)}}throw h}}function m(){if(l)return;l=!0,u++;t([g.release,p,...h.splice(0).map(([t,e])=>()=>s.off(t,e))])}try{const t=[["ready",y],["restart",y],["destroy",m]];for(const e of t){if(l)break;h.push(e),s.on(...e)}}catch(v){try{m()}catch(w){console.warn("ArtPlayer DASH subscription cleanup failed:",w)}throw v}return{name:"artplayerPluginDashControl",update:y}}}});