@bsky.app/hls 0.0.0-stage → 0.1.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 +93 -2
- package/dist/abr.d.ts +25 -0
- package/dist/abr.d.ts.map +1 -0
- package/dist/abr.js +67 -0
- package/dist/abr.js.map +1 -0
- package/dist/bandwidth.d.ts +8 -0
- package/dist/bandwidth.d.ts.map +1 -0
- package/dist/bandwidth.js +48 -0
- package/dist/bandwidth.js.map +1 -0
- package/dist/errors.d.ts +30 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +38 -0
- package/dist/errors.js.map +1 -0
- package/dist/fmp4.d.ts +3 -0
- package/dist/fmp4.d.ts.map +1 -0
- package/dist/fmp4.js +224 -0
- package/dist/fmp4.js.map +1 -0
- package/dist/gaps.d.ts +16 -0
- package/dist/gaps.d.ts.map +1 -0
- package/dist/gaps.js +40 -0
- package/dist/gaps.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +26 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +94 -0
- package/dist/loader.js.map +1 -0
- package/dist/mse.d.ts +18 -0
- package/dist/mse.d.ts.map +1 -0
- package/dist/mse.js +123 -0
- package/dist/mse.js.map +1 -0
- package/dist/player.d.ts +3 -0
- package/dist/player.d.ts.map +1 -0
- package/dist/player.js +715 -0
- package/dist/player.js.map +1 -0
- package/dist/playlist.d.ts +7 -0
- package/dist/playlist.d.ts.map +1 -0
- package/dist/playlist.js +151 -0
- package/dist/playlist.js.map +1 -0
- package/dist/scheduler.d.ts +24 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +79 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/subtitles.d.ts +5 -0
- package/dist/subtitles.d.ts.map +1 -0
- package/dist/subtitles.js +29 -0
- package/dist/subtitles.js.map +1 -0
- package/dist/ts/aac.d.ts +18 -0
- package/dist/ts/aac.d.ts.map +1 -0
- package/dist/ts/aac.js +55 -0
- package/dist/ts/aac.js.map +1 -0
- package/dist/ts/demux.d.ts +19 -0
- package/dist/ts/demux.d.ts.map +1 -0
- package/dist/ts/demux.js +91 -0
- package/dist/ts/demux.js.map +1 -0
- package/dist/ts/h264.d.ts +25 -0
- package/dist/ts/h264.d.ts.map +1 -0
- package/dist/ts/h264.js +171 -0
- package/dist/ts/h264.js.map +1 -0
- package/dist/ts/index.d.ts +3 -0
- package/dist/ts/index.d.ts.map +1 -0
- package/dist/ts/index.js +117 -0
- package/dist/ts/index.js.map +1 -0
- package/dist/ts/mp4.d.ts +32 -0
- package/dist/ts/mp4.d.ts.map +1 -0
- package/dist/ts/mp4.js +76 -0
- package/dist/ts/mp4.js.map +1 -0
- package/dist/types.d.ts +153 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +23 -0
- package/dist/types.js.map +1 -0
- package/package.json +45 -4
- package/src/abr.ts +114 -0
- package/src/bandwidth.ts +57 -0
- package/src/errors.ts +73 -0
- package/src/fmp4.ts +228 -0
- package/src/gaps.ts +50 -0
- package/src/index.ts +10 -0
- package/src/loader.ts +111 -0
- package/src/mse.ts +141 -0
- package/src/player.ts +834 -0
- package/src/playlist.ts +157 -0
- package/src/scheduler.ts +114 -0
- package/src/subtitles.ts +39 -0
- package/src/ts/aac.ts +63 -0
- package/src/ts/demux.ts +93 -0
- package/src/ts/h264.ts +172 -0
- package/src/ts/index.ts +131 -0
- package/src/ts/mp4.ts +242 -0
- package/src/types.ts +216 -0
package/dist/gaps.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export function toRanges(r) {
|
|
2
|
+
const out = [];
|
|
3
|
+
for (let i = 0; i < r.length; i++)
|
|
4
|
+
out.push([r.start(i), r.end(i)]);
|
|
5
|
+
return out;
|
|
6
|
+
}
|
|
7
|
+
export const inRanges = (ranges, t) => ranges.some(([s, e]) => t >= s && t < e);
|
|
8
|
+
/*
|
|
9
|
+
* Where to jump if the playhead sits in a hole shorter than 2 s before the
|
|
10
|
+
* next buffered range: the start gap left by media that begins a few frames
|
|
11
|
+
* after 0, or a hole between segments. Lands slightly inside the range,
|
|
12
|
+
* since some browsers won't resume exactly on its boundary.
|
|
13
|
+
*/
|
|
14
|
+
export function gapJump(ranges, t) {
|
|
15
|
+
if (inRanges(ranges, t))
|
|
16
|
+
return;
|
|
17
|
+
const next = ranges.find(([s]) => s > t);
|
|
18
|
+
if (next && next[0] - t < 2)
|
|
19
|
+
return Math.min(next[0] + 0.05, next[1]);
|
|
20
|
+
}
|
|
21
|
+
/*
|
|
22
|
+
* Called on every tick. A stall is the playhead not moving for 1.25 s while
|
|
23
|
+
* playing with data buffered ahead of it, which usually means the decoder is
|
|
24
|
+
* stuck on a tiny hole or a broken frame. Returns how far to nudge, or
|
|
25
|
+
* 'fatal' once three increasing nudges haven't helped.
|
|
26
|
+
*/
|
|
27
|
+
export function checkStall(state, { now, time, playing, bufferedAhead, }) {
|
|
28
|
+
if (!playing || time !== state.time) {
|
|
29
|
+
if (time !== state.time)
|
|
30
|
+
state.nudges = 0;
|
|
31
|
+
state.time = time;
|
|
32
|
+
state.since = now;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!bufferedAhead || now - state.since < 1250)
|
|
36
|
+
return;
|
|
37
|
+
state.since = now;
|
|
38
|
+
return ++state.nudges > 3 ? 'fatal' : state.nudges * 0.1;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=gaps.js.map
|
package/dist/gaps.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gaps.js","sourceRoot":"","sources":["../src/gaps.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,QAAQ,CAAC,CAAa;IACpC,MAAM,GAAG,GAAW,EAAE,CAAA;IACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC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I,CAAC,aAAa,IAAI,GAAG,GAAG,KAAK,CAAC,KAAK,GAAG,IAAI;QAAE,OAAM;IACtD,KAAK,CAAC,KAAK,GAAG,GAAG,CAAA;IACjB,OAAO,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,GAAG,GAAG,CAAA;AAC1D,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { getBandwidthEstimate, resetBandwidthEstimate, setBandwidthEstimate, } from './bandwidth.js';
|
|
2
|
+
export { HlsError, HlsUnsupportedError, VideoNotFoundError } from './errors.js';
|
|
3
|
+
export type { HlsErrorDetail } from './errors.js';
|
|
4
|
+
export { canPlayHls } from './mse.js';
|
|
5
|
+
export { createHlsPlayer } from './player.js';
|
|
6
|
+
export type * from './types.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,oBAAoB,GACrB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAC,QAAQ,EAAE,mBAAmB,EAAE,kBAAkB,EAAC,MAAM,aAAa,CAAA;AAC7E,YAAY,EAAC,cAAc,EAAC,MAAM,aAAa,CAAA;AAC/C,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAA;AACnC,OAAO,EAAC,eAAe,EAAC,MAAM,aAAa,CAAA;AAC3C,mBAAmB,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { getBandwidthEstimate, resetBandwidthEstimate, setBandwidthEstimate, } from './bandwidth.js';
|
|
2
|
+
export { HlsError, HlsUnsupportedError, VideoNotFoundError } from './errors.js';
|
|
3
|
+
export { canPlayHls } from './mse.js';
|
|
4
|
+
export { createHlsPlayer } from './player.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,oBAAoB,GACrB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAC,QAAQ,EAAE,mBAAmB,EAAE,kBAAkB,EAAC,MAAM,aAAa,CAAA;AAE7E,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAA;AACnC,OAAO,EAAC,eAAe,EAAC,MAAM,aAAa,CAAA"}
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { HlsError, type HlsErrorDetail } from './errors.js';
|
|
2
|
+
export type LoadResult = {
|
|
3
|
+
data: Uint8Array;
|
|
4
|
+
/** Response URL after redirects, for resolving relative URIs. */
|
|
5
|
+
url: string;
|
|
6
|
+
/** Request start to last byte, of the attempt that succeeded. */
|
|
7
|
+
ms: number;
|
|
8
|
+
};
|
|
9
|
+
export type LoadOptions = {
|
|
10
|
+
fetch: typeof fetch;
|
|
11
|
+
detail: HlsErrorDetail;
|
|
12
|
+
retries: number;
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
onRetry?: (error: HlsError) => void;
|
|
15
|
+
onProgress?: (loaded: number, total: number | undefined) => void;
|
|
16
|
+
};
|
|
17
|
+
export declare function load(url: string, { fetch, detail, retries, signal, onRetry, onProgress }: LoadOptions): Promise<LoadResult>;
|
|
18
|
+
export declare const loadText: (url: string, options: LoadOptions) => Promise<{
|
|
19
|
+
text: string;
|
|
20
|
+
data: Uint8Array;
|
|
21
|
+
/** Response URL after redirects, for resolving relative URIs. */
|
|
22
|
+
url: string;
|
|
23
|
+
/** Request start to last byte, of the attempt that succeeded. */
|
|
24
|
+
ms: number;
|
|
25
|
+
}>;
|
|
26
|
+
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,KAAK,cAAc,EAAC,MAAM,aAAa,CAAA;AAEzD,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,UAAU,CAAA;IAChB,iEAAiE;IACjE,GAAG,EAAE,MAAM,CAAA;IACX,iEAAiE;IACjE,EAAE,EAAE,MAAM,CAAA;CACX,CAAA;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,OAAO,KAAK,CAAA;IACnB,MAAM,EAAE,cAAc,CAAA;IACtB,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,KAAK,IAAI,CAAA;IACnC,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,KAAK,IAAI,CAAA;CACjE,CAAA;AAqBD,wBAAsB,IAAI,CACxB,GAAG,EAAE,MAAM,EACX,EAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAC,EAAE,WAAW,GACjE,OAAO,CAAC,UAAU,CAAC,CAgErB;AAED,eAAO,MAAM,QAAQ,GAAU,KAAK,MAAM,EAAE,SAAS,WAAW;;UAxGxD,UAAU;IAChB,iEAAiE;SAC5D,MAAM;IACX,iEAAiE;QAC7D,MAAM;EAuGX,CAAA"}
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { HlsError } from './errors.js';
|
|
2
|
+
const sleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
3
|
+
if (signal?.aborted)
|
|
4
|
+
return reject(signal.reason);
|
|
5
|
+
const onAbort = () => {
|
|
6
|
+
clearTimeout(id);
|
|
7
|
+
reject(signal.reason);
|
|
8
|
+
};
|
|
9
|
+
const id = setTimeout(() => {
|
|
10
|
+
signal?.removeEventListener('abort', onAbort);
|
|
11
|
+
resolve();
|
|
12
|
+
}, ms);
|
|
13
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
14
|
+
});
|
|
15
|
+
/*
|
|
16
|
+
* fetch() with a 10 s time-to-headers and 30 s total timeout, and retries
|
|
17
|
+
* with exponential backoff (1, 2, 4, 8 s). 4xx responses are never retried.
|
|
18
|
+
* Aborting `signal` rejects with the signal's reason, untouched.
|
|
19
|
+
*/
|
|
20
|
+
export async function load(url, { fetch, detail, retries, signal, onRetry, onProgress }) {
|
|
21
|
+
for (let attempt = 0;; attempt++) {
|
|
22
|
+
const controller = new AbortController();
|
|
23
|
+
const abort = () => controller.abort();
|
|
24
|
+
signal?.addEventListener('abort', abort);
|
|
25
|
+
let timeout;
|
|
26
|
+
const timer = (ms, what) => setTimeout(() => {
|
|
27
|
+
timeout = what;
|
|
28
|
+
controller.abort();
|
|
29
|
+
}, ms);
|
|
30
|
+
const firstByte = timer(10000, 'first byte');
|
|
31
|
+
const total = timer(30000, 'total');
|
|
32
|
+
let status;
|
|
33
|
+
try {
|
|
34
|
+
const start = performance.now();
|
|
35
|
+
const res = await fetch(url, { signal: controller.signal });
|
|
36
|
+
clearTimeout(firstByte);
|
|
37
|
+
status = res.status;
|
|
38
|
+
if (!res.ok)
|
|
39
|
+
throw new Error(`HTTP ${status}`);
|
|
40
|
+
const length = Number(res.headers.get('content-length')) || undefined;
|
|
41
|
+
const reader = res.body?.getReader();
|
|
42
|
+
let data;
|
|
43
|
+
if (reader) {
|
|
44
|
+
const chunks = [];
|
|
45
|
+
let loaded = 0;
|
|
46
|
+
for (let r; !(r = await reader.read()).done;) {
|
|
47
|
+
chunks.push(r.value);
|
|
48
|
+
loaded += r.value.length;
|
|
49
|
+
onProgress?.(loaded, length);
|
|
50
|
+
}
|
|
51
|
+
data = new Uint8Array(loaded);
|
|
52
|
+
loaded = 0;
|
|
53
|
+
for (const c of chunks) {
|
|
54
|
+
data.set(c, loaded);
|
|
55
|
+
loaded += c.length;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
data = new Uint8Array(await res.arrayBuffer());
|
|
60
|
+
}
|
|
61
|
+
return { data, url: res.url || url, ms: performance.now() - start };
|
|
62
|
+
}
|
|
63
|
+
catch (cause) {
|
|
64
|
+
if (signal?.aborted)
|
|
65
|
+
throw signal.reason;
|
|
66
|
+
const clientError = status !== undefined && status >= 400 && status < 500;
|
|
67
|
+
const error = new HlsError({
|
|
68
|
+
detail,
|
|
69
|
+
message: timeout
|
|
70
|
+
? `Timed out waiting for ${timeout}`
|
|
71
|
+
: `Failed to load (${cause?.message ?? cause})`,
|
|
72
|
+
fatal: clientError || attempt >= retries,
|
|
73
|
+
status,
|
|
74
|
+
url,
|
|
75
|
+
cause,
|
|
76
|
+
diagnostics: { attempt },
|
|
77
|
+
});
|
|
78
|
+
if (error.fatal)
|
|
79
|
+
throw error;
|
|
80
|
+
onRetry?.(error);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
clearTimeout(firstByte);
|
|
84
|
+
clearTimeout(total);
|
|
85
|
+
signal?.removeEventListener('abort', abort);
|
|
86
|
+
}
|
|
87
|
+
await sleep(Math.min(1000 * 2 ** attempt, 8000), signal);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export const loadText = async (url, options) => {
|
|
91
|
+
const res = await load(url, options);
|
|
92
|
+
return { ...res, text: new TextDecoder().decode(res.data) };
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAsB,MAAM,aAAa,CAAA;AAmBzD,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,MAAoB,EAAE,EAAE,CACj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,IAAI,CAAC,EAAE,MAAM,CAAC,CAAA;IAC1D,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,GAAW,EAAE,OAAoB,EAAE,EAAE;IAClE,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;IACpC,OAAO,EAAC,GAAG,GAAG,EAAE,IAAI,EAAE,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAC,CAAA;AAC3D,CAAC,CAAA"}
|
package/dist/mse.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type MediaSourceCtor = typeof MediaSource;
|
|
2
|
+
export declare function getMediaSource(): MediaSourceCtor | undefined;
|
|
3
|
+
export declare function canPlayHls(codecs?: string): boolean;
|
|
4
|
+
export type Attachment = {
|
|
5
|
+
ms: MediaSource;
|
|
6
|
+
managed: boolean;
|
|
7
|
+
opened: Promise<void>;
|
|
8
|
+
/** False between ManagedMediaSource's endstreaming and startstreaming. */
|
|
9
|
+
streaming(): boolean;
|
|
10
|
+
detach(): void;
|
|
11
|
+
};
|
|
12
|
+
export declare function attachMediaSource(video: HTMLVideoElement, playlistUrl: string, onStreaming: () => void): Attachment;
|
|
13
|
+
export type BufferQueue = {
|
|
14
|
+
run(op: () => void, async?: boolean): Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
export declare function createQueue(sb: SourceBuffer): BufferQueue;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=mse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mse.d.ts","sourceRoot":"","sources":["../src/mse.ts"],"names":[],"mappings":"AAAA,KAAK,eAAe,GAAG,OAAO,WAAW,CAAA;AAMzC,wBAAgB,cAAc,IAAI,eAAe,GAAG,SAAS,CAI5D;AAED,wBAAgB,UAAU,CAAC,MAAM,SAA0B,GAAG,OAAO,CAEpE;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,EAAE,EAAE,WAAW,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;IACrB,0EAA0E;IAC1E,SAAS,IAAI,OAAO,CAAA;IACpB,MAAM,IAAI,IAAI,CAAA;CACf,CAAA;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,gBAAgB,EACvB,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,IAAI,GACtB,UAAU,CAwDZ;AAED,MAAM,MAAM,WAAW,GAAG;IAKxB,GAAG,CAAC,EAAE,EAAE,MAAM,IAAI,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACpD,CAAA;AAMD,wBAAgB,WAAW,CAAC,EAAE,EAAE,YAAY,GAAG,WAAW,CAyCzD"}
|
package/dist/mse.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* ManagedMediaSource first: it's the only option on iOS (17.1+) and lets
|
|
3
|
+
* Safari evict buffered media under memory pressure instead of failing.
|
|
4
|
+
*/
|
|
5
|
+
export function getMediaSource() {
|
|
6
|
+
if (typeof self === 'undefined')
|
|
7
|
+
return;
|
|
8
|
+
const g = self;
|
|
9
|
+
return g.ManagedMediaSource ?? g.MediaSource ?? g.WebKitMediaSource;
|
|
10
|
+
}
|
|
11
|
+
export function canPlayHls(codecs = 'avc1.42E01E,mp4a.40.2') {
|
|
12
|
+
return !!getMediaSource()?.isTypeSupported(`video/mp4; codecs="${codecs}"`);
|
|
13
|
+
}
|
|
14
|
+
export function attachMediaSource(video, playlistUrl, onStreaming) {
|
|
15
|
+
const MS = getMediaSource();
|
|
16
|
+
const ms = new MS();
|
|
17
|
+
const managed = MS === self.ManagedMediaSource;
|
|
18
|
+
const url = URL.createObjectURL(ms);
|
|
19
|
+
const sources = [];
|
|
20
|
+
let streaming = true;
|
|
21
|
+
const opened = new Promise(resolve => ms.addEventListener('sourceopen', () => resolve(), { once: true }));
|
|
22
|
+
video.removeAttribute('src');
|
|
23
|
+
if (managed) {
|
|
24
|
+
/*
|
|
25
|
+
* ManagedMediaSource won't open unless remote playback is disabled or
|
|
26
|
+
* there's an AirPlay-able alternative. WebKit's documented way is the
|
|
27
|
+
* alternative: a second <source> with the HLS playlist, which also keeps
|
|
28
|
+
* AirPlay working. Streaming defaults to allowed, like hls.js; WebKit
|
|
29
|
+
* fires endstreaming when it has enough.
|
|
30
|
+
*/
|
|
31
|
+
ms.addEventListener('startstreaming', () => {
|
|
32
|
+
streaming = true;
|
|
33
|
+
onStreaming();
|
|
34
|
+
});
|
|
35
|
+
ms.addEventListener('endstreaming', () => {
|
|
36
|
+
streaming = false;
|
|
37
|
+
});
|
|
38
|
+
video.disableRemotePlayback = false;
|
|
39
|
+
for (const [src, type] of [
|
|
40
|
+
[url, 'video/mp4'],
|
|
41
|
+
[playlistUrl, 'application/x-mpegURL'],
|
|
42
|
+
]) {
|
|
43
|
+
const source = document.createElement('source');
|
|
44
|
+
source.src = src;
|
|
45
|
+
source.type = type;
|
|
46
|
+
sources.push(source);
|
|
47
|
+
}
|
|
48
|
+
video.append(...sources);
|
|
49
|
+
video.load();
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
video.src = url;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
ms,
|
|
56
|
+
managed,
|
|
57
|
+
opened,
|
|
58
|
+
streaming: () => streaming,
|
|
59
|
+
detach() {
|
|
60
|
+
try {
|
|
61
|
+
if (ms.readyState === 'open')
|
|
62
|
+
ms.endOfStream();
|
|
63
|
+
}
|
|
64
|
+
catch { }
|
|
65
|
+
for (const s of sources)
|
|
66
|
+
s.remove();
|
|
67
|
+
video.removeAttribute('src');
|
|
68
|
+
video.load();
|
|
69
|
+
URL.revokeObjectURL(url);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/*
|
|
74
|
+
* Serialises SourceBuffer access: touching it (including timestampOffset and
|
|
75
|
+
* the append window) while `updating` throws InvalidStateError.
|
|
76
|
+
*/
|
|
77
|
+
export function createQueue(sb) {
|
|
78
|
+
const ops = [];
|
|
79
|
+
let busy = false;
|
|
80
|
+
let failed = false;
|
|
81
|
+
const next = () => {
|
|
82
|
+
while (!busy && ops.length && !sb.updating) {
|
|
83
|
+
const [op, async, resolve, reject] = ops[0];
|
|
84
|
+
try {
|
|
85
|
+
op();
|
|
86
|
+
}
|
|
87
|
+
catch (e) {
|
|
88
|
+
ops.shift();
|
|
89
|
+
reject(e);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (async)
|
|
93
|
+
busy = true;
|
|
94
|
+
else {
|
|
95
|
+
ops.shift();
|
|
96
|
+
resolve();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
// An append that fails to parse fires `error`, then `updateend`.
|
|
101
|
+
sb.addEventListener('error', () => {
|
|
102
|
+
failed = true;
|
|
103
|
+
});
|
|
104
|
+
sb.addEventListener('updateend', () => {
|
|
105
|
+
if (!busy)
|
|
106
|
+
return next();
|
|
107
|
+
busy = false;
|
|
108
|
+
const [, , resolve, reject] = ops.shift();
|
|
109
|
+
if (failed)
|
|
110
|
+
reject(new Error('SourceBuffer error event'));
|
|
111
|
+
else
|
|
112
|
+
resolve();
|
|
113
|
+
failed = false;
|
|
114
|
+
next();
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
run: (op, async = false) => new Promise((resolve, reject) => {
|
|
118
|
+
ops.push([op, async, resolve, reject]);
|
|
119
|
+
next();
|
|
120
|
+
}),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=mse.js.map
|
package/dist/mse.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mse.js","sourceRoot":"","sources":["../src/mse.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM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