mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
70 lines
2.2 KiB
JavaScript
70 lines
2.2 KiB
JavaScript
import Hls from "hls.js";
|
|
//#region src/dom/hls-js/preload.ts
|
|
/**
|
|
* Manages HLS preload behavior by mapping the media element's `preload`
|
|
* attribute to hls.js `startLoad` / buffer-limit configuration.
|
|
*
|
|
* - `'auto'` or already playing → full buffer limits, immediate start.
|
|
* - `'metadata'` → minimal buffer (1 byte / 1 second), deferred full load on play.
|
|
* - `'none'` / `''` → no start, deferred full load on play.
|
|
*/
|
|
function HlsJsMediaPreloadMixin(BaseClass) {
|
|
class HlsJsMediaPreload extends BaseClass {
|
|
#preloadAbort = null;
|
|
#preload = "metadata";
|
|
#defaultMaxBufferLength;
|
|
#defaultMaxBufferSize;
|
|
constructor(...args) {
|
|
super(...args);
|
|
this.engine?.on(Hls.Events.MANIFEST_LOADING, () => this.#init());
|
|
this.engine?.on(Hls.Events.MEDIA_ATTACHED, () => this.#init());
|
|
this.engine?.on(Hls.Events.MEDIA_DETACHED, () => this.#destroy());
|
|
this.engine?.on(Hls.Events.DESTROYING, () => this.#destroy());
|
|
}
|
|
get preload() {
|
|
return this.#preload;
|
|
}
|
|
set preload(value) {
|
|
this.#preload = value;
|
|
this.#init();
|
|
}
|
|
#destroy() {
|
|
this.#preloadAbort?.abort();
|
|
this.#preloadAbort = null;
|
|
}
|
|
#init() {
|
|
this.#preloadAbort?.abort();
|
|
const target = this.target;
|
|
if (!target) return;
|
|
if (target.preload !== this.preload) target.preload = this.preload;
|
|
const { engine } = this;
|
|
if (!engine) return;
|
|
this.#defaultMaxBufferLength ??= engine.config.maxBufferLength;
|
|
this.#defaultMaxBufferSize ??= engine.config.maxBufferSize;
|
|
const defaultLength = this.#defaultMaxBufferLength;
|
|
const defaultSize = this.#defaultMaxBufferSize;
|
|
const startLoad = (length, size) => {
|
|
const { engine } = this;
|
|
if (!engine) return;
|
|
engine.config.maxBufferLength = length ?? defaultLength;
|
|
engine.config.maxBufferSize = size ?? defaultSize;
|
|
engine.startLoad();
|
|
};
|
|
if (this.preload === "auto" || !target.paused) {
|
|
startLoad();
|
|
return;
|
|
}
|
|
if (this.preload === "metadata") startLoad(1, 1);
|
|
this.#preloadAbort = new AbortController();
|
|
target.addEventListener("play", () => startLoad(), {
|
|
signal: this.#preloadAbort.signal,
|
|
once: true
|
|
});
|
|
}
|
|
}
|
|
return HlsJsMediaPreload;
|
|
}
|
|
//#endregion
|
|
export { HlsJsMediaPreloadMixin };
|
|
|
|
//# sourceMappingURL=preload.js.map
|