UNPKG

@livepeer/core-web

Version:

Livepeer UI Kit's core web library, for adding reactive stores to video elements.

184 lines (180 loc) 5.97 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/hls.ts var hls_exports = {}; __export(hls_exports, { VIDEO_HLS_INITIALIZED_ATTRIBUTE: () => VIDEO_HLS_INITIALIZED_ATTRIBUTE, createNewHls: () => createNewHls, isHlsSupported: () => isHlsSupported }); module.exports = __toCommonJS(hls_exports); // src/hls/hls.ts var import_media = require("@livepeer/core/media"); var import_hls = __toESM(require("hls.js"), 1); // src/media/utils.ts var import_utils = require("@livepeer/core/utils"); var isClient = () => typeof window !== "undefined"; // src/hls/hls.ts var VIDEO_HLS_INITIALIZED_ATTRIBUTE = "data-livepeer-video-hls-initialized"; var isHlsSupported = () => isClient() ? import_hls.default.isSupported() : true; var createNewHls = ({ source, element, callbacks, aspectRatio, config, initialQuality }) => { if (element.getAttribute(VIDEO_HLS_INITIALIZED_ATTRIBUTE) === "true") { return { setQuality: () => { }, destroy: () => { } }; } element.setAttribute(VIDEO_HLS_INITIALIZED_ATTRIBUTE, "true"); const hls = new import_hls.default({ backBufferLength: 60 * 1.5, manifestLoadingMaxRetry: 0, fragLoadingMaxRetry: 0, levelLoadingMaxRetry: 0, appendErrorMaxRetry: 0, ...config, ...config?.liveSyncDurationCount ? { liveSyncDurationCount: config.liveSyncDurationCount } : { liveMaxLatencyDurationCount: 7, liveSyncDurationCount: 3 } }); const onDestroy = () => { hls?.destroy?.(); element?.removeAttribute?.(VIDEO_HLS_INITIALIZED_ATTRIBUTE); }; if (element) { hls.attachMedia(element); } let redirected = false; hls.on(import_hls.default.Events.LEVEL_LOADED, async (_e, data) => { const { live, totalduration: duration, url } = data.details; if (!redirected) { callbacks?.onRedirect?.(url ?? null); redirected = true; } callbacks?.onLive?.(Boolean(live)); callbacks?.onDuration?.(duration ?? 0); }); hls.on(import_hls.default.Events.MEDIA_ATTACHED, async () => { hls.loadSource(source); hls.on(import_hls.default.Events.MANIFEST_PARSED, (_event, _data) => { setQuality({ hls: hls ?? null, quality: initialQuality, aspectRatio }); callbacks?.onCanPlay?.(); if (config.autoPlay) element?.play?.(); }); }); hls.on(import_hls.default.Events.ERROR, async (_event, data) => { const { details, fatal } = data; const isManifestParsingError = details === "manifestParsingError"; if (!fatal && !isManifestParsingError) return; callbacks?.onError?.(data); if (fatal) { console.error(`Fatal error : ${data.details}`); switch (data.type) { case import_hls.default.ErrorTypes.MEDIA_ERROR: hls.recoverMediaError(); break; case import_hls.default.ErrorTypes.NETWORK_ERROR: console.error(`A network error occurred: ${data.details}`); break; default: console.error(`An unrecoverable error occurred: ${data.details}`); hls.destroy(); break; } } }); function updateOffset() { const currentDate = Date.now(); const newDate = hls.playingDate; if (newDate && currentDate) { callbacks?.onPlaybackOffsetUpdated?.(currentDate - newDate.getTime()); } } const updateOffsetInterval = setInterval(updateOffset, 2e3); return { destroy: () => { onDestroy?.(); clearInterval?.(updateOffsetInterval); element?.removeAttribute?.(VIDEO_HLS_INITIALIZED_ATTRIBUTE); }, setQuality: (videoQuality) => { setQuality({ hls: hls ?? null, quality: videoQuality, aspectRatio }); } }; }; var setQuality = ({ hls, quality, aspectRatio }) => { if (hls) { const { width } = (0, import_media.calculateVideoQualityDimensions)(quality, aspectRatio); if (!width || quality === "auto") { hls.currentLevel = -1; return; } if (hls.levels && hls.levels.length > 0) { const sortedLevels = hls.levels.map((level, index) => ({ ...level, index })).sort( (a, b) => Math.abs((width ?? 0) - a.width) - Math.abs((width ?? 0) - b.width) ); const bestMatchLevel = sortedLevels?.[0]; if ((bestMatchLevel?.index ?? -1) >= 0) { hls.currentLevel = bestMatchLevel.index; } else { hls.currentLevel = -1; } } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { VIDEO_HLS_INITIALIZED_ATTRIBUTE, createNewHls, isHlsSupported }); //# sourceMappingURL=index.cjs.map