@scarlett-player/hls 1.1.1 → 1.4.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 +59 -11
- package/dist/chunk-IFUZZQQE.js +1141 -0
- package/dist/index.cjs +202 -53
- package/dist/index.d.cts +6 -8
- package/dist/index.d.ts +6 -8
- package/dist/index.js +34 -691
- package/dist/light.cjs +431 -43
- package/dist/light.d.cts +11 -4
- package/dist/light.d.ts +11 -4
- package/dist/light.js +34 -461
- package/dist/{types-DOzoPHQe.d.cts → types-D-HW4lmM.d.cts} +8 -0
- package/dist/{types-DOzoPHQe.d.ts → types-D-HW4lmM.d.ts} +8 -0
- package/package.json +2 -2
- package/dist/chunk-L73PRXS4.js +0 -337
package/dist/chunk-L73PRXS4.js
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
// src/quality.ts
|
|
2
|
-
function formatLevel(level) {
|
|
3
|
-
if (level.name) {
|
|
4
|
-
return level.name;
|
|
5
|
-
}
|
|
6
|
-
if (level.height) {
|
|
7
|
-
const standardLabels = {
|
|
8
|
-
2160: "4K",
|
|
9
|
-
1440: "1440p",
|
|
10
|
-
1080: "1080p",
|
|
11
|
-
720: "720p",
|
|
12
|
-
480: "480p",
|
|
13
|
-
360: "360p",
|
|
14
|
-
240: "240p",
|
|
15
|
-
144: "144p"
|
|
16
|
-
};
|
|
17
|
-
const closest = Object.keys(standardLabels).map(Number).sort((a, b) => Math.abs(a - level.height) - Math.abs(b - level.height))[0];
|
|
18
|
-
if (Math.abs(closest - level.height) <= 20) {
|
|
19
|
-
return standardLabels[closest];
|
|
20
|
-
}
|
|
21
|
-
return `${level.height}p`;
|
|
22
|
-
}
|
|
23
|
-
if (level.bitrate) {
|
|
24
|
-
return formatBitrate(level.bitrate);
|
|
25
|
-
}
|
|
26
|
-
return "Unknown";
|
|
27
|
-
}
|
|
28
|
-
function formatBitrate(bitrate) {
|
|
29
|
-
if (bitrate >= 1e6) {
|
|
30
|
-
return `${(bitrate / 1e6).toFixed(1)} Mbps`;
|
|
31
|
-
}
|
|
32
|
-
if (bitrate >= 1e3) {
|
|
33
|
-
return `${Math.round(bitrate / 1e3)} Kbps`;
|
|
34
|
-
}
|
|
35
|
-
return `${bitrate} bps`;
|
|
36
|
-
}
|
|
37
|
-
function mapLevels(levels, _currentLevel) {
|
|
38
|
-
return levels.map((level, index) => ({
|
|
39
|
-
index,
|
|
40
|
-
width: level.width || 0,
|
|
41
|
-
height: level.height || 0,
|
|
42
|
-
bitrate: level.bitrate || 0,
|
|
43
|
-
label: formatLevel(level),
|
|
44
|
-
codec: level.codecSet
|
|
45
|
-
}));
|
|
46
|
-
}
|
|
47
|
-
function getInitialBandwidthEstimate(overrideBps) {
|
|
48
|
-
const HLS_DEFAULT_ESTIMATE = 5e5;
|
|
49
|
-
if (overrideBps !== void 0 && overrideBps > 0) {
|
|
50
|
-
return overrideBps;
|
|
51
|
-
}
|
|
52
|
-
const connection = navigator.connection;
|
|
53
|
-
if (connection?.downlink && connection.downlink > 0) {
|
|
54
|
-
const bps = connection.downlink * 1e6;
|
|
55
|
-
return Math.round(bps * 0.85);
|
|
56
|
-
}
|
|
57
|
-
return HLS_DEFAULT_ESTIMATE;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/event-map.ts
|
|
61
|
-
var HLS_ERROR_TYPES = {
|
|
62
|
-
NETWORK_ERROR: "networkError",
|
|
63
|
-
MEDIA_ERROR: "mediaError",
|
|
64
|
-
KEY_SYSTEM_ERROR: "keySystemError",
|
|
65
|
-
MUX_ERROR: "muxError",
|
|
66
|
-
OTHER_ERROR: "otherError"
|
|
67
|
-
};
|
|
68
|
-
function mapErrorType(hlsType) {
|
|
69
|
-
switch (hlsType) {
|
|
70
|
-
case HLS_ERROR_TYPES.NETWORK_ERROR:
|
|
71
|
-
return "network";
|
|
72
|
-
case HLS_ERROR_TYPES.MEDIA_ERROR:
|
|
73
|
-
return "media";
|
|
74
|
-
case HLS_ERROR_TYPES.MUX_ERROR:
|
|
75
|
-
return "mux";
|
|
76
|
-
default:
|
|
77
|
-
return "other";
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
function parseHlsError(data) {
|
|
81
|
-
return {
|
|
82
|
-
type: mapErrorType(data.type),
|
|
83
|
-
details: data.details || "Unknown error",
|
|
84
|
-
fatal: data.fatal || false,
|
|
85
|
-
url: data.url,
|
|
86
|
-
reason: data.reason,
|
|
87
|
-
response: data.response
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function setupHlsEventHandlers(hls, api, callbacks) {
|
|
91
|
-
const handlers = [];
|
|
92
|
-
const addHandler = (event, handler) => {
|
|
93
|
-
hls.on(event, handler);
|
|
94
|
-
handlers.push({ event, handler });
|
|
95
|
-
};
|
|
96
|
-
addHandler("hlsManifestParsed", (_event, data) => {
|
|
97
|
-
api.logger.debug("HLS manifest parsed", { levels: data.levels.length });
|
|
98
|
-
const levels = data.levels.map((level, index) => ({
|
|
99
|
-
id: `level-${index}`,
|
|
100
|
-
label: formatLevel(level),
|
|
101
|
-
width: level.width,
|
|
102
|
-
height: level.height,
|
|
103
|
-
bitrate: level.bitrate,
|
|
104
|
-
active: index === hls.currentLevel
|
|
105
|
-
}));
|
|
106
|
-
api.setState("qualities", levels);
|
|
107
|
-
api.emit("quality:levels", {
|
|
108
|
-
levels: levels.map((l) => ({ id: l.id, label: l.label }))
|
|
109
|
-
});
|
|
110
|
-
callbacks.onManifestParsed?.(data.levels);
|
|
111
|
-
});
|
|
112
|
-
addHandler("hlsLevelSwitched", (_event, data) => {
|
|
113
|
-
const level = hls.levels[data.level];
|
|
114
|
-
const isAuto = callbacks.getIsAutoQuality?.() ?? hls.autoLevelEnabled;
|
|
115
|
-
api.logger.debug("HLS level switched", { level: data.level, height: level?.height, auto: isAuto });
|
|
116
|
-
if (level) {
|
|
117
|
-
const label = isAuto ? `Auto (${formatLevel(level)})` : formatLevel(level);
|
|
118
|
-
api.setState("currentQuality", {
|
|
119
|
-
id: isAuto ? "auto" : `level-${data.level}`,
|
|
120
|
-
label,
|
|
121
|
-
width: level.width,
|
|
122
|
-
height: level.height,
|
|
123
|
-
bitrate: level.bitrate,
|
|
124
|
-
active: true
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
api.emit("quality:change", {
|
|
128
|
-
quality: level ? formatLevel(level) : "auto",
|
|
129
|
-
auto: isAuto
|
|
130
|
-
});
|
|
131
|
-
callbacks.onLevelSwitched?.(data.level);
|
|
132
|
-
});
|
|
133
|
-
let lastBandwidthUpdate = 0;
|
|
134
|
-
addHandler("hlsFragLoaded", () => {
|
|
135
|
-
const now = Date.now();
|
|
136
|
-
if (now - lastBandwidthUpdate >= 2e3 && hls.bandwidthEstimate) {
|
|
137
|
-
lastBandwidthUpdate = now;
|
|
138
|
-
api.setState("bandwidth", Math.round(hls.bandwidthEstimate));
|
|
139
|
-
}
|
|
140
|
-
callbacks.onFragLoaded?.();
|
|
141
|
-
});
|
|
142
|
-
addHandler("hlsFragBuffered", () => {
|
|
143
|
-
api.setState("buffering", false);
|
|
144
|
-
callbacks.onBufferUpdate?.();
|
|
145
|
-
});
|
|
146
|
-
addHandler("hlsFragLoading", () => {
|
|
147
|
-
api.setState("buffering", true);
|
|
148
|
-
});
|
|
149
|
-
addHandler("hlsLevelLoaded", (_event, data) => {
|
|
150
|
-
if (data.details?.live !== void 0) {
|
|
151
|
-
api.setState("live", data.details.live);
|
|
152
|
-
if (data.details.live) {
|
|
153
|
-
const video = hls.media;
|
|
154
|
-
if (video && video.seekable && video.seekable.length > 0) {
|
|
155
|
-
const start = video.seekable.start(0);
|
|
156
|
-
const end = video.seekable.end(video.seekable.length - 1);
|
|
157
|
-
api.setState("seekableRange", { start, end });
|
|
158
|
-
const threshold = (data.details.targetduration ?? 3) * 3;
|
|
159
|
-
const isAtLiveEdge = end - video.currentTime < threshold;
|
|
160
|
-
api.setState("liveEdge", isAtLiveEdge);
|
|
161
|
-
const latency = end - video.currentTime;
|
|
162
|
-
api.setState("liveLatency", Math.max(0, latency));
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
callbacks.onLiveUpdate?.();
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
addHandler("hlsError", (_event, data) => {
|
|
169
|
-
const error = parseHlsError(data);
|
|
170
|
-
const isBufferHoleSeek = !error.fatal && (error.details?.includes("bufferStalledError") || data.reason?.includes("buffer holes"));
|
|
171
|
-
if (isBufferHoleSeek) {
|
|
172
|
-
api.logger.debug(`HLS buffer recovery: ${error.reason || error.details}`, {
|
|
173
|
-
details: error.details,
|
|
174
|
-
reason: error.reason
|
|
175
|
-
});
|
|
176
|
-
} else if (error.fatal) {
|
|
177
|
-
api.logger.error(`HLS fatal error: ${error.details} (type=${error.type})`, {
|
|
178
|
-
type: error.type,
|
|
179
|
-
details: error.details,
|
|
180
|
-
url: error.url
|
|
181
|
-
});
|
|
182
|
-
} else {
|
|
183
|
-
api.logger.warn(`HLS error: ${error.details} (type=${error.type}, fatal=${error.fatal})`, {
|
|
184
|
-
type: error.type,
|
|
185
|
-
details: error.details,
|
|
186
|
-
fatal: error.fatal,
|
|
187
|
-
url: error.url
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
callbacks.onError?.(error);
|
|
191
|
-
});
|
|
192
|
-
return () => {
|
|
193
|
-
for (const { event, handler } of handlers) {
|
|
194
|
-
hls.off(event, handler);
|
|
195
|
-
}
|
|
196
|
-
handlers.length = 0;
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
function setupVideoEventHandlers(video, api) {
|
|
200
|
-
const handlers = [];
|
|
201
|
-
const addHandler = (event, handler) => {
|
|
202
|
-
video.addEventListener(event, handler);
|
|
203
|
-
handlers.push({ event, handler });
|
|
204
|
-
};
|
|
205
|
-
addHandler("play", () => {
|
|
206
|
-
api.setState("paused", false);
|
|
207
|
-
});
|
|
208
|
-
addHandler("playing", () => {
|
|
209
|
-
api.setState("playing", true);
|
|
210
|
-
api.setState("paused", false);
|
|
211
|
-
api.setState("waiting", false);
|
|
212
|
-
api.setState("buffering", false);
|
|
213
|
-
api.setState("playbackState", "playing");
|
|
214
|
-
});
|
|
215
|
-
addHandler("pause", () => {
|
|
216
|
-
api.setState("playing", false);
|
|
217
|
-
api.setState("paused", true);
|
|
218
|
-
api.setState("playbackState", "paused");
|
|
219
|
-
});
|
|
220
|
-
addHandler("ended", () => {
|
|
221
|
-
api.setState("playing", false);
|
|
222
|
-
api.setState("ended", true);
|
|
223
|
-
api.setState("playbackState", "ended");
|
|
224
|
-
api.emit("playback:ended", void 0);
|
|
225
|
-
});
|
|
226
|
-
addHandler("timeupdate", () => {
|
|
227
|
-
api.setState("currentTime", video.currentTime);
|
|
228
|
-
api.emit("playback:timeupdate", { currentTime: video.currentTime });
|
|
229
|
-
const isLive = api.getState("live");
|
|
230
|
-
if (isLive && video.seekable && video.seekable.length > 0) {
|
|
231
|
-
const start = video.seekable.start(0);
|
|
232
|
-
const end = video.seekable.end(video.seekable.length - 1);
|
|
233
|
-
api.setState("seekableRange", { start, end });
|
|
234
|
-
const isAtLiveEdge = end - video.currentTime < 10;
|
|
235
|
-
api.setState("liveEdge", isAtLiveEdge);
|
|
236
|
-
api.setState("liveLatency", Math.max(0, end - video.currentTime));
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
addHandler("durationchange", () => {
|
|
240
|
-
api.setState("duration", video.duration || 0);
|
|
241
|
-
api.emit("media:loadedmetadata", { duration: video.duration || 0 });
|
|
242
|
-
});
|
|
243
|
-
addHandler("waiting", () => {
|
|
244
|
-
api.setState("waiting", true);
|
|
245
|
-
api.setState("buffering", true);
|
|
246
|
-
api.emit("media:waiting", void 0);
|
|
247
|
-
});
|
|
248
|
-
addHandler("canplay", () => {
|
|
249
|
-
api.setState("waiting", false);
|
|
250
|
-
api.setState("playbackState", "ready");
|
|
251
|
-
api.emit("media:canplay", void 0);
|
|
252
|
-
});
|
|
253
|
-
addHandler("canplaythrough", () => {
|
|
254
|
-
api.setState("buffering", false);
|
|
255
|
-
api.emit("media:canplaythrough", void 0);
|
|
256
|
-
});
|
|
257
|
-
addHandler("progress", () => {
|
|
258
|
-
if (video.buffered.length > 0) {
|
|
259
|
-
const bufferedEnd = video.buffered.end(video.buffered.length - 1);
|
|
260
|
-
const bufferedAmount = video.duration > 0 ? bufferedEnd / video.duration : 0;
|
|
261
|
-
api.setState("bufferedAmount", bufferedAmount);
|
|
262
|
-
api.setState("buffered", video.buffered);
|
|
263
|
-
api.emit("media:progress", { buffered: bufferedAmount });
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
addHandler("seeking", () => {
|
|
267
|
-
api.setState("seeking", true);
|
|
268
|
-
});
|
|
269
|
-
addHandler("seeked", () => {
|
|
270
|
-
api.setState("seeking", false);
|
|
271
|
-
api.emit("playback:seeked", { time: video.currentTime });
|
|
272
|
-
});
|
|
273
|
-
addHandler("volumechange", () => {
|
|
274
|
-
api.setState("volume", video.volume);
|
|
275
|
-
api.setState("muted", video.muted);
|
|
276
|
-
api.emit("volume:change", { volume: video.volume, muted: video.muted });
|
|
277
|
-
});
|
|
278
|
-
addHandler("ratechange", () => {
|
|
279
|
-
api.setState("playbackRate", video.playbackRate);
|
|
280
|
-
api.emit("playback:ratechange", { rate: video.playbackRate });
|
|
281
|
-
});
|
|
282
|
-
addHandler("loadedmetadata", () => {
|
|
283
|
-
api.setState("duration", video.duration);
|
|
284
|
-
api.setState("mediaType", video.videoWidth > 0 ? "video" : "audio");
|
|
285
|
-
});
|
|
286
|
-
addHandler("loadeddata", () => {
|
|
287
|
-
if (video.videoWidth > 0) {
|
|
288
|
-
api.setState("mediaType", "video");
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
addHandler("error", () => {
|
|
292
|
-
const error = video.error;
|
|
293
|
-
if (error) {
|
|
294
|
-
api.logger.error("Video element error", { code: error.code, message: error.message });
|
|
295
|
-
api.emit("media:error", { error: new Error(error.message || "Video playback error") });
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
addHandler("enterpictureinpicture", () => {
|
|
299
|
-
api.setState("pip", true);
|
|
300
|
-
api.logger.debug("PiP: entered (standard)");
|
|
301
|
-
});
|
|
302
|
-
addHandler("leavepictureinpicture", () => {
|
|
303
|
-
api.setState("pip", false);
|
|
304
|
-
api.logger.debug("PiP: exited (standard)");
|
|
305
|
-
if (!video.paused || api.getState("playing")) {
|
|
306
|
-
video.play().catch(() => {
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
const webkitVideo = video;
|
|
311
|
-
if ("webkitPresentationMode" in video) {
|
|
312
|
-
addHandler("webkitpresentationmodechanged", () => {
|
|
313
|
-
const mode = webkitVideo.webkitPresentationMode;
|
|
314
|
-
const isInPip = mode === "picture-in-picture";
|
|
315
|
-
api.setState("pip", isInPip);
|
|
316
|
-
api.logger.debug(`PiP: mode changed to ${mode} (webkit)`);
|
|
317
|
-
if (mode === "inline" && video.paused) {
|
|
318
|
-
video.play().catch(() => {
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
return () => {
|
|
324
|
-
for (const { event, handler } of handlers) {
|
|
325
|
-
video.removeEventListener(event, handler);
|
|
326
|
-
}
|
|
327
|
-
handlers.length = 0;
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
export {
|
|
332
|
-
formatLevel,
|
|
333
|
-
mapLevels,
|
|
334
|
-
getInitialBandwidthEstimate,
|
|
335
|
-
setupHlsEventHandlers,
|
|
336
|
-
setupVideoEventHandlers
|
|
337
|
-
};
|