@nomercy-entertainment/nomercy-video-player 2.9.3 → 2.11.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/dist/adapters/video-backend/IVideoBackend.d.ts +8 -0
- package/dist/adapters/video-backend/hls-seek-prefetch.d.ts +36 -0
- package/dist/adapters/video-backend/hls-seek-prefetch.js +114 -0
- package/dist/adapters/video-backend/html5.d.ts +9 -0
- package/dist/adapters/video-backend/html5.js +47 -4
- package/dist/index.d.ts +6 -0
- package/dist/index.js +9 -0
- package/dist/nomercy-video-player.iife.js +2 -2
- package/dist/plugins/key-handler/index.js +2 -0
- package/dist/types.d.ts +2 -0
- package/package.json +3 -3
|
@@ -181,12 +181,20 @@ export interface IVideoBackend {
|
|
|
181
181
|
setHdrOnSdrFallback?(fallback: HdrOnSdrFallback): void;
|
|
182
182
|
/** How a live stream stays near the live edge. `NMVideoPlayer` pushes `options.liveSync` here before the first load; backends with no live catch-up omit it. */
|
|
183
183
|
setLiveSync?(options: LiveSyncOptions): void;
|
|
184
|
+
/** Use hls.js over the element's native HLS when it is supported. `NMVideoPlayer` pushes `options.preferHlsJs` here before the first load; backends with one HLS path omit it. */
|
|
185
|
+
setPreferHlsJs?(prefer: boolean): void;
|
|
184
186
|
/** Fetch the manifests and first `segments` segments of `url` into memory for the next `load(url)`. Optional; a repeat call for the same URL is a no-op. */
|
|
185
187
|
warm?(url: string, opts?: {
|
|
186
188
|
segments?: number;
|
|
187
189
|
}): Promise<void>;
|
|
188
190
|
/** Abort an in-flight `warm()` and drop what it held. Idempotent. */
|
|
189
191
|
cancelWarm?(): void;
|
|
192
|
+
/** Fetch the fragments of the current source that cover `[seconds, seconds + opts.seconds)` (default 10 s) into memory, so a later seek there is answered without the network. Optional; a no-op until the playlist is parsed. */
|
|
193
|
+
prefetchAt?(seconds: number, opts?: {
|
|
194
|
+
seconds?: number;
|
|
195
|
+
}): Promise<void>;
|
|
196
|
+
/** `true` when a seek to `seconds` lands on media already held: buffered, or fully fetched by `prefetchAt`. Optional. */
|
|
197
|
+
isPrerolled?(seconds: number): boolean;
|
|
190
198
|
play(): Promise<void>;
|
|
191
199
|
pause(): void;
|
|
192
200
|
stop(): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { WarmEntries } from './hls-warm-cache.js';
|
|
2
|
+
/** The part of an hls.js `Fragment` the prefetch reads. */
|
|
3
|
+
export interface PrefetchFragment {
|
|
4
|
+
start: number;
|
|
5
|
+
duration: number;
|
|
6
|
+
/** Absolute URL, as hls.js passes it to its loader. */
|
|
7
|
+
url: string;
|
|
8
|
+
byteRangeStartOffset?: number | undefined;
|
|
9
|
+
byteRangeEndOffset?: number | undefined;
|
|
10
|
+
}
|
|
11
|
+
export interface SeekPrefetchOptions {
|
|
12
|
+
/** Length of the window to fetch from the position. */
|
|
13
|
+
seconds: number;
|
|
14
|
+
/** Stop starting new fetches once this many bytes are held. */
|
|
15
|
+
maxBytes?: number;
|
|
16
|
+
authHeader?: (url: string) => string | undefined;
|
|
17
|
+
fetch?: typeof fetch;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Fetches the fragments of the CURRENT source around a future seek target into
|
|
21
|
+
* memory. The bodies sit in `entries`, which the warm loader reads, so hls.js
|
|
22
|
+
* answers the seek from memory instead of the network.
|
|
23
|
+
*/
|
|
24
|
+
export declare class SeekPrefetch {
|
|
25
|
+
readonly entries: WarmEntries;
|
|
26
|
+
private _windows;
|
|
27
|
+
private _abort;
|
|
28
|
+
private _bytes;
|
|
29
|
+
constructor(entries?: WarmEntries);
|
|
30
|
+
/** Fetch the fragments of every stream that overlap `[from, from + seconds)`. Resolves when all have settled. */
|
|
31
|
+
prefetch(streams: PrefetchFragment[][], from: number, opts: SeekPrefetchOptions): Promise<void>;
|
|
32
|
+
/** `true` once every fragment of a window that covers `seconds` has been fetched. */
|
|
33
|
+
isPrerolled(seconds: number): boolean;
|
|
34
|
+
/** Abort in-flight fetches and drop everything held. Idempotent. */
|
|
35
|
+
clear(): void;
|
|
36
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// -----------------------------------------------------------------------------
|
|
2
|
+
// Copyright (c) NoMercy Entertainment
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0. See LICENSE for details.
|
|
5
|
+
//
|
|
6
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
7
|
+
// -----------------------------------------------------------------------------
|
|
8
|
+
import { warmKey } from './hls-warm-cache.js';
|
|
9
|
+
const DEFAULT_MAX_BYTES = 48 * 1024 * 1024;
|
|
10
|
+
/**
|
|
11
|
+
* Fetches the fragments of the CURRENT source around a future seek target into
|
|
12
|
+
* memory. The bodies sit in `entries`, which the warm loader reads, so hls.js
|
|
13
|
+
* answers the seek from memory instead of the network.
|
|
14
|
+
*/
|
|
15
|
+
export class SeekPrefetch {
|
|
16
|
+
entries;
|
|
17
|
+
_windows = [];
|
|
18
|
+
_abort = new AbortController();
|
|
19
|
+
_bytes = 0;
|
|
20
|
+
constructor(entries = new Map()) {
|
|
21
|
+
this.entries = entries;
|
|
22
|
+
}
|
|
23
|
+
/** Fetch the fragments of every stream that overlap `[from, from + seconds)`. Resolves when all have settled. */
|
|
24
|
+
async prefetch(streams, from, opts) {
|
|
25
|
+
const to = from + opts.seconds;
|
|
26
|
+
const doFetch = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
27
|
+
const maxBytes = opts.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
28
|
+
const signal = this._abort.signal;
|
|
29
|
+
const pending = [];
|
|
30
|
+
let covered;
|
|
31
|
+
for (const fragments of streams) {
|
|
32
|
+
const inside = fragments.filter((frag) => frag.start + frag.duration > from && frag.start < to);
|
|
33
|
+
if (inside.length === 0)
|
|
34
|
+
continue;
|
|
35
|
+
const first = inside[0];
|
|
36
|
+
const last = inside[inside.length - 1];
|
|
37
|
+
const start = first.start;
|
|
38
|
+
const end = last.start + last.duration;
|
|
39
|
+
covered = covered
|
|
40
|
+
? {
|
|
41
|
+
from: Math.max(covered.from, start),
|
|
42
|
+
to: Math.min(covered.to, end),
|
|
43
|
+
settled: false,
|
|
44
|
+
}
|
|
45
|
+
: {
|
|
46
|
+
from: start,
|
|
47
|
+
to: end,
|
|
48
|
+
settled: false,
|
|
49
|
+
};
|
|
50
|
+
for (const frag of inside) {
|
|
51
|
+
const key = warmKey(frag.url, frag.byteRangeStartOffset, frag.byteRangeEndOffset);
|
|
52
|
+
const held = this.entries.get(key);
|
|
53
|
+
if (held) {
|
|
54
|
+
pending.push(held);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (this._bytes >= maxBytes)
|
|
58
|
+
continue;
|
|
59
|
+
const startedAt = performance.now();
|
|
60
|
+
const authorization = opts.authHeader?.(frag.url);
|
|
61
|
+
const range = (frag.byteRangeStartOffset ?? 0) !== 0 || (frag.byteRangeEndOffset ?? 0) !== 0
|
|
62
|
+
? { Range: `bytes=${frag.byteRangeStartOffset}-${(frag.byteRangeEndOffset ?? 1) - 1}` }
|
|
63
|
+
: {};
|
|
64
|
+
const headers = {
|
|
65
|
+
...range,
|
|
66
|
+
...(authorization
|
|
67
|
+
? { Authorization: authorization }
|
|
68
|
+
: {}),
|
|
69
|
+
};
|
|
70
|
+
const request = doFetch(frag.url, {
|
|
71
|
+
signal,
|
|
72
|
+
headers,
|
|
73
|
+
}).then(async (response) => {
|
|
74
|
+
if (!response.ok)
|
|
75
|
+
return undefined;
|
|
76
|
+
const data = await response.arrayBuffer();
|
|
77
|
+
this._bytes += data.byteLength;
|
|
78
|
+
return {
|
|
79
|
+
data,
|
|
80
|
+
fetchMs: performance.now() - startedAt,
|
|
81
|
+
};
|
|
82
|
+
})
|
|
83
|
+
.catch(() => undefined);
|
|
84
|
+
this.entries.set(key, request);
|
|
85
|
+
pending.push(request);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (covered === undefined)
|
|
89
|
+
return;
|
|
90
|
+
const window = covered;
|
|
91
|
+
this._windows.push(window);
|
|
92
|
+
const results = await Promise.all(pending);
|
|
93
|
+
if (signal.aborted)
|
|
94
|
+
return;
|
|
95
|
+
if (results.includes(undefined)) {
|
|
96
|
+
this._windows = this._windows.filter((item) => item !== window);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
window.settled = true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** `true` once every fragment of a window that covers `seconds` has been fetched. */
|
|
103
|
+
isPrerolled(seconds) {
|
|
104
|
+
return this._windows.some((window) => window.settled && seconds >= window.from && seconds < window.to);
|
|
105
|
+
}
|
|
106
|
+
/** Abort in-flight fetches and drop everything held. Idempotent. */
|
|
107
|
+
clear() {
|
|
108
|
+
this._abort.abort();
|
|
109
|
+
this._abort = new AbortController();
|
|
110
|
+
this.entries.clear();
|
|
111
|
+
this._windows = [];
|
|
112
|
+
this._bytes = 0;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -145,6 +145,10 @@ export declare class Html5VideoBackend extends MediaElementBackend<HTMLVideoElem
|
|
|
145
145
|
/** The speed the viewer chose; hls.js catch-up may move the element's own rate off it while live. */
|
|
146
146
|
private _viewerRate;
|
|
147
147
|
setLiveSync(options: LiveSyncOptions): void;
|
|
148
|
+
private _preferHlsJs;
|
|
149
|
+
setPreferHlsJs(prefer: boolean): void;
|
|
150
|
+
/** The element's own HLS, unless hls.js is preferred and MediaSource lets it run. */
|
|
151
|
+
private _useNativeHls;
|
|
148
152
|
private _maxCatchUpRate;
|
|
149
153
|
playbackRate(): number;
|
|
150
154
|
playbackRate(rate: number): void;
|
|
@@ -156,6 +160,11 @@ export declare class Html5VideoBackend extends MediaElementBackend<HTMLVideoElem
|
|
|
156
160
|
segments?: number;
|
|
157
161
|
}): Promise<void>;
|
|
158
162
|
cancelWarm(): void;
|
|
163
|
+
private _seekPrefetch;
|
|
164
|
+
prefetchAt(seconds: number, opts?: {
|
|
165
|
+
seconds?: number;
|
|
166
|
+
}): Promise<void>;
|
|
167
|
+
isPrerolled(seconds: number): boolean;
|
|
159
168
|
unload(): void;
|
|
160
169
|
dispose(): void;
|
|
161
170
|
private _teardownAudioGraph;
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// -----------------------------------------------------------------------------
|
|
8
8
|
import { abrCeiling, appendAuthTokenParam, createAuthorizationXhrSetup, destroyHlsInstance, detectDisplayHdr, hdrDecision, isHls, MediaElementBackend, MediaFormatError, mediaFormatError, panePixels, resetMediaElement, sizeAbrCeiling, StreamError, supportsNativeHls, } from '@nomercy-entertainment/nomercy-player-core';
|
|
9
9
|
import { drainCaptionCache } from './hls-caption-drain.js';
|
|
10
|
+
import { SeekPrefetch } from './hls-seek-prefetch.js';
|
|
10
11
|
import { createWarmLoader, HlsWarmCache } from './hls-warm-cache.js';
|
|
11
12
|
import { definiteFailureCode, SOURCE_OUTAGE_BACKOFF_MS, sourceOutageRetryLimit } from './source-outage.js';
|
|
12
13
|
/** Cap on bytes the next-item warmup may hold in memory. */
|
|
@@ -205,7 +206,14 @@ export class Html5VideoBackend extends MediaElementBackend {
|
|
|
205
206
|
}
|
|
206
207
|
const hlsUrl = isHls(url);
|
|
207
208
|
const warmed = this._warmCache.take(url);
|
|
208
|
-
|
|
209
|
+
this._seekPrefetch.clear();
|
|
210
|
+
this._seekPrefetch = new SeekPrefetch(warmed?.entries);
|
|
211
|
+
let nativeHls = this._useNativeHls();
|
|
212
|
+
// Preferred hls.js that cannot run here leaves the element's own HLS in place.
|
|
213
|
+
if (hlsUrl && !nativeHls && supportsNativeHls(this.element)) {
|
|
214
|
+
const { default: Hls } = await import('hls.js');
|
|
215
|
+
nativeHls = !Hls?.isSupported?.();
|
|
216
|
+
}
|
|
209
217
|
// Tear down any previous Hls instance BEFORE wiring a new source.
|
|
210
218
|
// Without this, every load() leaks an Hls that keeps polling segment 0
|
|
211
219
|
// against the same media element — symptom: thousands of identical
|
|
@@ -296,8 +304,8 @@ export class Html5VideoBackend extends MediaElementBackend {
|
|
|
296
304
|
});
|
|
297
305
|
}
|
|
298
306
|
const BaseLoader = Hls.DefaultConfig?.loader;
|
|
299
|
-
const warmLoader =
|
|
300
|
-
? createWarmLoader(BaseLoader,
|
|
307
|
+
const warmLoader = BaseLoader
|
|
308
|
+
? createWarmLoader(BaseLoader, this._seekPrefetch.entries)
|
|
301
309
|
: undefined;
|
|
302
310
|
const hlsInstance = new Hls({
|
|
303
311
|
...(warmLoader
|
|
@@ -408,6 +416,17 @@ export class Html5VideoBackend extends MediaElementBackend {
|
|
|
408
416
|
setLiveSync(options) {
|
|
409
417
|
this._liveSync = options;
|
|
410
418
|
}
|
|
419
|
+
_preferHlsJs = false;
|
|
420
|
+
setPreferHlsJs(prefer) {
|
|
421
|
+
this._preferHlsJs = prefer;
|
|
422
|
+
}
|
|
423
|
+
/** The element's own HLS, unless hls.js is preferred and MediaSource lets it run. */
|
|
424
|
+
_useNativeHls() {
|
|
425
|
+
if (!supportsNativeHls(this.element)) {
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
return !(this._preferHlsJs && typeof MediaSource !== 'undefined');
|
|
429
|
+
}
|
|
411
430
|
_maxCatchUpRate() {
|
|
412
431
|
return this._liveSync.maxCatchUpRate ?? DEFAULT_MAX_CATCH_UP_RATE;
|
|
413
432
|
}
|
|
@@ -438,7 +457,7 @@ export class Html5VideoBackend extends MediaElementBackend {
|
|
|
438
457
|
}
|
|
439
458
|
_warmCache = new HlsWarmCache();
|
|
440
459
|
async warm(url, opts) {
|
|
441
|
-
if (!isHls(url) ||
|
|
460
|
+
if (!isHls(url) || this._useNativeHls())
|
|
442
461
|
return;
|
|
443
462
|
const playingHeight = this.hls?.levels[this.currentLevel()]?.height;
|
|
444
463
|
await this._warmCache.warm(url, {
|
|
@@ -455,7 +474,31 @@ export class Html5VideoBackend extends MediaElementBackend {
|
|
|
455
474
|
cancelWarm() {
|
|
456
475
|
this._warmCache.cancel();
|
|
457
476
|
}
|
|
477
|
+
_seekPrefetch = new SeekPrefetch();
|
|
478
|
+
async prefetchAt(seconds, opts) {
|
|
479
|
+
const hls = this.hls;
|
|
480
|
+
if (!hls)
|
|
481
|
+
return;
|
|
482
|
+
const video = hls.levels[this.currentLevel()]?.details?.fragments;
|
|
483
|
+
const audio = hls.audioTracks[hls.audioTrack]?.details?.fragments;
|
|
484
|
+
const streams = [video, audio].filter((frags) => Array.isArray(frags));
|
|
485
|
+
await this._seekPrefetch.prefetch(streams, seconds, {
|
|
486
|
+
seconds: opts?.seconds ?? 10,
|
|
487
|
+
...(this._authHeaderProvider
|
|
488
|
+
? { authHeader: this._authHeaderProvider }
|
|
489
|
+
: {}),
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
isPrerolled(seconds) {
|
|
493
|
+
const ranges = this.element.buffered;
|
|
494
|
+
for (let i = 0; i < ranges.length; i += 1) {
|
|
495
|
+
if (seconds >= ranges.start(i) && seconds <= ranges.end(i) - 1)
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
return this._seekPrefetch.isPrerolled(seconds);
|
|
499
|
+
}
|
|
458
500
|
unload() {
|
|
501
|
+
this._seekPrefetch.clear();
|
|
459
502
|
this._state = 'idle';
|
|
460
503
|
this._ended = false;
|
|
461
504
|
this._resetRecoveryState();
|
package/dist/index.d.ts
CHANGED
|
@@ -98,6 +98,12 @@ export declare class NMVideoPlayer<T extends VideoPlaylistItem = VideoPlaylistIt
|
|
|
98
98
|
resolveCustomStream: (url: string, contentType?: string) => IStreamSource | undefined;
|
|
99
99
|
private _backend;
|
|
100
100
|
live(): boolean;
|
|
101
|
+
/** Fetch the media around `seconds` of the current source into memory so a later seek there starts without waiting on the network. */
|
|
102
|
+
prefetchAt(seconds: number, opts?: {
|
|
103
|
+
seconds?: number;
|
|
104
|
+
}): Promise<void>;
|
|
105
|
+
/** `true` when a seek to `seconds` lands on media already held (buffered, or fetched by `prefetchAt`). */
|
|
106
|
+
isPrerolled(seconds: number): boolean;
|
|
101
107
|
backend(): IVideoBackend;
|
|
102
108
|
backend(kind: VideoBackendKind): Promise<void>;
|
|
103
109
|
/**
|
package/dist/index.js
CHANGED
|
@@ -321,6 +321,14 @@ export class NMVideoPlayer extends EventEmitter {
|
|
|
321
321
|
live() {
|
|
322
322
|
return this._backend?.isLive?.() ?? false;
|
|
323
323
|
}
|
|
324
|
+
/** Fetch the media around `seconds` of the current source into memory so a later seek there starts without waiting on the network. */
|
|
325
|
+
async prefetchAt(seconds, opts) {
|
|
326
|
+
await this._backend?.prefetchAt?.(seconds, opts);
|
|
327
|
+
}
|
|
328
|
+
/** `true` when a seek to `seconds` lands on media already held (buffered, or fetched by `prefetchAt`). */
|
|
329
|
+
isPrerolled(seconds) {
|
|
330
|
+
return this._backend?.isPrerolled?.(seconds) ?? false;
|
|
331
|
+
}
|
|
324
332
|
backend(kind) {
|
|
325
333
|
if (kind === undefined) {
|
|
326
334
|
if (this._backend)
|
|
@@ -371,6 +379,7 @@ export class NMVideoPlayer extends EventEmitter {
|
|
|
371
379
|
// point of use when this was never pushed at all.
|
|
372
380
|
instance.setHdrOnSdrFallback?.(this.options?.hdrOnSdr ?? 'play');
|
|
373
381
|
instance.setLiveSync?.(this.options?.liveSync ?? {});
|
|
382
|
+
instance.setPreferHlsJs?.(this.options?.preferHlsJs ?? false);
|
|
374
383
|
// Mount the ResizeObserver that keeps videoRect() fresh. Done here so
|
|
375
384
|
// the observer sees the real video element immediately after backend init.
|
|
376
385
|
this._mountVideoRectObserver();
|