@interlacejs/hls-adapter 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -2
- package/dist/index.cjs +239 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +86 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.js +201 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -3
package/README.md
CHANGED
|
@@ -1,3 +1,93 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @interlacejs/hls-adapter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An HLS implementation of the [`@interlacejs/core`](https://github.com/farayolaj/interlace) `VideoAdapter` contract, built on [hls.js](https://github.com/video-dev/hls.js). It extends `NativeVideoAdapter` from `@interlacejs/native-adapter` (play/pause/seek, `programmaticSeek`, overlay mounting, fullscreen and `destroy()` behave the same) and adds the optional **quality** capability, so `@interlacejs/player` shows its built-in quality menu.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @interlacejs/hls-adapter @interlacejs/core hls.js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`hls.js` (^1.5) is a peer dependency.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { HlsVideoAdapter } from "@interlacejs/hls-adapter";
|
|
17
|
+
import { InteractiveVideoPlayer } from "@interlacejs/player";
|
|
18
|
+
|
|
19
|
+
function Watch({ doc, hlsUrl, mp4Url }) {
|
|
20
|
+
const videoRef = useRef<HTMLVideoElement>(null);
|
|
21
|
+
const [adapter, setAdapter] = useState<HlsVideoAdapter>();
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (!videoRef.current) return;
|
|
25
|
+
const a = new HlsVideoAdapter(videoRef.current, {
|
|
26
|
+
src: hlsUrl, // .m3u8 manifest
|
|
27
|
+
fallbackSrc: mp4Url, // optional progressive fallback
|
|
28
|
+
rememberQualityKey: "my-app:video-quality", // optional
|
|
29
|
+
});
|
|
30
|
+
setAdapter(a);
|
|
31
|
+
return () => a.destroy();
|
|
32
|
+
}, [hlsUrl, mp4Url]);
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<div style={{ position: "relative" }}>
|
|
36
|
+
{/* No src: the adapter sets the source. */}
|
|
37
|
+
<video ref={videoRef} controls />
|
|
38
|
+
{adapter && (
|
|
39
|
+
<div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
|
|
40
|
+
<InteractiveVideoPlayer
|
|
41
|
+
adapter={adapter}
|
|
42
|
+
document={doc}
|
|
43
|
+
registry={registry}
|
|
44
|
+
/>
|
|
45
|
+
</div>
|
|
46
|
+
)}
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Don't set `src` on the `<video>` yourself; the adapter picks the source.
|
|
53
|
+
|
|
54
|
+
## Options
|
|
55
|
+
|
|
56
|
+
| Option | Description |
|
|
57
|
+
| -------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
58
|
+
| `src` | Video URL (required): an HLS manifest (`.m3u8`) or a plain video file such as an MP4. |
|
|
59
|
+
| `type` | `"auto"` (default: HLS when the URL path ends in `.m3u8`), `"hls"` or `"progressive"`. |
|
|
60
|
+
| `fallbackSrc` | Progressive source (e.g. MP4) for browsers without HLS support and for unrecoverable errors. |
|
|
61
|
+
| `hlsConfig` | `Partial<HlsConfig>` passed to `new Hls()`. |
|
|
62
|
+
| `rememberQualityKey` | localStorage key for the viewer's quality choice (stored as a height or `"auto"`). Not remembered when omitted. |
|
|
63
|
+
|
|
64
|
+
## Playback paths
|
|
65
|
+
|
|
66
|
+
- **Plain video files** (`src` isn't HLS, e.g. an MP4): played directly in the `<video>` element, like `NativeVideoAdapter`. There is one quality, so `getQualityLevels()` returns `[]` and the player shows no quality menu. You can use this adapter for every video and let it pick the path.
|
|
67
|
+
- **hls.js** (`Hls.isSupported()`, most desktop and Android browsers): attaches hls.js and loads `src`. Once the manifest is parsed, levels are exposed one per height (highest bitrate wins), sorted high to low and labelled `720p`, etc. A remembered choice is re-applied (the same height, or the nearest lower one), and `qualitychange` fires.
|
|
68
|
+
- **Native HLS** (Safari, iOS): sets `video.src = src`. The browser picks the quality, so `getQualityLevels()` returns `[]` and the player hides its quality menu.
|
|
69
|
+
- **Neither**: plays `fallbackSrc`, if given.
|
|
70
|
+
|
|
71
|
+
## Quality API
|
|
72
|
+
|
|
73
|
+
`HlsVideoAdapter` implements `QualityCapableAdapter` from `@interlacejs/core`:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
getQualityLevels(): QualityLevel[]; // [] until the manifest loads, and on the native/fallback paths
|
|
77
|
+
getQualityLevel(): number; // selected id, -1 = Auto
|
|
78
|
+
getActiveQualityLevel(): number | null; // the level playing right now (useful in Auto)
|
|
79
|
+
setQualityLevel(id: number): void; // -1 = Auto; persisted under rememberQualityKey
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`qualitychange` fires when the levels load or are cleared, when the selection changes, and when hls.js switches the level that is playing. `getHls()` returns the hls.js instance (or `null`) if you need lower-level access.
|
|
83
|
+
|
|
84
|
+
## Error handling
|
|
85
|
+
|
|
86
|
+
- Fatal network error after the manifest loaded: `hls.startLoad()` is retried once.
|
|
87
|
+
- Fatal media error: `hls.recoverMediaError()` is tried once.
|
|
88
|
+
- Anything else, or a repeated failure: hls.js is destroyed and the adapter switches to `fallbackSrc` (if given), keeping the current time and paused state. The position is restored with the adapter's own `seek()`, so the player receives `programmaticSeek`, not a user `seek`. Levels become `[]` and `qualitychange` fires.
|
|
89
|
+
- On the native HLS path, a `<video>` `error` event switches to `fallbackSrc` in the same way.
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
ISC
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
HlsVideoAdapter: () => HlsVideoAdapter,
|
|
34
|
+
isHlsSource: () => isHlsSource
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/hls-video-adapter.ts
|
|
39
|
+
var import_native_adapter = require("@interlacejs/native-adapter");
|
|
40
|
+
var import_hls = __toESM(require("hls.js"), 1);
|
|
41
|
+
var AUTO = -1;
|
|
42
|
+
var HLS_MIME = "application/vnd.apple.mpegurl";
|
|
43
|
+
var HlsVideoAdapter = class extends import_native_adapter.NativeVideoAdapter {
|
|
44
|
+
options;
|
|
45
|
+
hls = null;
|
|
46
|
+
levels = [];
|
|
47
|
+
selectedLevel = AUTO;
|
|
48
|
+
activeLevel = null;
|
|
49
|
+
manifestLoaded = false;
|
|
50
|
+
networkRetried = false;
|
|
51
|
+
mediaRecovered = false;
|
|
52
|
+
usingFallback = false;
|
|
53
|
+
qualityListeners = /* @__PURE__ */ new Set();
|
|
54
|
+
onVideoError = () => {
|
|
55
|
+
if (!this.hls) this.switchToFallback();
|
|
56
|
+
};
|
|
57
|
+
constructor(video, options) {
|
|
58
|
+
super(video);
|
|
59
|
+
this.options = options;
|
|
60
|
+
video.addEventListener("error", this.onVideoError);
|
|
61
|
+
if (!isHlsSource(options)) {
|
|
62
|
+
video.src = options.src;
|
|
63
|
+
} else if (import_hls.default.isSupported()) {
|
|
64
|
+
this.attachHls();
|
|
65
|
+
} else if (video.canPlayType(HLS_MIME)) {
|
|
66
|
+
video.src = options.src;
|
|
67
|
+
} else if (options.fallbackSrc) {
|
|
68
|
+
this.usingFallback = true;
|
|
69
|
+
video.src = options.fallbackSrc;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
getQualityLevels() {
|
|
73
|
+
return this.levels.map((level) => ({ ...level }));
|
|
74
|
+
}
|
|
75
|
+
getQualityLevel() {
|
|
76
|
+
return this.selectedLevel;
|
|
77
|
+
}
|
|
78
|
+
getActiveQualityLevel() {
|
|
79
|
+
return this.activeLevel;
|
|
80
|
+
}
|
|
81
|
+
setQualityLevel(id) {
|
|
82
|
+
if (!this.hls) return;
|
|
83
|
+
const level = this.levels.find((l) => l.id === id);
|
|
84
|
+
const next = level ? level.id : AUTO;
|
|
85
|
+
this.selectedLevel = next;
|
|
86
|
+
this.hls.currentLevel = next;
|
|
87
|
+
this.rememberPreference(level ? String(level.height) : "auto");
|
|
88
|
+
this.emitQualityChange();
|
|
89
|
+
}
|
|
90
|
+
on(eventType, handler) {
|
|
91
|
+
if (eventType !== "qualitychange") return super.on(eventType, handler);
|
|
92
|
+
this.qualityListeners.add(handler);
|
|
93
|
+
return () => {
|
|
94
|
+
this.qualityListeners.delete(handler);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** The hls.js instance while HLS plays through hls.js, else `null`. */
|
|
98
|
+
getHls() {
|
|
99
|
+
return this.hls;
|
|
100
|
+
}
|
|
101
|
+
destroy() {
|
|
102
|
+
this.getVideoElement().removeEventListener("error", this.onVideoError);
|
|
103
|
+
this.hls?.destroy();
|
|
104
|
+
this.hls = null;
|
|
105
|
+
this.qualityListeners.clear();
|
|
106
|
+
super.destroy();
|
|
107
|
+
}
|
|
108
|
+
attachHls() {
|
|
109
|
+
const hls = new import_hls.default(this.options.hlsConfig);
|
|
110
|
+
this.hls = hls;
|
|
111
|
+
hls.on(import_hls.default.Events.MANIFEST_PARSED, (_event, data) => {
|
|
112
|
+
this.manifestLoaded = true;
|
|
113
|
+
this.levels = toQualityLevels(data.levels);
|
|
114
|
+
this.applyRememberedPreference();
|
|
115
|
+
this.emitQualityChange();
|
|
116
|
+
});
|
|
117
|
+
hls.on(import_hls.default.Events.LEVEL_SWITCHED, (_event, data) => {
|
|
118
|
+
this.activeLevel = this.toExposedId(data.level);
|
|
119
|
+
this.emitQualityChange();
|
|
120
|
+
});
|
|
121
|
+
hls.on(import_hls.default.Events.ERROR, (_event, data) => this.handleHlsError(data));
|
|
122
|
+
hls.attachMedia(this.getVideoElement());
|
|
123
|
+
hls.loadSource(this.options.src);
|
|
124
|
+
}
|
|
125
|
+
handleHlsError(data) {
|
|
126
|
+
if (!data.fatal || !this.hls) return;
|
|
127
|
+
if (data.type === import_hls.default.ErrorTypes.NETWORK_ERROR && this.manifestLoaded && !this.networkRetried) {
|
|
128
|
+
this.networkRetried = true;
|
|
129
|
+
this.hls.startLoad();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (data.type === import_hls.default.ErrorTypes.MEDIA_ERROR && !this.mediaRecovered) {
|
|
133
|
+
this.mediaRecovered = true;
|
|
134
|
+
this.hls.recoverMediaError();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this.switchToFallback();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Gives up on HLS: tears hls.js down and plays `fallbackSrc` from the
|
|
141
|
+
* same position, keeping the paused state.
|
|
142
|
+
*/
|
|
143
|
+
switchToFallback() {
|
|
144
|
+
const { fallbackSrc } = this.options;
|
|
145
|
+
const video = this.getVideoElement();
|
|
146
|
+
const time = video.currentTime;
|
|
147
|
+
const wasPaused = video.paused;
|
|
148
|
+
if (this.hls) {
|
|
149
|
+
this.hls.destroy();
|
|
150
|
+
this.hls = null;
|
|
151
|
+
}
|
|
152
|
+
this.levels = [];
|
|
153
|
+
this.selectedLevel = AUTO;
|
|
154
|
+
this.activeLevel = null;
|
|
155
|
+
this.emitQualityChange();
|
|
156
|
+
if (!fallbackSrc || this.usingFallback) return;
|
|
157
|
+
this.usingFallback = true;
|
|
158
|
+
video.addEventListener(
|
|
159
|
+
"loadedmetadata",
|
|
160
|
+
() => {
|
|
161
|
+
if (time > 0) void this.seek(time);
|
|
162
|
+
if (!wasPaused) {
|
|
163
|
+
Promise.resolve().then(() => video.play()).catch((err) => {
|
|
164
|
+
console.error("Failed to resume fallback video:", err);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
{ once: true }
|
|
169
|
+
);
|
|
170
|
+
video.src = fallbackSrc;
|
|
171
|
+
}
|
|
172
|
+
applyRememberedPreference() {
|
|
173
|
+
const stored = this.readPreference();
|
|
174
|
+
if (!stored || stored === "auto" || !this.hls) return;
|
|
175
|
+
const height = Number(stored);
|
|
176
|
+
if (!Number.isFinite(height) || this.levels.length === 0) return;
|
|
177
|
+
const level = this.levels.find((l) => l.height <= height) ?? this.levels[this.levels.length - 1];
|
|
178
|
+
this.selectedLevel = level.id;
|
|
179
|
+
this.hls.currentLevel = level.id;
|
|
180
|
+
}
|
|
181
|
+
readPreference() {
|
|
182
|
+
const key = this.options.rememberQualityKey;
|
|
183
|
+
if (!key) return null;
|
|
184
|
+
try {
|
|
185
|
+
return window.localStorage.getItem(key);
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
rememberPreference(value) {
|
|
191
|
+
const key = this.options.rememberQualityKey;
|
|
192
|
+
if (!key) return;
|
|
193
|
+
try {
|
|
194
|
+
window.localStorage.setItem(key, value);
|
|
195
|
+
} catch {
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/** Maps an hls.js level index to the id of the exposed level of the same height. */
|
|
199
|
+
toExposedId(hlsIndex) {
|
|
200
|
+
const height = this.hls?.levels[hlsIndex]?.height;
|
|
201
|
+
if (height === void 0) return null;
|
|
202
|
+
return this.levels.find((l) => l.height === height)?.id ?? null;
|
|
203
|
+
}
|
|
204
|
+
emitQualityChange() {
|
|
205
|
+
const event = { type: "qualitychange" };
|
|
206
|
+
this.qualityListeners.forEach((handler) => handler(event));
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
function toQualityLevels(levels) {
|
|
210
|
+
const byHeight = /* @__PURE__ */ new Map();
|
|
211
|
+
levels.forEach((level, index) => {
|
|
212
|
+
if (!level.height) return;
|
|
213
|
+
const existing = byHeight.get(level.height);
|
|
214
|
+
if (existing && (existing.bitrate ?? 0) >= (level.bitrate ?? 0)) return;
|
|
215
|
+
byHeight.set(level.height, {
|
|
216
|
+
id: index,
|
|
217
|
+
height: level.height,
|
|
218
|
+
bitrate: level.bitrate || void 0,
|
|
219
|
+
label: `${level.height}p`
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
return [...byHeight.values()].sort((a, b) => b.height - a.height);
|
|
223
|
+
}
|
|
224
|
+
function isHlsSource(options) {
|
|
225
|
+
if (options.type === "hls") return true;
|
|
226
|
+
if (options.type === "progressive") return false;
|
|
227
|
+
let path = options.src;
|
|
228
|
+
try {
|
|
229
|
+
path = new URL(options.src, "http://localhost").pathname;
|
|
230
|
+
} catch {
|
|
231
|
+
}
|
|
232
|
+
return /\.m3u8$/i.test(path);
|
|
233
|
+
}
|
|
234
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
235
|
+
0 && (module.exports = {
|
|
236
|
+
HlsVideoAdapter,
|
|
237
|
+
isHlsSource
|
|
238
|
+
});
|
|
239
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hls-video-adapter.ts"],"sourcesContent":["export { HlsVideoAdapter, isHlsSource } from \"./hls-video-adapter\";\nexport type { HlsVideoAdapterOptions } from \"./hls-video-adapter\";\n","import type {\n QualityCapableAdapter,\n QualityLevel,\n VideoAdapterEvent,\n VideoAdapterEventType,\n} from \"@interlacejs/core\";\nimport { NativeVideoAdapter } from \"@interlacejs/native-adapter\";\nimport Hls, { type ErrorData, type HlsConfig, type Level } from \"hls.js\";\n\nexport interface HlsVideoAdapterOptions {\n /**\n * Video URL. HLS manifests (`.m3u8`) play through hls.js or the\n * browser's native HLS; any other source (e.g. an MP4) plays directly in\n * the `<video>` element, with no quality levels.\n */\n src: string;\n /**\n * Source type. `\"auto\"` (default) treats URLs whose path ends in `.m3u8`\n * as HLS; set `\"hls\"` for manifests served from other paths, or\n * `\"progressive\"` to always play `src` directly.\n */\n type?: \"auto\" | \"hls\" | \"progressive\";\n /**\n * Progressive source (e.g. an MP4) used when HLS cannot play: no hls.js\n * support and no native HLS, or an unrecoverable playback error.\n */\n fallbackSrc?: string;\n /** Extra hls.js configuration. */\n hlsConfig?: Partial<HlsConfig>;\n /**\n * localStorage key under which the viewer's quality choice is remembered\n * (as a height, or \"auto\"). Not remembered when omitted.\n */\n rememberQualityKey?: string;\n}\n\ntype Listener = (event: VideoAdapterEvent) => void;\n\nconst AUTO = -1;\nconst HLS_MIME = \"application/vnd.apple.mpegurl\";\n\n/**\n * HLS video adapter. Extends {@link NativeVideoAdapter} (play/pause/seek,\n * events, overlay mounting, fullscreen and destroy are reused) and adds\n * quality levels via hls.js.\n *\n * - hls.js supported: plays through hls.js and exposes the stream's levels.\n * - Native HLS (Safari/iOS): sets `video.src`; no levels are exposed.\n * - Neither: plays `fallbackSrc`, if given.\n *\n * Usage:\n * const adapter = new HlsVideoAdapter(video, {\n * src: \"https://cdn.example.com/lesson/master.m3u8\",\n * fallbackSrc: \"https://cdn.example.com/lesson.mp4\",\n * rememberQualityKey: \"my-app:quality\",\n * });\n */\nexport class HlsVideoAdapter\n extends NativeVideoAdapter\n implements QualityCapableAdapter\n{\n private readonly options: HlsVideoAdapterOptions;\n private hls: Hls | null = null;\n private levels: QualityLevel[] = [];\n private selectedLevel = AUTO;\n private activeLevel: number | null = null;\n private manifestLoaded = false;\n private networkRetried = false;\n private mediaRecovered = false;\n private usingFallback = false;\n private qualityListeners = new Set<Listener>();\n private readonly onVideoError = () => {\n // Only the native path reports failures here; hls.js reports its own.\n if (!this.hls) this.switchToFallback();\n };\n\n constructor(video: HTMLVideoElement, options: HlsVideoAdapterOptions) {\n super(video);\n this.options = options;\n video.addEventListener(\"error\", this.onVideoError);\n\n if (!isHlsSource(options)) {\n // Plain video file: play it as-is (one quality, no menu).\n video.src = options.src;\n } else if (Hls.isSupported()) {\n this.attachHls();\n } else if (video.canPlayType(HLS_MIME)) {\n video.src = options.src;\n } else if (options.fallbackSrc) {\n this.usingFallback = true;\n video.src = options.fallbackSrc;\n }\n }\n\n getQualityLevels(): QualityLevel[] {\n return this.levels.map((level) => ({ ...level }));\n }\n\n getQualityLevel(): number {\n return this.selectedLevel;\n }\n\n getActiveQualityLevel(): number | null {\n return this.activeLevel;\n }\n\n setQualityLevel(id: number): void {\n if (!this.hls) return;\n const level = this.levels.find((l) => l.id === id);\n const next = level ? level.id : AUTO;\n this.selectedLevel = next;\n this.hls.currentLevel = next;\n this.rememberPreference(level ? String(level.height) : \"auto\");\n this.emitQualityChange();\n }\n\n on(eventType: VideoAdapterEventType, handler: Listener): () => void {\n if (eventType !== \"qualitychange\") return super.on(eventType, handler);\n this.qualityListeners.add(handler);\n return () => {\n this.qualityListeners.delete(handler);\n };\n }\n\n /** The hls.js instance while HLS plays through hls.js, else `null`. */\n getHls(): Hls | null {\n return this.hls;\n }\n\n destroy(): void {\n this.getVideoElement().removeEventListener(\"error\", this.onVideoError);\n this.hls?.destroy();\n this.hls = null;\n this.qualityListeners.clear();\n super.destroy();\n }\n\n private attachHls(): void {\n const hls = new Hls(this.options.hlsConfig);\n this.hls = hls;\n\n hls.on(Hls.Events.MANIFEST_PARSED, (_event, data) => {\n this.manifestLoaded = true;\n this.levels = toQualityLevels(data.levels);\n this.applyRememberedPreference();\n this.emitQualityChange();\n });\n\n hls.on(Hls.Events.LEVEL_SWITCHED, (_event, data) => {\n this.activeLevel = this.toExposedId(data.level);\n this.emitQualityChange();\n });\n\n hls.on(Hls.Events.ERROR, (_event, data) => this.handleHlsError(data));\n\n hls.attachMedia(this.getVideoElement());\n hls.loadSource(this.options.src);\n }\n\n private handleHlsError(data: ErrorData): void {\n if (!data.fatal || !this.hls) return;\n if (\n data.type === Hls.ErrorTypes.NETWORK_ERROR &&\n this.manifestLoaded &&\n !this.networkRetried\n ) {\n this.networkRetried = true;\n this.hls.startLoad();\n return;\n }\n if (data.type === Hls.ErrorTypes.MEDIA_ERROR && !this.mediaRecovered) {\n this.mediaRecovered = true;\n this.hls.recoverMediaError();\n return;\n }\n this.switchToFallback();\n }\n\n /**\n * Gives up on HLS: tears hls.js down and plays `fallbackSrc` from the\n * same position, keeping the paused state.\n */\n private switchToFallback(): void {\n const { fallbackSrc } = this.options;\n const video = this.getVideoElement();\n const time = video.currentTime;\n const wasPaused = video.paused;\n\n if (this.hls) {\n this.hls.destroy();\n this.hls = null;\n }\n this.levels = [];\n this.selectedLevel = AUTO;\n this.activeLevel = null;\n this.emitQualityChange();\n\n if (!fallbackSrc || this.usingFallback) return;\n this.usingFallback = true;\n video.addEventListener(\n \"loadedmetadata\",\n () => {\n if (time > 0) void this.seek(time);\n if (!wasPaused) {\n Promise.resolve()\n .then(() => video.play())\n .catch((err: unknown) => {\n console.error(\"Failed to resume fallback video:\", err);\n });\n }\n },\n { once: true },\n );\n video.src = fallbackSrc;\n }\n\n private applyRememberedPreference(): void {\n const stored = this.readPreference();\n if (!stored || stored === \"auto\" || !this.hls) return;\n const height = Number(stored);\n if (!Number.isFinite(height) || this.levels.length === 0) return;\n // Levels are sorted high → low: take the first at or below the stored\n // height, else the lowest available.\n const level =\n this.levels.find((l) => l.height <= height) ??\n this.levels[this.levels.length - 1]!;\n this.selectedLevel = level.id;\n this.hls.currentLevel = level.id;\n }\n\n private readPreference(): string | null {\n const key = this.options.rememberQualityKey;\n if (!key) return null;\n try {\n return window.localStorage.getItem(key);\n } catch {\n return null;\n }\n }\n\n private rememberPreference(value: string): void {\n const key = this.options.rememberQualityKey;\n if (!key) return;\n try {\n window.localStorage.setItem(key, value);\n } catch {\n // Storage unavailable (private mode, quota): the choice is not kept.\n }\n }\n\n /** Maps an hls.js level index to the id of the exposed level of the same height. */\n private toExposedId(hlsIndex: number): number | null {\n const height = this.hls?.levels[hlsIndex]?.height;\n if (height === undefined) return null;\n return this.levels.find((l) => l.height === height)?.id ?? null;\n }\n\n private emitQualityChange(): void {\n const event: VideoAdapterEvent = { type: \"qualitychange\" };\n this.qualityListeners.forEach((handler) => handler(event));\n }\n}\n\n/**\n * One level per height (highest bitrate wins), sorted high → low. Ids are\n * the hls.js level indices, so they can be assigned to `hls.currentLevel`.\n */\nfunction toQualityLevels(levels: Level[]): QualityLevel[] {\n const byHeight = new Map<number, QualityLevel>();\n levels.forEach((level, index) => {\n if (!level.height) return;\n const existing = byHeight.get(level.height);\n if (existing && (existing.bitrate ?? 0) >= (level.bitrate ?? 0)) return;\n byHeight.set(level.height, {\n id: index,\n height: level.height,\n bitrate: level.bitrate || undefined,\n label: `${level.height}p`,\n });\n });\n return [...byHeight.values()].sort((a, b) => b.height - a.height);\n}\n\n/** Whether the options describe an HLS manifest rather than a video file. */\nexport function isHlsSource(\n options: Pick<HlsVideoAdapterOptions, \"src\" | \"type\">,\n): boolean {\n if (options.type === \"hls\") return true;\n if (options.type === \"progressive\") return false;\n let path = options.src;\n try {\n path = new URL(options.src, \"http://localhost\").pathname;\n } catch {\n // Not a URL: test the raw string.\n }\n return /\\.m3u8$/i.test(path);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,4BAAmC;AACnC,iBAAgE;AA+BhE,IAAM,OAAO;AACb,IAAM,WAAW;AAkBV,IAAM,kBAAN,cACG,yCAEV;AAAA,EACmB;AAAA,EACT,MAAkB;AAAA,EAClB,SAAyB,CAAC;AAAA,EAC1B,gBAAgB;AAAA,EAChB,cAA6B;AAAA,EAC7B,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,gBAAgB;AAAA,EAChB,mBAAmB,oBAAI,IAAc;AAAA,EAC5B,eAAe,MAAM;AAEpC,QAAI,CAAC,KAAK,IAAK,MAAK,iBAAiB;AAAA,EACvC;AAAA,EAEA,YAAY,OAAyB,SAAiC;AACpE,UAAM,KAAK;AACX,SAAK,UAAU;AACf,UAAM,iBAAiB,SAAS,KAAK,YAAY;AAEjD,QAAI,CAAC,YAAY,OAAO,GAAG;AAEzB,YAAM,MAAM,QAAQ;AAAA,IACtB,WAAW,WAAAA,QAAI,YAAY,GAAG;AAC5B,WAAK,UAAU;AAAA,IACjB,WAAW,MAAM,YAAY,QAAQ,GAAG;AACtC,YAAM,MAAM,QAAQ;AAAA,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,MAAM,gBAAgB;AACzD,SAAK,iBAAiB,QAAQ,CAAC,YAAY,QAAQ,KAAK,CAAC;AAAA,EAC3D;AACF;AAMA,SAAS,gBAAgB,QAAiC;AACxD,QAAM,WAAW,oBAAI,IAA0B;AAC/C,SAAO,QAAQ,CAAC,OAAO,UAAU;AAC/B,QAAI,CAAC,MAAM,OAAQ;AACnB,UAAM,WAAW,SAAS,IAAI,MAAM,MAAM;AAC1C,QAAI,aAAa,SAAS,WAAW,OAAO,MAAM,WAAW,GAAI;AACjE,aAAS,IAAI,MAAM,QAAQ;AAAA,MACzB,IAAI;AAAA,MACJ,QAAQ,MAAM;AAAA,MACd,SAAS,MAAM,WAAW;AAAA,MAC1B,OAAO,GAAG,MAAM,MAAM;AAAA,IACxB,CAAC;AAAA,EACH,CAAC;AACD,SAAO,CAAC,GAAG,SAAS,OAAO,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,SAAS,EAAE,MAAM;AAClE;AAGO,SAAS,YACd,SACS;AACT,MAAI,QAAQ,SAAS,MAAO,QAAO;AACnC,MAAI,QAAQ,SAAS,cAAe,QAAO;AAC3C,MAAI,OAAO,QAAQ;AACnB,MAAI;AACF,WAAO,IAAI,IAAI,QAAQ,KAAK,kBAAkB,EAAE;AAAA,EAClD,QAAQ;AAAA,EAER;AACA,SAAO,WAAW,KAAK,IAAI;AAC7B;","names":["Hls"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { QualityCapableAdapter, QualityLevel, VideoAdapterEventType, VideoAdapterEvent } from '@interlacejs/core';
|
|
2
|
+
import { NativeVideoAdapter } from '@interlacejs/native-adapter';
|
|
3
|
+
import Hls, { HlsConfig } from 'hls.js';
|
|
4
|
+
|
|
5
|
+
interface HlsVideoAdapterOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Video URL. HLS manifests (`.m3u8`) play through hls.js or the
|
|
8
|
+
* browser's native HLS; any other source (e.g. an MP4) plays directly in
|
|
9
|
+
* the `<video>` element, with no quality levels.
|
|
10
|
+
*/
|
|
11
|
+
src: string;
|
|
12
|
+
/**
|
|
13
|
+
* Source type. `"auto"` (default) treats URLs whose path ends in `.m3u8`
|
|
14
|
+
* as HLS; set `"hls"` for manifests served from other paths, or
|
|
15
|
+
* `"progressive"` to always play `src` directly.
|
|
16
|
+
*/
|
|
17
|
+
type?: "auto" | "hls" | "progressive";
|
|
18
|
+
/**
|
|
19
|
+
* Progressive source (e.g. an MP4) used when HLS cannot play: no hls.js
|
|
20
|
+
* support and no native HLS, or an unrecoverable playback error.
|
|
21
|
+
*/
|
|
22
|
+
fallbackSrc?: string;
|
|
23
|
+
/** Extra hls.js configuration. */
|
|
24
|
+
hlsConfig?: Partial<HlsConfig>;
|
|
25
|
+
/**
|
|
26
|
+
* localStorage key under which the viewer's quality choice is remembered
|
|
27
|
+
* (as a height, or "auto"). Not remembered when omitted.
|
|
28
|
+
*/
|
|
29
|
+
rememberQualityKey?: string;
|
|
30
|
+
}
|
|
31
|
+
type Listener = (event: VideoAdapterEvent) => void;
|
|
32
|
+
/**
|
|
33
|
+
* HLS video adapter. Extends {@link NativeVideoAdapter} (play/pause/seek,
|
|
34
|
+
* events, overlay mounting, fullscreen and destroy are reused) and adds
|
|
35
|
+
* quality levels via hls.js.
|
|
36
|
+
*
|
|
37
|
+
* - hls.js supported: plays through hls.js and exposes the stream's levels.
|
|
38
|
+
* - Native HLS (Safari/iOS): sets `video.src`; no levels are exposed.
|
|
39
|
+
* - Neither: plays `fallbackSrc`, if given.
|
|
40
|
+
*
|
|
41
|
+
* Usage:
|
|
42
|
+
* const adapter = new HlsVideoAdapter(video, {
|
|
43
|
+
* src: "https://cdn.example.com/lesson/master.m3u8",
|
|
44
|
+
* fallbackSrc: "https://cdn.example.com/lesson.mp4",
|
|
45
|
+
* rememberQualityKey: "my-app:quality",
|
|
46
|
+
* });
|
|
47
|
+
*/
|
|
48
|
+
declare class HlsVideoAdapter extends NativeVideoAdapter implements QualityCapableAdapter {
|
|
49
|
+
private readonly options;
|
|
50
|
+
private hls;
|
|
51
|
+
private levels;
|
|
52
|
+
private selectedLevel;
|
|
53
|
+
private activeLevel;
|
|
54
|
+
private manifestLoaded;
|
|
55
|
+
private networkRetried;
|
|
56
|
+
private mediaRecovered;
|
|
57
|
+
private usingFallback;
|
|
58
|
+
private qualityListeners;
|
|
59
|
+
private readonly onVideoError;
|
|
60
|
+
constructor(video: HTMLVideoElement, options: HlsVideoAdapterOptions);
|
|
61
|
+
getQualityLevels(): QualityLevel[];
|
|
62
|
+
getQualityLevel(): number;
|
|
63
|
+
getActiveQualityLevel(): number | null;
|
|
64
|
+
setQualityLevel(id: number): void;
|
|
65
|
+
on(eventType: VideoAdapterEventType, handler: Listener): () => void;
|
|
66
|
+
/** The hls.js instance while HLS plays through hls.js, else `null`. */
|
|
67
|
+
getHls(): Hls | null;
|
|
68
|
+
destroy(): void;
|
|
69
|
+
private attachHls;
|
|
70
|
+
private handleHlsError;
|
|
71
|
+
/**
|
|
72
|
+
* Gives up on HLS: tears hls.js down and plays `fallbackSrc` from the
|
|
73
|
+
* same position, keeping the paused state.
|
|
74
|
+
*/
|
|
75
|
+
private switchToFallback;
|
|
76
|
+
private applyRememberedPreference;
|
|
77
|
+
private readPreference;
|
|
78
|
+
private rememberPreference;
|
|
79
|
+
/** Maps an hls.js level index to the id of the exposed level of the same height. */
|
|
80
|
+
private toExposedId;
|
|
81
|
+
private emitQualityChange;
|
|
82
|
+
}
|
|
83
|
+
/** Whether the options describe an HLS manifest rather than a video file. */
|
|
84
|
+
declare function isHlsSource(options: Pick<HlsVideoAdapterOptions, "src" | "type">): boolean;
|
|
85
|
+
|
|
86
|
+
export { HlsVideoAdapter, type HlsVideoAdapterOptions, isHlsSource };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { QualityCapableAdapter, QualityLevel, VideoAdapterEventType, VideoAdapterEvent } from '@interlacejs/core';
|
|
2
|
+
import { NativeVideoAdapter } from '@interlacejs/native-adapter';
|
|
3
|
+
import Hls, { HlsConfig } from 'hls.js';
|
|
4
|
+
|
|
5
|
+
interface HlsVideoAdapterOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Video URL. HLS manifests (`.m3u8`) play through hls.js or the
|
|
8
|
+
* browser's native HLS; any other source (e.g. an MP4) plays directly in
|
|
9
|
+
* the `<video>` element, with no quality levels.
|
|
10
|
+
*/
|
|
11
|
+
src: string;
|
|
12
|
+
/**
|
|
13
|
+
* Source type. `"auto"` (default) treats URLs whose path ends in `.m3u8`
|
|
14
|
+
* as HLS; set `"hls"` for manifests served from other paths, or
|
|
15
|
+
* `"progressive"` to always play `src` directly.
|
|
16
|
+
*/
|
|
17
|
+
type?: "auto" | "hls" | "progressive";
|
|
18
|
+
/**
|
|
19
|
+
* Progressive source (e.g. an MP4) used when HLS cannot play: no hls.js
|
|
20
|
+
* support and no native HLS, or an unrecoverable playback error.
|
|
21
|
+
*/
|
|
22
|
+
fallbackSrc?: string;
|
|
23
|
+
/** Extra hls.js configuration. */
|
|
24
|
+
hlsConfig?: Partial<HlsConfig>;
|
|
25
|
+
/**
|
|
26
|
+
* localStorage key under which the viewer's quality choice is remembered
|
|
27
|
+
* (as a height, or "auto"). Not remembered when omitted.
|
|
28
|
+
*/
|
|
29
|
+
rememberQualityKey?: string;
|
|
30
|
+
}
|
|
31
|
+
type Listener = (event: VideoAdapterEvent) => void;
|
|
32
|
+
/**
|
|
33
|
+
* HLS video adapter. Extends {@link NativeVideoAdapter} (play/pause/seek,
|
|
34
|
+
* events, overlay mounting, fullscreen and destroy are reused) and adds
|
|
35
|
+
* quality levels via hls.js.
|
|
36
|
+
*
|
|
37
|
+
* - hls.js supported: plays through hls.js and exposes the stream's levels.
|
|
38
|
+
* - Native HLS (Safari/iOS): sets `video.src`; no levels are exposed.
|
|
39
|
+
* - Neither: plays `fallbackSrc`, if given.
|
|
40
|
+
*
|
|
41
|
+
* Usage:
|
|
42
|
+
* const adapter = new HlsVideoAdapter(video, {
|
|
43
|
+
* src: "https://cdn.example.com/lesson/master.m3u8",
|
|
44
|
+
* fallbackSrc: "https://cdn.example.com/lesson.mp4",
|
|
45
|
+
* rememberQualityKey: "my-app:quality",
|
|
46
|
+
* });
|
|
47
|
+
*/
|
|
48
|
+
declare class HlsVideoAdapter extends NativeVideoAdapter implements QualityCapableAdapter {
|
|
49
|
+
private readonly options;
|
|
50
|
+
private hls;
|
|
51
|
+
private levels;
|
|
52
|
+
private selectedLevel;
|
|
53
|
+
private activeLevel;
|
|
54
|
+
private manifestLoaded;
|
|
55
|
+
private networkRetried;
|
|
56
|
+
private mediaRecovered;
|
|
57
|
+
private usingFallback;
|
|
58
|
+
private qualityListeners;
|
|
59
|
+
private readonly onVideoError;
|
|
60
|
+
constructor(video: HTMLVideoElement, options: HlsVideoAdapterOptions);
|
|
61
|
+
getQualityLevels(): QualityLevel[];
|
|
62
|
+
getQualityLevel(): number;
|
|
63
|
+
getActiveQualityLevel(): number | null;
|
|
64
|
+
setQualityLevel(id: number): void;
|
|
65
|
+
on(eventType: VideoAdapterEventType, handler: Listener): () => void;
|
|
66
|
+
/** The hls.js instance while HLS plays through hls.js, else `null`. */
|
|
67
|
+
getHls(): Hls | null;
|
|
68
|
+
destroy(): void;
|
|
69
|
+
private attachHls;
|
|
70
|
+
private handleHlsError;
|
|
71
|
+
/**
|
|
72
|
+
* Gives up on HLS: tears hls.js down and plays `fallbackSrc` from the
|
|
73
|
+
* same position, keeping the paused state.
|
|
74
|
+
*/
|
|
75
|
+
private switchToFallback;
|
|
76
|
+
private applyRememberedPreference;
|
|
77
|
+
private readPreference;
|
|
78
|
+
private rememberPreference;
|
|
79
|
+
/** Maps an hls.js level index to the id of the exposed level of the same height. */
|
|
80
|
+
private toExposedId;
|
|
81
|
+
private emitQualityChange;
|
|
82
|
+
}
|
|
83
|
+
/** Whether the options describe an HLS manifest rather than a video file. */
|
|
84
|
+
declare function isHlsSource(options: Pick<HlsVideoAdapterOptions, "src" | "type">): boolean;
|
|
85
|
+
|
|
86
|
+
export { HlsVideoAdapter, type HlsVideoAdapterOptions, isHlsSource };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// src/hls-video-adapter.ts
|
|
2
|
+
import { NativeVideoAdapter } from "@interlacejs/native-adapter";
|
|
3
|
+
import Hls from "hls.js";
|
|
4
|
+
var AUTO = -1;
|
|
5
|
+
var HLS_MIME = "application/vnd.apple.mpegurl";
|
|
6
|
+
var HlsVideoAdapter = class extends NativeVideoAdapter {
|
|
7
|
+
options;
|
|
8
|
+
hls = null;
|
|
9
|
+
levels = [];
|
|
10
|
+
selectedLevel = AUTO;
|
|
11
|
+
activeLevel = null;
|
|
12
|
+
manifestLoaded = false;
|
|
13
|
+
networkRetried = false;
|
|
14
|
+
mediaRecovered = false;
|
|
15
|
+
usingFallback = false;
|
|
16
|
+
qualityListeners = /* @__PURE__ */ new Set();
|
|
17
|
+
onVideoError = () => {
|
|
18
|
+
if (!this.hls) this.switchToFallback();
|
|
19
|
+
};
|
|
20
|
+
constructor(video, options) {
|
|
21
|
+
super(video);
|
|
22
|
+
this.options = options;
|
|
23
|
+
video.addEventListener("error", this.onVideoError);
|
|
24
|
+
if (!isHlsSource(options)) {
|
|
25
|
+
video.src = options.src;
|
|
26
|
+
} else if (Hls.isSupported()) {
|
|
27
|
+
this.attachHls();
|
|
28
|
+
} else if (video.canPlayType(HLS_MIME)) {
|
|
29
|
+
video.src = options.src;
|
|
30
|
+
} else if (options.fallbackSrc) {
|
|
31
|
+
this.usingFallback = true;
|
|
32
|
+
video.src = options.fallbackSrc;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
getQualityLevels() {
|
|
36
|
+
return this.levels.map((level) => ({ ...level }));
|
|
37
|
+
}
|
|
38
|
+
getQualityLevel() {
|
|
39
|
+
return this.selectedLevel;
|
|
40
|
+
}
|
|
41
|
+
getActiveQualityLevel() {
|
|
42
|
+
return this.activeLevel;
|
|
43
|
+
}
|
|
44
|
+
setQualityLevel(id) {
|
|
45
|
+
if (!this.hls) return;
|
|
46
|
+
const level = this.levels.find((l) => l.id === id);
|
|
47
|
+
const next = level ? level.id : AUTO;
|
|
48
|
+
this.selectedLevel = next;
|
|
49
|
+
this.hls.currentLevel = next;
|
|
50
|
+
this.rememberPreference(level ? String(level.height) : "auto");
|
|
51
|
+
this.emitQualityChange();
|
|
52
|
+
}
|
|
53
|
+
on(eventType, handler) {
|
|
54
|
+
if (eventType !== "qualitychange") return super.on(eventType, handler);
|
|
55
|
+
this.qualityListeners.add(handler);
|
|
56
|
+
return () => {
|
|
57
|
+
this.qualityListeners.delete(handler);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** The hls.js instance while HLS plays through hls.js, else `null`. */
|
|
61
|
+
getHls() {
|
|
62
|
+
return this.hls;
|
|
63
|
+
}
|
|
64
|
+
destroy() {
|
|
65
|
+
this.getVideoElement().removeEventListener("error", this.onVideoError);
|
|
66
|
+
this.hls?.destroy();
|
|
67
|
+
this.hls = null;
|
|
68
|
+
this.qualityListeners.clear();
|
|
69
|
+
super.destroy();
|
|
70
|
+
}
|
|
71
|
+
attachHls() {
|
|
72
|
+
const hls = new Hls(this.options.hlsConfig);
|
|
73
|
+
this.hls = hls;
|
|
74
|
+
hls.on(Hls.Events.MANIFEST_PARSED, (_event, data) => {
|
|
75
|
+
this.manifestLoaded = true;
|
|
76
|
+
this.levels = toQualityLevels(data.levels);
|
|
77
|
+
this.applyRememberedPreference();
|
|
78
|
+
this.emitQualityChange();
|
|
79
|
+
});
|
|
80
|
+
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, data) => {
|
|
81
|
+
this.activeLevel = this.toExposedId(data.level);
|
|
82
|
+
this.emitQualityChange();
|
|
83
|
+
});
|
|
84
|
+
hls.on(Hls.Events.ERROR, (_event, data) => this.handleHlsError(data));
|
|
85
|
+
hls.attachMedia(this.getVideoElement());
|
|
86
|
+
hls.loadSource(this.options.src);
|
|
87
|
+
}
|
|
88
|
+
handleHlsError(data) {
|
|
89
|
+
if (!data.fatal || !this.hls) return;
|
|
90
|
+
if (data.type === Hls.ErrorTypes.NETWORK_ERROR && this.manifestLoaded && !this.networkRetried) {
|
|
91
|
+
this.networkRetried = true;
|
|
92
|
+
this.hls.startLoad();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (data.type === Hls.ErrorTypes.MEDIA_ERROR && !this.mediaRecovered) {
|
|
96
|
+
this.mediaRecovered = true;
|
|
97
|
+
this.hls.recoverMediaError();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.switchToFallback();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Gives up on HLS: tears hls.js down and plays `fallbackSrc` from the
|
|
104
|
+
* same position, keeping the paused state.
|
|
105
|
+
*/
|
|
106
|
+
switchToFallback() {
|
|
107
|
+
const { fallbackSrc } = this.options;
|
|
108
|
+
const video = this.getVideoElement();
|
|
109
|
+
const time = video.currentTime;
|
|
110
|
+
const wasPaused = video.paused;
|
|
111
|
+
if (this.hls) {
|
|
112
|
+
this.hls.destroy();
|
|
113
|
+
this.hls = null;
|
|
114
|
+
}
|
|
115
|
+
this.levels = [];
|
|
116
|
+
this.selectedLevel = AUTO;
|
|
117
|
+
this.activeLevel = null;
|
|
118
|
+
this.emitQualityChange();
|
|
119
|
+
if (!fallbackSrc || this.usingFallback) return;
|
|
120
|
+
this.usingFallback = true;
|
|
121
|
+
video.addEventListener(
|
|
122
|
+
"loadedmetadata",
|
|
123
|
+
() => {
|
|
124
|
+
if (time > 0) void this.seek(time);
|
|
125
|
+
if (!wasPaused) {
|
|
126
|
+
Promise.resolve().then(() => video.play()).catch((err) => {
|
|
127
|
+
console.error("Failed to resume fallback video:", err);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
{ once: true }
|
|
132
|
+
);
|
|
133
|
+
video.src = fallbackSrc;
|
|
134
|
+
}
|
|
135
|
+
applyRememberedPreference() {
|
|
136
|
+
const stored = this.readPreference();
|
|
137
|
+
if (!stored || stored === "auto" || !this.hls) return;
|
|
138
|
+
const height = Number(stored);
|
|
139
|
+
if (!Number.isFinite(height) || this.levels.length === 0) return;
|
|
140
|
+
const level = this.levels.find((l) => l.height <= height) ?? this.levels[this.levels.length - 1];
|
|
141
|
+
this.selectedLevel = level.id;
|
|
142
|
+
this.hls.currentLevel = level.id;
|
|
143
|
+
}
|
|
144
|
+
readPreference() {
|
|
145
|
+
const key = this.options.rememberQualityKey;
|
|
146
|
+
if (!key) return null;
|
|
147
|
+
try {
|
|
148
|
+
return window.localStorage.getItem(key);
|
|
149
|
+
} catch {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
rememberPreference(value) {
|
|
154
|
+
const key = this.options.rememberQualityKey;
|
|
155
|
+
if (!key) return;
|
|
156
|
+
try {
|
|
157
|
+
window.localStorage.setItem(key, value);
|
|
158
|
+
} catch {
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Maps an hls.js level index to the id of the exposed level of the same height. */
|
|
162
|
+
toExposedId(hlsIndex) {
|
|
163
|
+
const height = this.hls?.levels[hlsIndex]?.height;
|
|
164
|
+
if (height === void 0) return null;
|
|
165
|
+
return this.levels.find((l) => l.height === height)?.id ?? null;
|
|
166
|
+
}
|
|
167
|
+
emitQualityChange() {
|
|
168
|
+
const event = { type: "qualitychange" };
|
|
169
|
+
this.qualityListeners.forEach((handler) => handler(event));
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function toQualityLevels(levels) {
|
|
173
|
+
const byHeight = /* @__PURE__ */ new Map();
|
|
174
|
+
levels.forEach((level, index) => {
|
|
175
|
+
if (!level.height) return;
|
|
176
|
+
const existing = byHeight.get(level.height);
|
|
177
|
+
if (existing && (existing.bitrate ?? 0) >= (level.bitrate ?? 0)) return;
|
|
178
|
+
byHeight.set(level.height, {
|
|
179
|
+
id: index,
|
|
180
|
+
height: level.height,
|
|
181
|
+
bitrate: level.bitrate || void 0,
|
|
182
|
+
label: `${level.height}p`
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
return [...byHeight.values()].sort((a, b) => b.height - a.height);
|
|
186
|
+
}
|
|
187
|
+
function isHlsSource(options) {
|
|
188
|
+
if (options.type === "hls") return true;
|
|
189
|
+
if (options.type === "progressive") return false;
|
|
190
|
+
let path = options.src;
|
|
191
|
+
try {
|
|
192
|
+
path = new URL(options.src, "http://localhost").pathname;
|
|
193
|
+
} catch {
|
|
194
|
+
}
|
|
195
|
+
return /\.m3u8$/i.test(path);
|
|
196
|
+
}
|
|
197
|
+
export {
|
|
198
|
+
HlsVideoAdapter,
|
|
199
|
+
isHlsSource
|
|
200
|
+
};
|
|
201
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hls-video-adapter.ts"],"sourcesContent":["import type {\n QualityCapableAdapter,\n QualityLevel,\n VideoAdapterEvent,\n VideoAdapterEventType,\n} from \"@interlacejs/core\";\nimport { NativeVideoAdapter } from \"@interlacejs/native-adapter\";\nimport Hls, { type ErrorData, type HlsConfig, type Level } from \"hls.js\";\n\nexport interface HlsVideoAdapterOptions {\n /**\n * Video URL. HLS manifests (`.m3u8`) play through hls.js or the\n * browser's native HLS; any other source (e.g. an MP4) plays directly in\n * the `<video>` element, with no quality levels.\n */\n src: string;\n /**\n * Source type. `\"auto\"` (default) treats URLs whose path ends in `.m3u8`\n * as HLS; set `\"hls\"` for manifests served from other paths, or\n * `\"progressive\"` to always play `src` directly.\n */\n type?: \"auto\" | \"hls\" | \"progressive\";\n /**\n * Progressive source (e.g. an MP4) used when HLS cannot play: no hls.js\n * support and no native HLS, or an unrecoverable playback error.\n */\n fallbackSrc?: string;\n /** Extra hls.js configuration. */\n hlsConfig?: Partial<HlsConfig>;\n /**\n * localStorage key under which the viewer's quality choice is remembered\n * (as a height, or \"auto\"). Not remembered when omitted.\n */\n rememberQualityKey?: string;\n}\n\ntype Listener = (event: VideoAdapterEvent) => void;\n\nconst AUTO = -1;\nconst HLS_MIME = \"application/vnd.apple.mpegurl\";\n\n/**\n * HLS video adapter. Extends {@link NativeVideoAdapter} (play/pause/seek,\n * events, overlay mounting, fullscreen and destroy are reused) and adds\n * quality levels via hls.js.\n *\n * - hls.js supported: plays through hls.js and exposes the stream's levels.\n * - Native HLS (Safari/iOS): sets `video.src`; no levels are exposed.\n * - Neither: plays `fallbackSrc`, if given.\n *\n * Usage:\n * const adapter = new HlsVideoAdapter(video, {\n * src: \"https://cdn.example.com/lesson/master.m3u8\",\n * fallbackSrc: \"https://cdn.example.com/lesson.mp4\",\n * rememberQualityKey: \"my-app:quality\",\n * });\n */\nexport class HlsVideoAdapter\n extends NativeVideoAdapter\n implements QualityCapableAdapter\n{\n private readonly options: HlsVideoAdapterOptions;\n private hls: Hls | null = null;\n private levels: QualityLevel[] = [];\n private selectedLevel = AUTO;\n private activeLevel: number | null = null;\n private manifestLoaded = false;\n private networkRetried = false;\n private mediaRecovered = false;\n private usingFallback = false;\n private qualityListeners = new Set<Listener>();\n private readonly onVideoError = () => {\n // Only the native path reports failures here; hls.js reports its own.\n if (!this.hls) this.switchToFallback();\n };\n\n constructor(video: HTMLVideoElement, options: HlsVideoAdapterOptions) {\n super(video);\n this.options = options;\n video.addEventListener(\"error\", this.onVideoError);\n\n if (!isHlsSource(options)) {\n // Plain video file: play it as-is (one quality, no menu).\n video.src = options.src;\n } else if (Hls.isSupported()) {\n this.attachHls();\n } else if (video.canPlayType(HLS_MIME)) {\n video.src = options.src;\n } else if (options.fallbackSrc) {\n this.usingFallback = true;\n video.src = options.fallbackSrc;\n }\n }\n\n getQualityLevels(): QualityLevel[] {\n return this.levels.map((level) => ({ ...level }));\n }\n\n getQualityLevel(): number {\n return this.selectedLevel;\n }\n\n getActiveQualityLevel(): number | null {\n return this.activeLevel;\n }\n\n setQualityLevel(id: number): void {\n if (!this.hls) return;\n const level = this.levels.find((l) => l.id === id);\n const next = level ? level.id : AUTO;\n this.selectedLevel = next;\n this.hls.currentLevel = next;\n this.rememberPreference(level ? String(level.height) : \"auto\");\n this.emitQualityChange();\n }\n\n on(eventType: VideoAdapterEventType, handler: Listener): () => void {\n if (eventType !== \"qualitychange\") return super.on(eventType, handler);\n this.qualityListeners.add(handler);\n return () => {\n this.qualityListeners.delete(handler);\n };\n }\n\n /** The hls.js instance while HLS plays through hls.js, else `null`. */\n getHls(): Hls | null {\n return this.hls;\n }\n\n destroy(): void {\n this.getVideoElement().removeEventListener(\"error\", this.onVideoError);\n this.hls?.destroy();\n this.hls = null;\n this.qualityListeners.clear();\n super.destroy();\n }\n\n private attachHls(): void {\n const hls = new Hls(this.options.hlsConfig);\n this.hls = hls;\n\n hls.on(Hls.Events.MANIFEST_PARSED, (_event, data) => {\n this.manifestLoaded = true;\n this.levels = toQualityLevels(data.levels);\n this.applyRememberedPreference();\n this.emitQualityChange();\n });\n\n hls.on(Hls.Events.LEVEL_SWITCHED, (_event, data) => {\n this.activeLevel = this.toExposedId(data.level);\n this.emitQualityChange();\n });\n\n hls.on(Hls.Events.ERROR, (_event, data) => this.handleHlsError(data));\n\n hls.attachMedia(this.getVideoElement());\n hls.loadSource(this.options.src);\n }\n\n private handleHlsError(data: ErrorData): void {\n if (!data.fatal || !this.hls) return;\n if (\n data.type === Hls.ErrorTypes.NETWORK_ERROR &&\n this.manifestLoaded &&\n !this.networkRetried\n ) {\n this.networkRetried = true;\n this.hls.startLoad();\n return;\n }\n if (data.type === Hls.ErrorTypes.MEDIA_ERROR && !this.mediaRecovered) {\n this.mediaRecovered = true;\n this.hls.recoverMediaError();\n return;\n }\n this.switchToFallback();\n }\n\n /**\n * Gives up on HLS: tears hls.js down and plays `fallbackSrc` from the\n * same position, keeping the paused state.\n */\n private switchToFallback(): void {\n const { fallbackSrc } = this.options;\n const video = this.getVideoElement();\n const time = video.currentTime;\n const wasPaused = video.paused;\n\n if (this.hls) {\n this.hls.destroy();\n this.hls = null;\n }\n this.levels = [];\n this.selectedLevel = AUTO;\n this.activeLevel = null;\n this.emitQualityChange();\n\n if (!fallbackSrc || this.usingFallback) return;\n this.usingFallback = true;\n video.addEventListener(\n \"loadedmetadata\",\n () => {\n if (time > 0) void this.seek(time);\n if (!wasPaused) {\n Promise.resolve()\n .then(() => video.play())\n .catch((err: unknown) => {\n console.error(\"Failed to resume fallback video:\", err);\n });\n }\n },\n { once: true },\n );\n video.src = fallbackSrc;\n }\n\n private applyRememberedPreference(): void {\n const stored = this.readPreference();\n if (!stored || stored === \"auto\" || !this.hls) return;\n const height = Number(stored);\n if (!Number.isFinite(height) || this.levels.length === 0) return;\n // Levels are sorted high → low: take the first at or below the stored\n // height, else the lowest available.\n const level =\n this.levels.find((l) => l.height <= height) ??\n this.levels[this.levels.length - 1]!;\n this.selectedLevel = level.id;\n this.hls.currentLevel = level.id;\n }\n\n private readPreference(): string | null {\n const key = this.options.rememberQualityKey;\n if (!key) return null;\n try {\n return window.localStorage.getItem(key);\n } catch {\n return null;\n }\n }\n\n private rememberPreference(value: string): void {\n const key = this.options.rememberQualityKey;\n if (!key) return;\n try {\n window.localStorage.setItem(key, value);\n } catch {\n // Storage unavailable (private mode, quota): the choice is not kept.\n }\n }\n\n /** Maps an hls.js level index to the id of the exposed level of the same height. */\n private toExposedId(hlsIndex: number): number | null {\n const height = this.hls?.levels[hlsIndex]?.height;\n if (height === undefined) return null;\n return this.levels.find((l) => l.height === height)?.id ?? null;\n }\n\n private emitQualityChange(): void {\n const event: VideoAdapterEvent = { type: \"qualitychange\" };\n this.qualityListeners.forEach((handler) => handler(event));\n }\n}\n\n/**\n * One level per height (highest bitrate wins), sorted high → low. Ids are\n * the hls.js level indices, so they can be assigned to `hls.currentLevel`.\n */\nfunction toQualityLevels(levels: Level[]): QualityLevel[] {\n const byHeight = new Map<number, QualityLevel>();\n levels.forEach((level, index) => {\n if (!level.height) return;\n const existing = byHeight.get(level.height);\n if (existing && (existing.bitrate ?? 0) >= (level.bitrate ?? 0)) return;\n byHeight.set(level.height, {\n id: index,\n height: level.height,\n bitrate: level.bitrate || undefined,\n label: `${level.height}p`,\n });\n });\n return [...byHeight.values()].sort((a, b) => b.height - a.height);\n}\n\n/** Whether the options describe an HLS manifest rather than a video file. */\nexport function isHlsSource(\n options: Pick<HlsVideoAdapterOptions, \"src\" | \"type\">,\n): boolean {\n if (options.type === \"hls\") return true;\n if (options.type === \"progressive\") return false;\n let path = options.src;\n try {\n path = new URL(options.src, \"http://localhost\").pathname;\n } catch {\n // Not a URL: test the raw string.\n }\n return /\\.m3u8$/i.test(path);\n}\n"],"mappings":";AAMA,SAAS,0BAA0B;AACnC,OAAO,SAAyD;AA+BhE,IAAM,OAAO;AACb,IAAM,WAAW;AAkBV,IAAM,kBAAN,cACG,mBAEV;AAAA,EACmB;AAAA,EACT,MAAkB;AAAA,EAClB,SAAyB,CAAC;AAAA,EAC1B,gBAAgB;AAAA,EAChB,cAA6B;AAAA,EAC7B,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,gBAAgB;AAAA,EAChB,mBAAmB,oBAAI,IAAc;AAAA,EAC5B,eAAe,MAAM;AAEpC,QAAI,CAAC,KAAK,IAAK,MAAK,iBAAiB;AAAA,EACvC;AAAA,EAEA,YAAY,OAAyB,SAAiC;AACpE,UAAM,KAAK;AACX,SAAK,UAAU;AACf,UAAM,iBAAiB,SAAS,KAAK,YAAY;AAEjD,QAAI,CAAC,YAAY,OAAO,GAAG;AAEzB,YAAM,MAAM,QAAQ;AAAA,IACtB,WAAW,IAAI,YAAY,GAAG;AAC5B,WAAK,UAAU;AAAA,IACjB,WAAW,MAAM,YAAY,QAAQ,GAAG;AACtC,YAAM,MAAM,QAAQ;AAAA,IACtB,WAAW,QAAQ,aAAa;AAC9B,WAAK,gBAAgB;AACrB,YAAM,MAAM,QAAQ;AAAA,IACtB;AAAA,EACF;AAAA,EAEA,mBAAmC;AACjC,WAAO,KAAK,OAAO,I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,UAAU;AAC/B,QAAI,CAAC,MAAM,OAAQ;AACnB,UAAM,WAAW,SAAS,IAAI,MAAM,MAAM;AAC1C,QAAI,aAAa,SAAS,WAAW,OAAO,MAAM,WAAW,GAAI;AACjE,aAAS,IAAI,MAAM,QAAQ;AAAA,MACzB,IAAI;AAAA,MACJ,QAAQ,MAAM;AAAA,MACd,SAAS,MAAM,WAAW;AAAA,MAC1B,OAAO,GAAG,MAAM,MAAM;AAAA,IACxB,CAAC;AAAA,EACH,CAAC;AACD,SAAO,CAAC,GAAG,SAAS,OAAO,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,SAAS,EAAE,MAAM;AAClE;AAGO,SAAS,YACd,SACS;AACT,MAAI,QAAQ,SAAS,MAAO,QAAO;AACnC,MAAI,QAAQ,SAAS,cAAe,QAAO;AAC3C,MAAI,OAAO,QAAQ;AACnB,MAAI;AACF,WAAO,IAAI,IAAI,QAAQ,KAAK,kBAAkB,EAAE;AAAA,EAClD,QAAQ;AAAA,EAER;AACA,SAAO,WAAW,KAAK,IAAI;AAC7B;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interlacejs/hls-adapter",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "HLS VideoAdapter for Interlace: hls.js playback with selectable quality levels, native HLS and MP4 fallback.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/**"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"interlace",
|
|
22
|
+
"interactive-video",
|
|
23
|
+
"adapter",
|
|
24
|
+
"hls",
|
|
25
|
+
"edtech",
|
|
26
|
+
"lms"
|
|
27
|
+
],
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "Joshua Farayola",
|
|
30
|
+
"email": "farayolajoshua@gmail.com"
|
|
31
|
+
},
|
|
32
|
+
"license": "ISC",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"hls.js": "^1.5.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@repo/eslint-config": "^0.0.0",
|
|
41
|
+
"@repo/typescript-config": "^0.0.0",
|
|
42
|
+
"hls.js": "^1.5.20",
|
|
43
|
+
"jsdom": "^25.0.1",
|
|
44
|
+
"tsup": "^8.5.1",
|
|
45
|
+
"vitest": "^2.1.9"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@interlacejs/core": "0.3.0",
|
|
49
|
+
"@interlacejs/native-adapter": "0.0.6"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"dev": "tsup --watch",
|
|
54
|
+
"lint": "eslint \"src/**/*.ts*\"",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
57
|
+
}
|
|
6
58
|
}
|