fluxkit-player 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/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/README.md +440 -2
- package/dist/fluxkit.js +3963 -0
- package/dist/fluxkit.js.map +1 -0
- package/dist/fluxkit.umd.cjs +22 -0
- package/dist/fluxkit.umd.cjs.map +1 -0
- package/dist/types/codec/aac.d.ts +19 -0
- package/dist/types/codec/bit.d.ts +15 -0
- package/dist/types/codec/config.d.ts +19 -0
- package/dist/types/codec/g711.d.ts +4 -0
- package/dist/types/codec/h264.d.ts +37 -0
- package/dist/types/codec/h265.d.ts +27 -0
- package/dist/types/config.d.ts +46 -0
- package/dist/types/demux/flv.d.ts +46 -0
- package/dist/types/demux/hls.d.ts +54 -0
- package/dist/types/demux/mp4.d.ts +45 -0
- package/dist/types/demux/ts.d.ts +41 -0
- package/dist/types/index.d.ts +31 -0
- package/dist/types/io/http.d.ts +32 -0
- package/dist/types/io/websocket.d.ts +27 -0
- package/dist/types/pipeline/segmenter.d.ts +16 -0
- package/dist/types/pipeline/timeline.d.ts +11 -0
- package/dist/types/player.d.ts +110 -0
- package/dist/types/protocol/amf0.d.ts +12 -0
- package/dist/types/remux/fmp4.d.ts +32 -0
- package/dist/types/render/audio.d.ts +48 -0
- package/dist/types/render/mse.d.ts +49 -0
- package/dist/types/render/software.d.ts +77 -0
- package/dist/types/render/webcodecs.d.ts +69 -0
- package/dist/types/soft/h264bsd.d.ts +23 -0
- package/dist/types/soft/index.d.ts +9 -0
- package/dist/types/soft/libde265.d.ts +16 -0
- package/dist/types/soft/types.d.ts +37 -0
- package/dist/types/source/hls.d.ts +29 -0
- package/dist/types/source/stream.d.ts +22 -0
- package/dist/types/types.d.ts +246 -0
- package/dist/types/utils/async.d.ts +12 -0
- package/dist/types/utils/buffer.d.ts +17 -0
- package/dist/types/utils/bytes.d.ts +62 -0
- package/dist/types/utils/events.d.ts +10 -0
- package/dist/types/utils/log.d.ts +14 -0
- package/package.json +77 -4
package/dist/fluxkit.js
ADDED
|
@@ -0,0 +1,3963 @@
|
|
|
1
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/typeof.js
|
|
2
|
+
function e(t) {
|
|
3
|
+
"@babel/helpers - typeof";
|
|
4
|
+
return e = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(e) {
|
|
5
|
+
return typeof e;
|
|
6
|
+
} : function(e) {
|
|
7
|
+
return e && typeof Symbol == "function" && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
|
|
8
|
+
}, e(t);
|
|
9
|
+
}
|
|
10
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/toPrimitive.js
|
|
11
|
+
function t(t, n) {
|
|
12
|
+
if (e(t) != "object" || !t) return t;
|
|
13
|
+
var r = t[Symbol.toPrimitive];
|
|
14
|
+
if (r !== void 0) {
|
|
15
|
+
var i = r.call(t, n || "default");
|
|
16
|
+
if (e(i) != "object") return i;
|
|
17
|
+
throw TypeError("@@toPrimitive must return a primitive value.");
|
|
18
|
+
}
|
|
19
|
+
return (n === "string" ? String : Number)(t);
|
|
20
|
+
}
|
|
21
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/toPropertyKey.js
|
|
22
|
+
function n(n) {
|
|
23
|
+
var r = t(n, "string");
|
|
24
|
+
return e(r) == "symbol" ? r : r + "";
|
|
25
|
+
}
|
|
26
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/defineProperty.js
|
|
27
|
+
function r(e, t, r) {
|
|
28
|
+
return (t = n(t)) in e ? Object.defineProperty(e, t, {
|
|
29
|
+
value: r,
|
|
30
|
+
enumerable: !0,
|
|
31
|
+
configurable: !0,
|
|
32
|
+
writable: !0
|
|
33
|
+
}) : e[t] = r, e;
|
|
34
|
+
}
|
|
35
|
+
// src/types.ts
|
|
36
|
+
var i = class extends Error {
|
|
37
|
+
constructor(e, t, n) {
|
|
38
|
+
super(t), r(this, "code", void 0), r(this, "detail", void 0), this.name = "FluxKitError", this.code = e, this.detail = n;
|
|
39
|
+
}
|
|
40
|
+
}, a = {
|
|
41
|
+
decoder: "auto",
|
|
42
|
+
protocol: "native",
|
|
43
|
+
autoplay: !0,
|
|
44
|
+
muted: !0,
|
|
45
|
+
loop: !1,
|
|
46
|
+
volume: 1,
|
|
47
|
+
logLevel: "warn",
|
|
48
|
+
allowNativeFallback: !0,
|
|
49
|
+
hardwareAcceleration: "prefer-hardware",
|
|
50
|
+
segmentDuration: 500,
|
|
51
|
+
liveBufferTarget: 1.5,
|
|
52
|
+
catchUp: !0,
|
|
53
|
+
timeout: 1e4
|
|
54
|
+
};
|
|
55
|
+
function o(e = {}) {
|
|
56
|
+
let t = {
|
|
57
|
+
maxRetries: e.retry?.maxRetries ?? 3,
|
|
58
|
+
delay: e.retry?.delay ?? 1e3,
|
|
59
|
+
maxDelay: e.retry?.maxDelay ?? 8e3
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
decoder: e.decoder ?? a.decoder,
|
|
63
|
+
protocol: e.protocol ?? "auto",
|
|
64
|
+
autoplay: e.autoplay ?? a.autoplay,
|
|
65
|
+
muted: e.muted ?? a.muted,
|
|
66
|
+
loop: e.loop ?? a.loop,
|
|
67
|
+
volume: e.volume ?? a.volume,
|
|
68
|
+
logLevel: e.logLevel ?? a.logLevel,
|
|
69
|
+
logger: e.logger,
|
|
70
|
+
allowNativeFallback: e.allowNativeFallback ?? a.allowNativeFallback,
|
|
71
|
+
hardwareAcceleration: e.hardwareAcceleration ?? a.hardwareAcceleration,
|
|
72
|
+
segmentDuration: e.segmentDuration ?? a.segmentDuration,
|
|
73
|
+
liveBufferTarget: e.liveBufferTarget ?? a.liveBufferTarget,
|
|
74
|
+
catchUp: e.catchUp ?? a.catchUp,
|
|
75
|
+
timeout: e.timeout ?? a.timeout,
|
|
76
|
+
headers: e.headers ?? {},
|
|
77
|
+
retry: t,
|
|
78
|
+
hls: {
|
|
79
|
+
mode: e.hls?.mode ?? "demux",
|
|
80
|
+
maxBandwidth: e.hls?.maxBandwidth,
|
|
81
|
+
liveReloadFactor: e.hls?.liveReloadFactor ?? 1.5
|
|
82
|
+
},
|
|
83
|
+
webcodecs: {
|
|
84
|
+
fit: e.webcodecs?.fit ?? "contain",
|
|
85
|
+
audioBuffer: e.webcodecs?.audioBuffer ?? .15
|
|
86
|
+
},
|
|
87
|
+
softDecode: {
|
|
88
|
+
enabled: e.softDecode?.enabled ?? !0,
|
|
89
|
+
maxDecodeAhead: e.softDecode?.maxDecodeAhead ?? 4,
|
|
90
|
+
dropLateFrames: e.softDecode?.dropLateFrames ?? !0,
|
|
91
|
+
loaders: e.softDecode?.loaders
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function s(e) {
|
|
96
|
+
let t = e.toLowerCase();
|
|
97
|
+
return t.startsWith("ws://") || t.startsWith("wss://") ? "ws-flv" : t.includes(".m3u8") ? "hls" : t.includes(".mpd") ? "native" : t.includes(".fmp4") || t.includes(".m4s") ? "fmp4" : t.includes(".flv") ? "flv" : t.includes(".mp4") || t.includes(".m4v") || t.includes(".mov") ? "mp4" : "flv";
|
|
98
|
+
}
|
|
99
|
+
// src/utils/events.ts
|
|
100
|
+
var c = class {
|
|
101
|
+
constructor() {
|
|
102
|
+
r(this, "listeners", /* @__PURE__ */ new Map());
|
|
103
|
+
}
|
|
104
|
+
on(e, t) {
|
|
105
|
+
let n = this.listeners.get(e);
|
|
106
|
+
return n || (n = /* @__PURE__ */ new Set(), this.listeners.set(e, n)), n.add(t), () => this.off(e, t);
|
|
107
|
+
}
|
|
108
|
+
once(e, t) {
|
|
109
|
+
let n = this.on(e, (e) => {
|
|
110
|
+
n(), t(e);
|
|
111
|
+
});
|
|
112
|
+
return n;
|
|
113
|
+
}
|
|
114
|
+
off(e, t) {
|
|
115
|
+
this.listeners.get(e)?.delete(t);
|
|
116
|
+
}
|
|
117
|
+
emit(e, t) {
|
|
118
|
+
let n = this.listeners.get(e);
|
|
119
|
+
if (n) for (let e of [...n]) try {
|
|
120
|
+
e(t);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
queueMicrotask(() => {
|
|
123
|
+
throw e;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
removeAll() {
|
|
128
|
+
this.listeners.clear();
|
|
129
|
+
}
|
|
130
|
+
}, l = {
|
|
131
|
+
silent: 0,
|
|
132
|
+
error: 1,
|
|
133
|
+
warn: 2,
|
|
134
|
+
info: 3,
|
|
135
|
+
debug: 4
|
|
136
|
+
}, u = class {
|
|
137
|
+
constructor(e = "warn", t, n) {
|
|
138
|
+
r(this, "custom", void 0), r(this, "level", void 0), r(this, "sink", void 0), this.custom = t, this.level = l[e] ?? l.warn, this.sink = n;
|
|
139
|
+
}
|
|
140
|
+
setSink(e) {
|
|
141
|
+
this.sink = e;
|
|
142
|
+
}
|
|
143
|
+
write(e, t, n) {
|
|
144
|
+
l[e] > this.level || (this.custom?.(e, t, ...n), this.sink?.(e, t), (e === "error" ? console.error : e === "warn" ? console.warn : e === "info" ? console.info : console.debug).call(console, `[fluxkit] ${t}`, ...n));
|
|
145
|
+
}
|
|
146
|
+
error(e, ...t) {
|
|
147
|
+
this.write("error", e, t);
|
|
148
|
+
}
|
|
149
|
+
warn(e, ...t) {
|
|
150
|
+
this.write("warn", e, t);
|
|
151
|
+
}
|
|
152
|
+
info(e, ...t) {
|
|
153
|
+
this.write("info", e, t);
|
|
154
|
+
}
|
|
155
|
+
debug(e, ...t) {
|
|
156
|
+
this.write("debug", e, t);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
// src/utils/async.ts
|
|
160
|
+
function d() {
|
|
161
|
+
let e, t, n = !1;
|
|
162
|
+
return {
|
|
163
|
+
promise: new Promise((r, i) => {
|
|
164
|
+
e = (e) => {
|
|
165
|
+
n || (n = !0, r(e));
|
|
166
|
+
}, t = (e) => {
|
|
167
|
+
n || (n = !0, i(e));
|
|
168
|
+
};
|
|
169
|
+
}),
|
|
170
|
+
resolve: e,
|
|
171
|
+
reject: t,
|
|
172
|
+
get settled() {
|
|
173
|
+
return n;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function f(e, t) {
|
|
178
|
+
return new Promise((n, r) => {
|
|
179
|
+
if (t?.aborted) {
|
|
180
|
+
r(new DOMException("Aborted", "AbortError"));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
let i = setTimeout(() => {
|
|
184
|
+
t?.removeEventListener("abort", a), n();
|
|
185
|
+
}, e), a = () => {
|
|
186
|
+
clearTimeout(i), r(new DOMException("Aborted", "AbortError"));
|
|
187
|
+
};
|
|
188
|
+
t?.addEventListener("abort", a, { once: !0 });
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function p(e, t, n) {
|
|
192
|
+
return new Promise((r, i) => {
|
|
193
|
+
let a = setTimeout(() => i(/* @__PURE__ */ Error(`${n} timed out after ${t}ms`)), t);
|
|
194
|
+
e.then((e) => {
|
|
195
|
+
clearTimeout(a), r(e);
|
|
196
|
+
}, (e) => {
|
|
197
|
+
clearTimeout(a), i(e);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
// src/pipeline/timeline.ts
|
|
202
|
+
var m = class {
|
|
203
|
+
constructor(e) {
|
|
204
|
+
r(this, "tracks", void 0), r(this, "baseMs", null), r(this, "startMs", 0), r(this, "lastDts", /* @__PURE__ */ new Map()), this.tracks = e;
|
|
205
|
+
}
|
|
206
|
+
setStart(e) {
|
|
207
|
+
this.startMs = e, this.baseMs = null, this.lastDts.clear();
|
|
208
|
+
}
|
|
209
|
+
normalize(e) {
|
|
210
|
+
let t = this.tracks.get(e.trackId);
|
|
211
|
+
if (!t) return e;
|
|
212
|
+
let n = e.dts * 1e3 / t.timescale;
|
|
213
|
+
this.baseMs === null && (this.baseMs = n - this.startMs);
|
|
214
|
+
let r = t.timescale / 1e3, i = Math.round((n - this.baseMs) * r), a = Math.round((e.pts * 1e3 / t.timescale - this.baseMs) * r), o = this.lastDts.get(t.id);
|
|
215
|
+
if (o !== void 0 && i <= o) {
|
|
216
|
+
let e = o + 1 - i;
|
|
217
|
+
i += e, a += e;
|
|
218
|
+
}
|
|
219
|
+
return a < i && (a = i), this.lastDts.set(t.id, i), {
|
|
220
|
+
...e,
|
|
221
|
+
dts: i,
|
|
222
|
+
pts: a
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
}, h = class {
|
|
226
|
+
constructor(e) {
|
|
227
|
+
r(this, "options", void 0), r(this, "buffers", /* @__PURE__ */ new Map()), r(this, "windowStart", Infinity), this.options = e;
|
|
228
|
+
}
|
|
229
|
+
push(e) {
|
|
230
|
+
let t = this.options.getTrack(e.trackId);
|
|
231
|
+
if (!t) return;
|
|
232
|
+
let n = e.dts * 1e3 / t.timescale;
|
|
233
|
+
Number.isFinite(this.windowStart) || (this.windowStart = n), e.kind === "video" && e.keyframe && this.hasSamples() && n - this.windowStart >= this.options.durationMs && (this.flush(), this.windowStart = n);
|
|
234
|
+
let r = this.buffers.get(t.id);
|
|
235
|
+
r || (r = [], this.buffers.set(t.id, r)), r.push(e), n - this.windowStart >= this.options.durationMs * 2 && this.flush();
|
|
236
|
+
}
|
|
237
|
+
hasSamples() {
|
|
238
|
+
for (let e of this.buffers.values()) if (e.length > 0) return !0;
|
|
239
|
+
return !1;
|
|
240
|
+
}
|
|
241
|
+
flush() {
|
|
242
|
+
for (let [e, t] of this.buffers) {
|
|
243
|
+
if (t.length === 0) continue;
|
|
244
|
+
let n = this.options.getTrack(e);
|
|
245
|
+
if (n) {
|
|
246
|
+
for (let e = 0; e < t.length - 1; e++) {
|
|
247
|
+
let n = t[e + 1].dts - t[e].dts;
|
|
248
|
+
n > 0 && (t[e] = {
|
|
249
|
+
...t[e],
|
|
250
|
+
duration: n
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
this.options.onSegment(n, t);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
this.buffers.clear(), this.windowStart = Infinity;
|
|
257
|
+
}
|
|
258
|
+
}, g = class {
|
|
259
|
+
constructor(e = 4096) {
|
|
260
|
+
r(this, "buf", void 0), r(this, "start", 0), r(this, "end", 0), this.buf = new Uint8Array(e);
|
|
261
|
+
}
|
|
262
|
+
get length() {
|
|
263
|
+
return this.end - this.start;
|
|
264
|
+
}
|
|
265
|
+
append(e) {
|
|
266
|
+
this.ensure(this.length + e.length), this.buf.set(e, this.end), this.end += e.length;
|
|
267
|
+
}
|
|
268
|
+
ensure(e) {
|
|
269
|
+
if (e <= this.buf.length - this.start) return;
|
|
270
|
+
let t = Math.max(this.buf.length * 2, 1024, e), n = new Uint8Array(t);
|
|
271
|
+
n.set(this.buf.subarray(this.start, this.end)), this.end -= this.start, this.start = 0, this.buf = n;
|
|
272
|
+
}
|
|
273
|
+
peek(e) {
|
|
274
|
+
return e > this.length ? null : this.buf.subarray(this.start, this.start + e);
|
|
275
|
+
}
|
|
276
|
+
peekByte(e = 0) {
|
|
277
|
+
return e < this.length ? this.buf[this.start + e] : -1;
|
|
278
|
+
}
|
|
279
|
+
consume(e) {
|
|
280
|
+
let t = this.buf.slice(this.start, this.start + Math.min(e, this.length));
|
|
281
|
+
return this.skip(e), t;
|
|
282
|
+
}
|
|
283
|
+
skip(e) {
|
|
284
|
+
this.start = Math.min(this.start + e, this.end), this.start === this.end ? (this.start = 0, this.end = 0) : this.start > 1048576 && this.start > this.length * 2 && (this.buf.copyWithin(0, this.start, this.end), this.end -= this.start, this.start = 0);
|
|
285
|
+
}
|
|
286
|
+
clear() {
|
|
287
|
+
this.start = 0, this.end = 0;
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
// src/utils/bytes.ts
|
|
291
|
+
function _(...e) {
|
|
292
|
+
let t = 0;
|
|
293
|
+
for (let n of e) n && (t += n.byteLength);
|
|
294
|
+
let n = new Uint8Array(t), r = 0;
|
|
295
|
+
for (let t of e) t && (n.set(t, r), r += t.byteLength);
|
|
296
|
+
return n;
|
|
297
|
+
}
|
|
298
|
+
function v(e, t = 0, n = e.length) {
|
|
299
|
+
let r = "";
|
|
300
|
+
for (let i = t; i < n; i++) r += String.fromCharCode(e[i]);
|
|
301
|
+
return r;
|
|
302
|
+
}
|
|
303
|
+
function y(e, t = 2) {
|
|
304
|
+
return e.toString(16).toUpperCase().padStart(t, "0").slice(-t);
|
|
305
|
+
}
|
|
306
|
+
var b = class {
|
|
307
|
+
constructor(e, t = 0) {
|
|
308
|
+
r(this, "data", void 0), r(this, "view", void 0), r(this, "offset", void 0), this.data = e instanceof Uint8Array ? e : new Uint8Array(e), this.view = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength), this.offset = t;
|
|
309
|
+
}
|
|
310
|
+
get remaining() {
|
|
311
|
+
return this.data.length - this.offset;
|
|
312
|
+
}
|
|
313
|
+
get eof() {
|
|
314
|
+
return this.offset >= this.data.length;
|
|
315
|
+
}
|
|
316
|
+
need(e) {
|
|
317
|
+
if (this.offset + e > this.data.length) throw RangeError(`ByteReader: need ${e} bytes at ${this.offset}, have ${this.remaining}`);
|
|
318
|
+
}
|
|
319
|
+
u8() {
|
|
320
|
+
return this.need(1), this.data[this.offset++];
|
|
321
|
+
}
|
|
322
|
+
u16() {
|
|
323
|
+
this.need(2);
|
|
324
|
+
let e = this.view.getUint16(this.offset);
|
|
325
|
+
return this.offset += 2, e;
|
|
326
|
+
}
|
|
327
|
+
i16() {
|
|
328
|
+
this.need(2);
|
|
329
|
+
let e = this.view.getInt16(this.offset);
|
|
330
|
+
return this.offset += 2, e;
|
|
331
|
+
}
|
|
332
|
+
u24() {
|
|
333
|
+
this.need(3);
|
|
334
|
+
let e = this.data, t = this.offset;
|
|
335
|
+
return this.offset += 3, e[t] << 16 | e[t + 1] << 8 | e[t + 2];
|
|
336
|
+
}
|
|
337
|
+
i24() {
|
|
338
|
+
let e = this.u24();
|
|
339
|
+
return e & 8388608 ? e - 16777216 : e;
|
|
340
|
+
}
|
|
341
|
+
u32() {
|
|
342
|
+
this.need(4);
|
|
343
|
+
let e = this.view.getUint32(this.offset);
|
|
344
|
+
return this.offset += 4, e;
|
|
345
|
+
}
|
|
346
|
+
i32() {
|
|
347
|
+
this.need(4);
|
|
348
|
+
let e = this.view.getInt32(this.offset);
|
|
349
|
+
return this.offset += 4, e;
|
|
350
|
+
}
|
|
351
|
+
u64() {
|
|
352
|
+
this.need(8);
|
|
353
|
+
let e = this.view.getUint32(this.offset), t = this.view.getUint32(this.offset + 4);
|
|
354
|
+
return this.offset += 8, e * 4294967296 + t;
|
|
355
|
+
}
|
|
356
|
+
f64() {
|
|
357
|
+
this.need(8);
|
|
358
|
+
let e = this.view.getFloat64(this.offset);
|
|
359
|
+
return this.offset += 8, e;
|
|
360
|
+
}
|
|
361
|
+
fourcc() {
|
|
362
|
+
this.need(4);
|
|
363
|
+
let e = v(this.data, this.offset, this.offset + 4);
|
|
364
|
+
return this.offset += 4, e;
|
|
365
|
+
}
|
|
366
|
+
bytes(e) {
|
|
367
|
+
this.need(e);
|
|
368
|
+
let t = this.data.subarray(this.offset, this.offset + e);
|
|
369
|
+
return this.offset += e, t;
|
|
370
|
+
}
|
|
371
|
+
skip(e) {
|
|
372
|
+
this.need(e), this.offset += e;
|
|
373
|
+
}
|
|
374
|
+
seek(e) {
|
|
375
|
+
this.offset = e;
|
|
376
|
+
}
|
|
377
|
+
boxSize() {
|
|
378
|
+
let e = this.offset, t = this.u32();
|
|
379
|
+
this.fourcc();
|
|
380
|
+
let n = t, r = 8;
|
|
381
|
+
return t === 1 ? (n = this.u64(), r = 16) : t === 0 && (n = this.data.length - e), {
|
|
382
|
+
size: n,
|
|
383
|
+
headerSize: r
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
}, x = class {
|
|
387
|
+
constructor(e = 256) {
|
|
388
|
+
r(this, "buf", void 0), r(this, "view", void 0), r(this, "pos", 0), this.buf = new Uint8Array(e), this.view = new DataView(this.buf.buffer);
|
|
389
|
+
}
|
|
390
|
+
ensure(e) {
|
|
391
|
+
if (this.pos + e <= this.buf.length) return;
|
|
392
|
+
let t = Math.max(this.buf.length * 2, 256);
|
|
393
|
+
for (; t < this.pos + e;) t *= 2;
|
|
394
|
+
let n = new Uint8Array(t);
|
|
395
|
+
n.set(this.buf.subarray(0, this.pos)), this.buf = n, this.view = new DataView(n.buffer);
|
|
396
|
+
}
|
|
397
|
+
get length() {
|
|
398
|
+
return this.pos;
|
|
399
|
+
}
|
|
400
|
+
u8(e) {
|
|
401
|
+
return this.ensure(1), this.buf[this.pos++] = e & 255, this;
|
|
402
|
+
}
|
|
403
|
+
u16(e) {
|
|
404
|
+
return this.ensure(2), this.view.setUint16(this.pos, e & 65535), this.pos += 2, this;
|
|
405
|
+
}
|
|
406
|
+
i16(e) {
|
|
407
|
+
return this.ensure(2), this.view.setInt16(this.pos, e), this.pos += 2, this;
|
|
408
|
+
}
|
|
409
|
+
u24(e) {
|
|
410
|
+
return this.ensure(3), this.buf[this.pos++] = e >>> 16 & 255, this.buf[this.pos++] = e >>> 8 & 255, this.buf[this.pos++] = e & 255, this;
|
|
411
|
+
}
|
|
412
|
+
u32(e) {
|
|
413
|
+
return this.ensure(4), this.view.setUint32(this.pos, e >>> 0), this.pos += 4, this;
|
|
414
|
+
}
|
|
415
|
+
i32(e) {
|
|
416
|
+
return this.ensure(4), this.view.setInt32(this.pos, e | 0), this.pos += 4, this;
|
|
417
|
+
}
|
|
418
|
+
u64(e) {
|
|
419
|
+
let t = Math.floor(e / 4294967296), n = e >>> 0;
|
|
420
|
+
return this.u32(t), this.u32(n), this;
|
|
421
|
+
}
|
|
422
|
+
f64(e) {
|
|
423
|
+
return this.ensure(8), this.view.setFloat64(this.pos, e), this.pos += 8, this;
|
|
424
|
+
}
|
|
425
|
+
raw(e) {
|
|
426
|
+
return this.ensure(e.length), this.buf.set(e, this.pos), this.pos += e.length, this;
|
|
427
|
+
}
|
|
428
|
+
fourcc(e) {
|
|
429
|
+
this.ensure(4);
|
|
430
|
+
for (let t = 0; t < 4; t++) this.buf[this.pos++] = e.charCodeAt(t) & 255;
|
|
431
|
+
return this;
|
|
432
|
+
}
|
|
433
|
+
cstring(e) {
|
|
434
|
+
for (let t = 0; t < e.length; t++) this.u8(e.charCodeAt(t) & 255);
|
|
435
|
+
return this.u8(0), this;
|
|
436
|
+
}
|
|
437
|
+
toUint8Array() {
|
|
438
|
+
return this.buf.slice(0, this.pos);
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
function S(e, ...t) {
|
|
442
|
+
let n = _(...t), r = new Uint8Array(8 + n.length);
|
|
443
|
+
new DataView(r.buffer).setUint32(0, r.length);
|
|
444
|
+
for (let t = 0; t < 4; t++) r[4 + t] = e.charCodeAt(t) & 255;
|
|
445
|
+
return r.set(n, 8), r;
|
|
446
|
+
}
|
|
447
|
+
function C(e, t, n, ...r) {
|
|
448
|
+
let i = new x();
|
|
449
|
+
return i.u8(t).u24(n), S(e, i.toUint8Array(), ...r);
|
|
450
|
+
}
|
|
451
|
+
function w(e) {
|
|
452
|
+
return Math.round(e * 65536) >>> 0;
|
|
453
|
+
}
|
|
454
|
+
// src/protocol/amf0.ts
|
|
455
|
+
var T = new TextDecoder();
|
|
456
|
+
function E(e, t, n) {
|
|
457
|
+
return T.decode(e.subarray(t, t + n));
|
|
458
|
+
}
|
|
459
|
+
function D(e, t = 0) {
|
|
460
|
+
if (t >= e.length) return {
|
|
461
|
+
value: void 0,
|
|
462
|
+
offset: t
|
|
463
|
+
};
|
|
464
|
+
let n = new DataView(e.buffer, e.byteOffset, e.byteLength), r = e[t], i = t + 1;
|
|
465
|
+
switch (r) {
|
|
466
|
+
case 0: return {
|
|
467
|
+
value: n.getFloat64(i),
|
|
468
|
+
offset: i + 8
|
|
469
|
+
};
|
|
470
|
+
case 1: return {
|
|
471
|
+
value: e[i] !== 0,
|
|
472
|
+
offset: i + 1
|
|
473
|
+
};
|
|
474
|
+
case 2: {
|
|
475
|
+
let t = n.getUint16(i);
|
|
476
|
+
return {
|
|
477
|
+
value: E(e, i + 2, t),
|
|
478
|
+
offset: i + 2 + t
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
case 3:
|
|
482
|
+
i = t + 1;
|
|
483
|
+
break;
|
|
484
|
+
case 8:
|
|
485
|
+
i = t + 5;
|
|
486
|
+
break;
|
|
487
|
+
case 10: {
|
|
488
|
+
let t = n.getUint32(i);
|
|
489
|
+
i += 4;
|
|
490
|
+
let r = [];
|
|
491
|
+
for (let n = 0; n < t; n++) {
|
|
492
|
+
let t = D(e, i);
|
|
493
|
+
r.push(t.value), i = t.offset;
|
|
494
|
+
}
|
|
495
|
+
return {
|
|
496
|
+
value: r,
|
|
497
|
+
offset: i
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
case 5: return {
|
|
501
|
+
value: null,
|
|
502
|
+
offset: i
|
|
503
|
+
};
|
|
504
|
+
case 6: return {
|
|
505
|
+
value: void 0,
|
|
506
|
+
offset: i
|
|
507
|
+
};
|
|
508
|
+
case 11: return {
|
|
509
|
+
value: n.getFloat64(i),
|
|
510
|
+
offset: i + 10
|
|
511
|
+
};
|
|
512
|
+
case 12: {
|
|
513
|
+
let t = n.getUint32(i);
|
|
514
|
+
return {
|
|
515
|
+
value: E(e, i + 4, t),
|
|
516
|
+
offset: i + 4 + t
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
case 16: {
|
|
520
|
+
let e = n.getUint16(i);
|
|
521
|
+
i += 2 + e;
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
case 17: return {
|
|
525
|
+
value: void 0,
|
|
526
|
+
offset: i
|
|
527
|
+
};
|
|
528
|
+
default: return {
|
|
529
|
+
value: void 0,
|
|
530
|
+
offset: e.length
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
let a = {};
|
|
534
|
+
for (; i + 2 <= e.length;) {
|
|
535
|
+
let t = n.getUint16(i);
|
|
536
|
+
if (i += 2, t === 0) {
|
|
537
|
+
e[i] === 9 && i++;
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
let r = E(e, i, t);
|
|
541
|
+
i += t;
|
|
542
|
+
let o = D(e, i);
|
|
543
|
+
if (a[r] = o.value, i = o.offset, o.offset >= e.length) break;
|
|
544
|
+
}
|
|
545
|
+
return {
|
|
546
|
+
value: a,
|
|
547
|
+
offset: i
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
function ee(e) {
|
|
551
|
+
let t = [], n = 0;
|
|
552
|
+
for (; n < e.length;) {
|
|
553
|
+
let r = D(e, n);
|
|
554
|
+
if (r.offset <= n) break;
|
|
555
|
+
t.push(r.value), n = r.offset;
|
|
556
|
+
}
|
|
557
|
+
return t;
|
|
558
|
+
}
|
|
559
|
+
function te(e, t) {
|
|
560
|
+
let n = new TextEncoder().encode(t);
|
|
561
|
+
n.length > 65535 ? e.u8(12).u32(n.length).raw(n) : e.u8(2).u16(n.length).raw(n);
|
|
562
|
+
}
|
|
563
|
+
function ne(e) {
|
|
564
|
+
let t = new x(64);
|
|
565
|
+
return O(t, e), t.toUint8Array();
|
|
566
|
+
}
|
|
567
|
+
function O(e, t) {
|
|
568
|
+
if (t === null) e.u8(5);
|
|
569
|
+
else if (t === void 0) e.u8(6);
|
|
570
|
+
else switch (typeof t) {
|
|
571
|
+
case "number":
|
|
572
|
+
e.u8(0).f64(t);
|
|
573
|
+
return;
|
|
574
|
+
case "boolean":
|
|
575
|
+
e.u8(1).u8(+!!t);
|
|
576
|
+
return;
|
|
577
|
+
case "string":
|
|
578
|
+
te(e, t);
|
|
579
|
+
return;
|
|
580
|
+
case "object":
|
|
581
|
+
if (Array.isArray(t)) {
|
|
582
|
+
e.u8(10).u32(t.length);
|
|
583
|
+
for (let n of t) O(e, n);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
e.u8(3);
|
|
587
|
+
for (let [n, r] of Object.entries(t)) {
|
|
588
|
+
let t = new TextEncoder().encode(n);
|
|
589
|
+
e.u16(t.length).raw(t), O(e, r);
|
|
590
|
+
}
|
|
591
|
+
e.u16(0).u8(9);
|
|
592
|
+
return;
|
|
593
|
+
default: e.u8(6);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function re(...e) {
|
|
597
|
+
return _(...e.map(ne));
|
|
598
|
+
}
|
|
599
|
+
// src/codec/bit.ts
|
|
600
|
+
var ie = class {
|
|
601
|
+
constructor(e, t = 0) {
|
|
602
|
+
r(this, "data", void 0), r(this, "bitPos", 0), this.data = e, this.bitPos = t;
|
|
603
|
+
}
|
|
604
|
+
get bitsLeft() {
|
|
605
|
+
return this.data.length * 8 - this.bitPos;
|
|
606
|
+
}
|
|
607
|
+
skip(e) {
|
|
608
|
+
this.bitPos += e;
|
|
609
|
+
}
|
|
610
|
+
u1() {
|
|
611
|
+
let e = this.data[this.bitPos >> 3];
|
|
612
|
+
if (e === void 0) throw RangeError("BitReader: out of data");
|
|
613
|
+
let t = e >> 7 - (this.bitPos & 7) & 1;
|
|
614
|
+
return this.bitPos++, t;
|
|
615
|
+
}
|
|
616
|
+
u(e) {
|
|
617
|
+
let t = 0;
|
|
618
|
+
for (let n = 0; n < e; n++) t = t * 2 + this.u1();
|
|
619
|
+
return t;
|
|
620
|
+
}
|
|
621
|
+
ue() {
|
|
622
|
+
let e = 0;
|
|
623
|
+
for (; this.u1() === 0;) if (e++, e > 32) throw RangeError("BitReader: invalid exp-golomb code");
|
|
624
|
+
return e === 0 ? 0 : 2 ** e - 1 + this.u(e);
|
|
625
|
+
}
|
|
626
|
+
se() {
|
|
627
|
+
let e = this.ue();
|
|
628
|
+
return e & 1 ? (e + 1) / 2 : -(e / 2);
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
function ae(e) {
|
|
632
|
+
let t = !1;
|
|
633
|
+
for (let n = 2; n < e.length; n++) if (e[n] === 3 && e[n - 1] === 0 && e[n - 2] === 0) {
|
|
634
|
+
t = !0;
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
if (!t) return e;
|
|
638
|
+
let n = new Uint8Array(e.length), r = 0, i = 0;
|
|
639
|
+
for (let t = 0; t < e.length; t++) {
|
|
640
|
+
let a = e[t];
|
|
641
|
+
i >= 2 && a === 3 ? i = 0 : (n[r++] = a, i = a === 0 ? i + 1 : 0);
|
|
642
|
+
}
|
|
643
|
+
return n.subarray(0, r);
|
|
644
|
+
}
|
|
645
|
+
// src/codec/h264.ts
|
|
646
|
+
var k = {
|
|
647
|
+
SLICE: 1,
|
|
648
|
+
IDR: 5,
|
|
649
|
+
SEI: 6,
|
|
650
|
+
SPS: 7,
|
|
651
|
+
PPS: 8,
|
|
652
|
+
AUD: 9
|
|
653
|
+
};
|
|
654
|
+
function A(e) {
|
|
655
|
+
let t = [], n = -1, r = 0;
|
|
656
|
+
for (; r + 2 < e.length;) if (e[r] === 0 && e[r + 1] === 0 && e[r + 2] === 1) {
|
|
657
|
+
if (n >= 0) {
|
|
658
|
+
let i = r;
|
|
659
|
+
for (; i > n && e[i - 1] === 0;) i--;
|
|
660
|
+
i > n && t.push(e.subarray(n, i));
|
|
661
|
+
}
|
|
662
|
+
r += 3, n = r;
|
|
663
|
+
} else r++;
|
|
664
|
+
if (n >= 0) {
|
|
665
|
+
let r = e.length;
|
|
666
|
+
for (; r > n && e[r - 1] === 0;) r--;
|
|
667
|
+
r > n && t.push(e.subarray(n, r));
|
|
668
|
+
}
|
|
669
|
+
return t;
|
|
670
|
+
}
|
|
671
|
+
function oe(e, t = 4) {
|
|
672
|
+
let n = [];
|
|
673
|
+
for (let r of e) {
|
|
674
|
+
let e = new Uint8Array(t), i = new DataView(e.buffer);
|
|
675
|
+
t === 4 ? i.setUint32(0, r.length) : t === 2 ? i.setUint16(0, r.length) : (e[0] = r.length >>> 16 & 255, e[1] = r.length >>> 8 & 255, e[2] = r.length & 255), n.push(e, r);
|
|
676
|
+
}
|
|
677
|
+
return _(...n);
|
|
678
|
+
}
|
|
679
|
+
function se(e, t) {
|
|
680
|
+
let n = 8, r = 8;
|
|
681
|
+
for (let i = 0; i < t; i++) r !== 0 && (r = (n + e.se() + 256) % 256), n = r === 0 ? n : r;
|
|
682
|
+
}
|
|
683
|
+
function ce(e) {
|
|
684
|
+
let t = new ie(ae(e.subarray(1))), n = t.u(8), r = t.u(8), i = t.u(8);
|
|
685
|
+
t.ue();
|
|
686
|
+
let a = 1, o = 8, s = 8;
|
|
687
|
+
if ([
|
|
688
|
+
100,
|
|
689
|
+
110,
|
|
690
|
+
122,
|
|
691
|
+
244,
|
|
692
|
+
44,
|
|
693
|
+
83,
|
|
694
|
+
86,
|
|
695
|
+
118,
|
|
696
|
+
128,
|
|
697
|
+
138,
|
|
698
|
+
139,
|
|
699
|
+
134,
|
|
700
|
+
135
|
|
701
|
+
].includes(n) && (a = t.ue(), a === 3 && t.u(1), o = t.ue() + 8, s = t.ue() + 8, t.u(1), t.u(1))) {
|
|
702
|
+
let e = a === 3 ? 12 : 8;
|
|
703
|
+
for (let n = 0; n < e; n++) t.u(1) && se(t, n < 6 ? 16 : 64);
|
|
704
|
+
}
|
|
705
|
+
t.ue();
|
|
706
|
+
let c = t.ue();
|
|
707
|
+
if (c === 0) t.ue();
|
|
708
|
+
else if (c === 1) {
|
|
709
|
+
t.u(1), t.se(), t.se();
|
|
710
|
+
let e = t.ue();
|
|
711
|
+
for (let n = 0; n < e; n++) t.se();
|
|
712
|
+
}
|
|
713
|
+
t.ue(), t.u(1);
|
|
714
|
+
let l = t.ue() + 1, u = t.ue() + 1, d = t.u(1);
|
|
715
|
+
d || t.u(1), t.u(1);
|
|
716
|
+
let f = 0, p = 0, m = 0, h = 0;
|
|
717
|
+
t.u(1) && (f = t.ue(), p = t.ue(), m = t.ue(), h = t.ue());
|
|
718
|
+
let g = a === 1 || a === 2 ? 2 : 1, _ = a === 1 ? 2 : 1, v = l * 16 - (f + p) * g, y = (2 - d) * u * 16 - (m + h) * _, b;
|
|
719
|
+
try {
|
|
720
|
+
if (t.u(1) && (t.u(1) && t.u(8) === 255 && (t.u(16), t.u(16)), t.u(1) && t.u(1), t.u(1) && (t.u(3), t.u(1), t.u(1) && (t.u(8), t.u(8), t.u(8))), t.u(1) && (t.ue(), t.ue()), t.u(1))) {
|
|
721
|
+
let e = t.u(32), n = t.u(32);
|
|
722
|
+
e > 0 && (b = Math.round(n / (2 * e) * 100) / 100);
|
|
723
|
+
}
|
|
724
|
+
} catch {}
|
|
725
|
+
return (v <= 0 || y <= 0) && (v = v || 0, y = y || 0), {
|
|
726
|
+
profile: n,
|
|
727
|
+
compat: r,
|
|
728
|
+
level: i,
|
|
729
|
+
width: v,
|
|
730
|
+
height: y,
|
|
731
|
+
fps: b,
|
|
732
|
+
chromaFormat: a,
|
|
733
|
+
bitDepthLuma: o,
|
|
734
|
+
bitDepthChroma: s
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
function le(e, t, n, r = 4) {
|
|
738
|
+
let i = new x();
|
|
739
|
+
i.u8(1), i.u8(n.profile), i.u8(n.compat), i.u8(n.level), i.u8(252 | r - 1 & 3), i.u8(224 | e.length & 31);
|
|
740
|
+
for (let t of e) i.u16(t.length), i.raw(t);
|
|
741
|
+
i.u8(t.length & 255);
|
|
742
|
+
for (let e of t) i.u16(e.length), i.raw(e);
|
|
743
|
+
return i.toUint8Array();
|
|
744
|
+
}
|
|
745
|
+
function ue(e) {
|
|
746
|
+
return e.length < 4 ? "avc1.42E01E" : `avc1.${y(e[1])}${y(e[2])}${y(e[3])}`;
|
|
747
|
+
}
|
|
748
|
+
function de(e) {
|
|
749
|
+
return {
|
|
750
|
+
profile: e[1] ?? 0,
|
|
751
|
+
compat: e[2] ?? 0,
|
|
752
|
+
level: e[3] ?? 0
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
function fe(e, t = 4) {
|
|
756
|
+
let n = [], r = 0;
|
|
757
|
+
for (; r + t <= e.length;) {
|
|
758
|
+
let i = 0;
|
|
759
|
+
for (let n = 0; n < t; n++) i = i * 256 + e[r + n];
|
|
760
|
+
if (r += t, i <= 0 || r + i > e.length) break;
|
|
761
|
+
n.push(Uint8Array.of(0, 0, 0, 1), e.subarray(r, r + i)), r += i;
|
|
762
|
+
}
|
|
763
|
+
return _(...n);
|
|
764
|
+
}
|
|
765
|
+
function pe(e, t = 4) {
|
|
766
|
+
let n = 0;
|
|
767
|
+
for (; n + t <= e.length;) {
|
|
768
|
+
let r = 0;
|
|
769
|
+
for (let i = 0; i < t; i++) r = r * 256 + e[n + i];
|
|
770
|
+
if (n += t, r <= 0 || n >= e.length) return !1;
|
|
771
|
+
let i = e[n] & 31;
|
|
772
|
+
if (i === k.IDR || i === k.SPS) return !0;
|
|
773
|
+
if (i === k.SEI || i === k.AUD) n += r;
|
|
774
|
+
else return !1;
|
|
775
|
+
}
|
|
776
|
+
return !1;
|
|
777
|
+
}
|
|
778
|
+
// src/codec/h265.ts
|
|
779
|
+
var me = {
|
|
780
|
+
VPS: 32,
|
|
781
|
+
SPS: 33,
|
|
782
|
+
PPS: 34,
|
|
783
|
+
AUD: 35,
|
|
784
|
+
PREFIX_SEI: 39,
|
|
785
|
+
SUFFIX_SEI: 40
|
|
786
|
+
};
|
|
787
|
+
function he(e) {
|
|
788
|
+
return e[0] >> 1 & 63;
|
|
789
|
+
}
|
|
790
|
+
function ge(e, t) {
|
|
791
|
+
let n = e.u(2), r = e.u(1), i = e.u(5), a = e.u(32), o = /* @__PURE__ */ new Uint8Array(6);
|
|
792
|
+
for (let t = 0; t < 48; t++) e.u(1) && (o[t >> 3] |= 128 >> (t & 7));
|
|
793
|
+
let s = e.u(8), c = [], l = [];
|
|
794
|
+
for (let n = 0; n < t; n++) c.push(e.u(1)), l.push(e.u(1));
|
|
795
|
+
if (t > 0) for (let n = t; n < 8; n++) e.u(2);
|
|
796
|
+
for (let n = 0; n < t; n++) c[n] && (e.u(2), e.u(1), e.u(5), e.u(32), e.u(48)), l[n] && e.u(8);
|
|
797
|
+
return {
|
|
798
|
+
profileSpace: n,
|
|
799
|
+
tier: r,
|
|
800
|
+
profileIdc: i,
|
|
801
|
+
compatFlags: a,
|
|
802
|
+
constraintFlags: o,
|
|
803
|
+
levelIdc: s
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
function _e(e) {
|
|
807
|
+
let t = new ie(ae(e.subarray(2)));
|
|
808
|
+
t.u(4);
|
|
809
|
+
let n = t.u(3) + 1;
|
|
810
|
+
t.u(1);
|
|
811
|
+
let r = ge(t, n - 1);
|
|
812
|
+
t.ue();
|
|
813
|
+
let i = t.ue();
|
|
814
|
+
i === 3 && t.u(1);
|
|
815
|
+
let a = t.ue(), o = t.ue(), s = 0, c = 0, l = 0, u = 0;
|
|
816
|
+
t.u(1) && (s = t.ue(), c = t.ue(), l = t.ue(), u = t.ue());
|
|
817
|
+
let d = t.ue() + 8, f = t.ue() + 8, p = i === 1 || i === 2 ? 2 : 1, m = i === 1 ? 2 : 1;
|
|
818
|
+
return {
|
|
819
|
+
...r,
|
|
820
|
+
width: a - (s + c) * p,
|
|
821
|
+
height: o - (l + u) * m,
|
|
822
|
+
chromaFormat: i,
|
|
823
|
+
bitDepthLuma: d,
|
|
824
|
+
bitDepthChroma: f,
|
|
825
|
+
maxSubLayers: n
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
function ve(e, t, n, r, i = 4) {
|
|
829
|
+
let a = new x();
|
|
830
|
+
a.u8(1), a.u8(e.profileSpace << 6 | e.tier << 5 | e.profileIdc & 31), a.u32(e.compatFlags >>> 0), a.raw(e.constraintFlags.subarray(0, 6)), a.u8(e.levelIdc), a.u16(61440), a.u8(252), a.u8(252 | e.chromaFormat & 3), a.u8(248 | e.bitDepthLuma - 8 & 7), a.u8(248 | e.bitDepthChroma - 8 & 7), a.u16(0), a.u8(e.maxSubLayers - 1 << 3 | 4 | i - 1 & 3), a.u8(3);
|
|
831
|
+
let o = [
|
|
832
|
+
[me.VPS, t],
|
|
833
|
+
[me.SPS, n],
|
|
834
|
+
[me.PPS, r]
|
|
835
|
+
];
|
|
836
|
+
for (let [e, t] of o) a.u8(128 | e & 63), a.u16(1), a.u16(t.length), a.raw(t);
|
|
837
|
+
return a.toUint8Array();
|
|
838
|
+
}
|
|
839
|
+
function j(e) {
|
|
840
|
+
if (e.length < 13) return "hvc1.1.6.L93.B0";
|
|
841
|
+
let t = e[1] >> 6 & 3, n = e[1] >> 5 & 1, r = e[1] & 31, i = (e[2] << 24 | e[3] << 16 | e[4] << 8 | e[5]) >>> 0, a = 0;
|
|
842
|
+
for (let e = 0; e < 32; e++) i & 1 << e && (a |= 1 << 31 - e);
|
|
843
|
+
let o = e[12], s = [];
|
|
844
|
+
for (let t = 6; t < 12; t++) s.push(e[t]);
|
|
845
|
+
for (; s.length > 0 && s[s.length - 1] === 0;) s.pop();
|
|
846
|
+
let c = `hvc1.${[
|
|
847
|
+
"",
|
|
848
|
+
"A",
|
|
849
|
+
"B",
|
|
850
|
+
"C"
|
|
851
|
+
][t] ?? ""}${r}.${a.toString(16).toUpperCase()}.${n ? "H" : "L"}${o}`;
|
|
852
|
+
for (let e of s) c += `.${y(e)}`;
|
|
853
|
+
return c;
|
|
854
|
+
}
|
|
855
|
+
function ye(e, t = 4) {
|
|
856
|
+
let n = 0;
|
|
857
|
+
for (; n + t <= e.length;) {
|
|
858
|
+
let r = 0;
|
|
859
|
+
for (let i = 0; i < t; i++) r = r * 256 + e[n + i];
|
|
860
|
+
if (n += t, r <= 0 || n >= e.length) return !1;
|
|
861
|
+
let i = he(e.subarray(n, n + Math.min(r, 2)));
|
|
862
|
+
if (i <= 31) return i >= 16 && i <= 23;
|
|
863
|
+
n += r;
|
|
864
|
+
}
|
|
865
|
+
return !1;
|
|
866
|
+
}
|
|
867
|
+
// src/codec/config.ts
|
|
868
|
+
function M(e) {
|
|
869
|
+
let t = [], n = [], r = 4, i = null;
|
|
870
|
+
if (e.length < 7) return {
|
|
871
|
+
nalLengthSize: r,
|
|
872
|
+
sps: t,
|
|
873
|
+
pps: n,
|
|
874
|
+
info: i
|
|
875
|
+
};
|
|
876
|
+
let a = new b(e);
|
|
877
|
+
a.u8(), a.u8(), a.u8(), a.u8(), r = (a.u8() & 3) + 1;
|
|
878
|
+
let o = a.u8() & 31;
|
|
879
|
+
try {
|
|
880
|
+
for (let n = 0; n < o; n++) {
|
|
881
|
+
let n = a.u16();
|
|
882
|
+
t.push(e.subarray(a.offset, a.offset + n)), a.skip(n);
|
|
883
|
+
}
|
|
884
|
+
let r = a.u8();
|
|
885
|
+
for (let t = 0; t < r; t++) {
|
|
886
|
+
let t = a.u16();
|
|
887
|
+
n.push(e.subarray(a.offset, a.offset + t)), a.skip(t);
|
|
888
|
+
}
|
|
889
|
+
t.length > 0 && (i = ce(t[0]));
|
|
890
|
+
} catch {}
|
|
891
|
+
return {
|
|
892
|
+
nalLengthSize: r,
|
|
893
|
+
sps: t,
|
|
894
|
+
pps: n,
|
|
895
|
+
info: i
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
function N(e) {
|
|
899
|
+
let t = [], n = [], r = [], i = 4, a = null;
|
|
900
|
+
if (e.length < 23) return {
|
|
901
|
+
nalLengthSize: i,
|
|
902
|
+
vps: t,
|
|
903
|
+
sps: n,
|
|
904
|
+
pps: r,
|
|
905
|
+
info: a
|
|
906
|
+
};
|
|
907
|
+
i = (e[21] & 3) + 1;
|
|
908
|
+
let o = e[22], s = 23;
|
|
909
|
+
try {
|
|
910
|
+
for (let i = 0; i < o; i++) {
|
|
911
|
+
let i = e[s] & 63, a = e[s + 1] << 8 | e[s + 2];
|
|
912
|
+
s += 3;
|
|
913
|
+
for (let o = 0; o < a; o++) {
|
|
914
|
+
let a = e[s] << 8 | e[s + 1];
|
|
915
|
+
s += 2;
|
|
916
|
+
let o = e.subarray(s, s + a);
|
|
917
|
+
s += a, i === 32 ? t.push(o) : i === 33 ? n.push(o) : i === 34 && r.push(o);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
n.length > 0 && (a = _e(n[0]));
|
|
921
|
+
} catch {}
|
|
922
|
+
return {
|
|
923
|
+
nalLengthSize: i,
|
|
924
|
+
vps: t,
|
|
925
|
+
sps: n,
|
|
926
|
+
pps: r,
|
|
927
|
+
info: a
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
function be(e, t) {
|
|
931
|
+
let n = e[t] << 16 | e[t + 1] << 8 | e[t + 2];
|
|
932
|
+
return n & 8388608 ? n - 16777216 : n;
|
|
933
|
+
}
|
|
934
|
+
// src/codec/aac.ts
|
|
935
|
+
var xe = [
|
|
936
|
+
96e3,
|
|
937
|
+
88200,
|
|
938
|
+
64e3,
|
|
939
|
+
48e3,
|
|
940
|
+
44100,
|
|
941
|
+
32e3,
|
|
942
|
+
24e3,
|
|
943
|
+
22050,
|
|
944
|
+
16e3,
|
|
945
|
+
12e3,
|
|
946
|
+
11025,
|
|
947
|
+
8e3,
|
|
948
|
+
7350,
|
|
949
|
+
0,
|
|
950
|
+
0,
|
|
951
|
+
0
|
|
952
|
+
];
|
|
953
|
+
function Se(e, t = 0) {
|
|
954
|
+
if (t + 7 > e.length || e[t] !== 255 || (e[t + 1] & 240) != 240) return null;
|
|
955
|
+
let n = e[t + 1] & 1, r = e[t + 2] >> 6 & 3, i = e[t + 2] >> 2 & 15, a = (e[t + 2] & 1) << 2 | e[t + 3] >> 6 & 3, o = (e[t + 3] & 3) << 11 | e[t + 4] << 3 | e[t + 5] >> 5 & 7;
|
|
956
|
+
return o < 7 ? null : {
|
|
957
|
+
objectType: r + 1,
|
|
958
|
+
sampleRate: xe[i] || 44100,
|
|
959
|
+
sampleRateIndex: i,
|
|
960
|
+
channels: a || 2,
|
|
961
|
+
frameLength: o,
|
|
962
|
+
headerLength: n ? 7 : 9
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
function P(e, t, n) {
|
|
966
|
+
let r = xe.indexOf(t);
|
|
967
|
+
(r < 0 || r > 12) && (r = 4);
|
|
968
|
+
let i = e << 11 | r << 7 | (n & 15) << 3;
|
|
969
|
+
return Uint8Array.of(i >> 8 & 255, i & 255);
|
|
970
|
+
}
|
|
971
|
+
function F(e) {
|
|
972
|
+
let t = 0, n = (n) => {
|
|
973
|
+
let r = 0;
|
|
974
|
+
for (let i = 0; i < n; i++) {
|
|
975
|
+
let n = e[t >> 3] ?? 0;
|
|
976
|
+
r = r * 2 + (n >> 7 - (t & 7) & 1), t++;
|
|
977
|
+
}
|
|
978
|
+
return r;
|
|
979
|
+
}, r = n(5);
|
|
980
|
+
r === 31 && (r = 32 + n(6));
|
|
981
|
+
let i = n(4), a = i === 15 ? n(24) : xe[i] || 44100, o = n(4);
|
|
982
|
+
return {
|
|
983
|
+
objectType: r,
|
|
984
|
+
sampleRate: a,
|
|
985
|
+
channels: o || 2
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
function I(e) {
|
|
989
|
+
if (!e || e.length < 2) return "mp4a.40.2";
|
|
990
|
+
let { objectType: t } = F(e);
|
|
991
|
+
return `mp4a.40.${t}`;
|
|
992
|
+
}
|
|
993
|
+
// src/demux/flv.ts
|
|
994
|
+
var Ce = 8, we = 9, Te = 18, L = 1e3, Ee = class {
|
|
995
|
+
constructor(e) {
|
|
996
|
+
r(this, "onTag", void 0), r(this, "buf", new g()), r(this, "headerParsed", !1), this.onTag = e;
|
|
997
|
+
}
|
|
998
|
+
push(e) {
|
|
999
|
+
this.buf.append(e), this.parse();
|
|
1000
|
+
}
|
|
1001
|
+
flush() {}
|
|
1002
|
+
reset() {
|
|
1003
|
+
this.buf.clear(), this.headerParsed = !1;
|
|
1004
|
+
}
|
|
1005
|
+
parse() {
|
|
1006
|
+
if (!this.headerParsed) {
|
|
1007
|
+
if (this.buf.length < 3) return;
|
|
1008
|
+
let e = this.buf.peek(3);
|
|
1009
|
+
if (e[0] === 70 && e[1] === 76 && e[2] === 86) {
|
|
1010
|
+
if (this.buf.length < 13) return;
|
|
1011
|
+
this.buf.skip(13);
|
|
1012
|
+
}
|
|
1013
|
+
this.headerParsed = !0;
|
|
1014
|
+
}
|
|
1015
|
+
for (;;) {
|
|
1016
|
+
let e = this.buf.peek(11);
|
|
1017
|
+
if (!e) return;
|
|
1018
|
+
let t = e[0];
|
|
1019
|
+
if (t !== Ce && t !== we && t !== Te) {
|
|
1020
|
+
this.buf.skip(1);
|
|
1021
|
+
continue;
|
|
1022
|
+
}
|
|
1023
|
+
let n = e[1] << 16 | e[2] << 8 | e[3], r = (e[7] << 24 | e[4] << 16 | e[5] << 8 | e[6]) >>> 0, i = 11 + n + 4, a = this.buf.peek(i);
|
|
1024
|
+
if (!a) return;
|
|
1025
|
+
let o = a.subarray(11, 11 + n);
|
|
1026
|
+
try {
|
|
1027
|
+
this.onTag(t, r, o);
|
|
1028
|
+
} finally {
|
|
1029
|
+
this.buf.skip(i);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
}, De = class {
|
|
1034
|
+
constructor(e) {
|
|
1035
|
+
r(this, "sink", void 0), r(this, "format", "flv"), r(this, "parser", void 0), r(this, "videoTrack", null), r(this, "audioTrack", null), r(this, "meta", {}), r(this, "warnedUnsupported", /* @__PURE__ */ new Set()), this.sink = e, this.parser = new Ee((e, t, n) => this.handleTag(e, t, n));
|
|
1036
|
+
}
|
|
1037
|
+
push(e) {
|
|
1038
|
+
this.parser.push(e);
|
|
1039
|
+
}
|
|
1040
|
+
pushTag(e, t, n) {
|
|
1041
|
+
this.handleTag(e, t, n);
|
|
1042
|
+
}
|
|
1043
|
+
flush() {
|
|
1044
|
+
this.parser.flush();
|
|
1045
|
+
}
|
|
1046
|
+
reset() {
|
|
1047
|
+
this.parser.reset(), this.videoTrack = null, this.audioTrack = null, this.meta = {}, this.warnedUnsupported.clear();
|
|
1048
|
+
}
|
|
1049
|
+
tracks() {
|
|
1050
|
+
let e = [];
|
|
1051
|
+
return this.videoTrack && e.push(this.videoTrack), this.audioTrack && e.push(this.audioTrack), e;
|
|
1052
|
+
}
|
|
1053
|
+
handleTag(e, t, n) {
|
|
1054
|
+
e === we ? this.handleVideo(t, n) : e === Ce ? this.handleAudio(t, n) : e === Te && this.handleScript(n);
|
|
1055
|
+
}
|
|
1056
|
+
handleScript(e) {
|
|
1057
|
+
try {
|
|
1058
|
+
let t = ee(e);
|
|
1059
|
+
t[0] === "onMetaData" && t[1] && typeof t[1] == "object" && !Array.isArray(t[1]) && (this.meta = t[1], this.videoTrack && this.applyMetaSize(this.videoTrack));
|
|
1060
|
+
} catch {}
|
|
1061
|
+
}
|
|
1062
|
+
applyMetaSize(e) {
|
|
1063
|
+
typeof this.meta.width == "number" && !e.width && (e.width = this.meta.width), typeof this.meta.height == "number" && !e.height && (e.height = this.meta.height), typeof this.meta.framerate == "number" && !e.fps && (e.fps = this.meta.framerate);
|
|
1064
|
+
}
|
|
1065
|
+
handleVideo(e, t) {
|
|
1066
|
+
if (t.length < 2) return;
|
|
1067
|
+
let n = t[0], r = n & 128 ? n >> 4 & 7 : n >> 4 & 15, i, a, o = 0, s = null, c = !1;
|
|
1068
|
+
if (n & 128) {
|
|
1069
|
+
if (a = n & 15, t.length < 5) return;
|
|
1070
|
+
let e = v(t, 1, 5);
|
|
1071
|
+
if (i = e === "hvc1" ? 12 : e === "avc1" ? 7 : -1, i < 0) {
|
|
1072
|
+
this.warnOnce(1e3 + t[1], `unsupported enhanced video fourCC ${e}`);
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
if (a === 0) c = !0, s = t.subarray(5);
|
|
1076
|
+
else if (a === 1) {
|
|
1077
|
+
if (t.length < 8) return;
|
|
1078
|
+
o = be(t, 5), s = t.subarray(8);
|
|
1079
|
+
} else if (a === 3) s = t.subarray(5);
|
|
1080
|
+
else return;
|
|
1081
|
+
} else if (i = n & 15, a = t[1], i === 7 || i === 12) {
|
|
1082
|
+
if (a === 0) c = !0, s = t.subarray(5);
|
|
1083
|
+
else if (a === 1) o = be(t, 2), s = t.subarray(5);
|
|
1084
|
+
else return;
|
|
1085
|
+
} else if (i === 2) {
|
|
1086
|
+
this.warnOnce(2, "Sorenson H.263 video is not supported");
|
|
1087
|
+
return;
|
|
1088
|
+
} else {
|
|
1089
|
+
this.warnOnce(i, `unsupported FLV video codec id ${i}`);
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
if (c) {
|
|
1093
|
+
i === 7 ? this.configureAvc(s) : this.configureHevc(s);
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
if (!s || s.length === 0 || !this.videoTrack) return;
|
|
1097
|
+
let l = r === 1 || (this.videoTrack.codec === "avc" ? pe(s) : ye(s));
|
|
1098
|
+
this.sink.onSample({
|
|
1099
|
+
trackId: this.videoTrack.id,
|
|
1100
|
+
kind: "video",
|
|
1101
|
+
dts: e,
|
|
1102
|
+
pts: e + o,
|
|
1103
|
+
duration: 0,
|
|
1104
|
+
keyframe: l,
|
|
1105
|
+
data: s.slice()
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
configureAvc(e) {
|
|
1109
|
+
let t = M(e).info, n = {
|
|
1110
|
+
kind: "video",
|
|
1111
|
+
id: 1,
|
|
1112
|
+
codec: "avc",
|
|
1113
|
+
timescale: L,
|
|
1114
|
+
width: t?.width ?? 0,
|
|
1115
|
+
height: t?.height ?? 0,
|
|
1116
|
+
fps: t?.fps,
|
|
1117
|
+
codecPrivate: e.slice(),
|
|
1118
|
+
profile: t?.profile,
|
|
1119
|
+
level: t?.level
|
|
1120
|
+
};
|
|
1121
|
+
this.applyMetaSize(n), this.videoTrack = n, this.sink.onTracks(this.tracks());
|
|
1122
|
+
}
|
|
1123
|
+
configureHevc(e) {
|
|
1124
|
+
let t = N(e).info, n = {
|
|
1125
|
+
kind: "video",
|
|
1126
|
+
id: 1,
|
|
1127
|
+
codec: "hevc",
|
|
1128
|
+
timescale: L,
|
|
1129
|
+
width: t?.width ?? 0,
|
|
1130
|
+
height: t?.height ?? 0,
|
|
1131
|
+
codecPrivate: e.slice(),
|
|
1132
|
+
profile: t?.profileIdc,
|
|
1133
|
+
level: t?.levelIdc
|
|
1134
|
+
};
|
|
1135
|
+
this.applyMetaSize(n), this.videoTrack = n, this.sink.onTracks(this.tracks());
|
|
1136
|
+
}
|
|
1137
|
+
handleAudio(e, t) {
|
|
1138
|
+
if (t.length < 2) return;
|
|
1139
|
+
let n = t[0], r = n >> 4 & 15, i = n >> 2 & 3, a = n & 1;
|
|
1140
|
+
if (r === 10) {
|
|
1141
|
+
if (t[1] === 0) {
|
|
1142
|
+
this.configureAac(t.subarray(2));
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
if (!this.audioTrack) return;
|
|
1146
|
+
this.sink.onSample({
|
|
1147
|
+
trackId: this.audioTrack.id,
|
|
1148
|
+
kind: "audio",
|
|
1149
|
+
dts: e,
|
|
1150
|
+
pts: e,
|
|
1151
|
+
duration: 0,
|
|
1152
|
+
keyframe: !0,
|
|
1153
|
+
data: t.slice(2)
|
|
1154
|
+
});
|
|
1155
|
+
} else if (r === 7 || r === 8) {
|
|
1156
|
+
let n = r === 7 ? "g711a" : "g711u";
|
|
1157
|
+
this.audioTrack && this.audioTrack.codec === n || (this.audioTrack = {
|
|
1158
|
+
kind: "audio",
|
|
1159
|
+
id: 2,
|
|
1160
|
+
codec: n,
|
|
1161
|
+
timescale: L,
|
|
1162
|
+
sampleRate: 8e3,
|
|
1163
|
+
channels: a + 1
|
|
1164
|
+
}, this.sink.onTracks(this.tracks())), this.sink.onSample({
|
|
1165
|
+
trackId: this.audioTrack.id,
|
|
1166
|
+
kind: "audio",
|
|
1167
|
+
dts: e,
|
|
1168
|
+
pts: e,
|
|
1169
|
+
duration: 0,
|
|
1170
|
+
keyframe: !0,
|
|
1171
|
+
data: t.slice(1)
|
|
1172
|
+
});
|
|
1173
|
+
} else r === 2 ? (this.audioTrack || (this.audioTrack = {
|
|
1174
|
+
kind: "audio",
|
|
1175
|
+
id: 2,
|
|
1176
|
+
codec: "mp3",
|
|
1177
|
+
timescale: L,
|
|
1178
|
+
sampleRate: [
|
|
1179
|
+
5512,
|
|
1180
|
+
11025,
|
|
1181
|
+
22050,
|
|
1182
|
+
44100
|
|
1183
|
+
][i] ?? 44100,
|
|
1184
|
+
channels: a + 1
|
|
1185
|
+
}, this.sink.onTracks(this.tracks())), this.sink.onSample({
|
|
1186
|
+
trackId: this.audioTrack.id,
|
|
1187
|
+
kind: "audio",
|
|
1188
|
+
dts: e,
|
|
1189
|
+
pts: e,
|
|
1190
|
+
duration: 0,
|
|
1191
|
+
keyframe: !0,
|
|
1192
|
+
data: t.slice(1)
|
|
1193
|
+
})) : (r === 0 || r === 3) && this.warnOnce(20 + r, "uncompressed FLV audio is not supported");
|
|
1194
|
+
}
|
|
1195
|
+
configureAac(e) {
|
|
1196
|
+
let t = F(e), n = {
|
|
1197
|
+
kind: "audio",
|
|
1198
|
+
id: 2,
|
|
1199
|
+
codec: "aac",
|
|
1200
|
+
timescale: L,
|
|
1201
|
+
sampleRate: t.sampleRate,
|
|
1202
|
+
channels: t.channels,
|
|
1203
|
+
codecPrivate: e.slice()
|
|
1204
|
+
};
|
|
1205
|
+
this.audioTrack = n, this.sink.onTracks(this.tracks());
|
|
1206
|
+
}
|
|
1207
|
+
warnOnce(e, t) {
|
|
1208
|
+
this.warnedUnsupported.has(e) || (this.warnedUnsupported.add(e), this.sink.onError?.(Error(t)));
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
// src/demux/mp4.ts
|
|
1212
|
+
function R(e) {
|
|
1213
|
+
let t = [], n = new DataView(e.buffer, e.byteOffset, e.byteLength), r = 0;
|
|
1214
|
+
for (; r + 8 <= e.length;) {
|
|
1215
|
+
let i = n.getUint32(r), a = v(e, r + 4, r + 8), o = 8, s = i;
|
|
1216
|
+
if (i === 1) {
|
|
1217
|
+
if (r + 16 > e.length) break;
|
|
1218
|
+
s = Number(n.getBigUint64(r + 8)), o = 16;
|
|
1219
|
+
} else i === 0 && (s = e.length - r);
|
|
1220
|
+
if (s < o || r + s > e.length) break;
|
|
1221
|
+
t.push({
|
|
1222
|
+
type: a,
|
|
1223
|
+
start: r,
|
|
1224
|
+
end: r + s,
|
|
1225
|
+
headerSize: o,
|
|
1226
|
+
payload: e.subarray(r + o, r + s)
|
|
1227
|
+
}), r += s;
|
|
1228
|
+
}
|
|
1229
|
+
return t;
|
|
1230
|
+
}
|
|
1231
|
+
function z(e, t) {
|
|
1232
|
+
return e.find((e) => e.type === t);
|
|
1233
|
+
}
|
|
1234
|
+
function B(e, t) {
|
|
1235
|
+
return e.filter((e) => e.type === t);
|
|
1236
|
+
}
|
|
1237
|
+
var V = class {
|
|
1238
|
+
constructor(e) {
|
|
1239
|
+
r(this, "sink", void 0), r(this, "format", "mp4"), r(this, "buf", new g(65536)), r(this, "bufStart", 0), r(this, "skipBytes", 0), r(this, "state", "boxes"), r(this, "mdatEnd", 0), r(this, "pending", []), r(this, "pendingIndex", 0), r(this, "moovDone", !1), r(this, "sawMdatFirst", !1), r(this, "mediaDuration", 0), r(this, "tracks", /* @__PURE__ */ new Map()), r(this, "sawMoof", !1), this.sink = e;
|
|
1240
|
+
}
|
|
1241
|
+
get fragmented() {
|
|
1242
|
+
return this.sawMoof;
|
|
1243
|
+
}
|
|
1244
|
+
get duration() {
|
|
1245
|
+
return this.mediaDuration > 0 ? this.mediaDuration : void 0;
|
|
1246
|
+
}
|
|
1247
|
+
push(e) {
|
|
1248
|
+
let t = e;
|
|
1249
|
+
if (this.skipBytes > 0 && t.length > 0) {
|
|
1250
|
+
let e = Math.min(this.skipBytes, t.length);
|
|
1251
|
+
this.skipBytes -= e, this.bufStart += e, t = t.subarray(e);
|
|
1252
|
+
}
|
|
1253
|
+
t.length > 0 && this.buf.append(t), this.parse();
|
|
1254
|
+
}
|
|
1255
|
+
flush() {
|
|
1256
|
+
this.parse();
|
|
1257
|
+
}
|
|
1258
|
+
reset() {
|
|
1259
|
+
this.buf.clear(), this.bufStart = 0, this.skipBytes = 0, this.state = "boxes", this.pending = [], this.pendingIndex = 0, this.moovDone = !1, this.sawMdatFirst = !1, this.tracks.clear();
|
|
1260
|
+
}
|
|
1261
|
+
consume(e) {
|
|
1262
|
+
this.buf.skip(e), this.bufStart += e;
|
|
1263
|
+
}
|
|
1264
|
+
skipAhead(e) {
|
|
1265
|
+
let t = Math.min(e, this.buf.length);
|
|
1266
|
+
this.consume(t);
|
|
1267
|
+
let n = e - t;
|
|
1268
|
+
n > 0 && (this.skipBytes = n);
|
|
1269
|
+
}
|
|
1270
|
+
parse() {
|
|
1271
|
+
for (;;) {
|
|
1272
|
+
if (this.state === "mdat") {
|
|
1273
|
+
if (!this.pumpMdat()) return;
|
|
1274
|
+
continue;
|
|
1275
|
+
}
|
|
1276
|
+
if (this.buf.length < 8) return;
|
|
1277
|
+
let e = this.buf.peek(Math.min(this.buf.length, 16)), t = (e[0] << 24 | e[1] << 16 | e[2] << 8 | e[3]) >>> 0, n = v(e, 4, 8), r = 8;
|
|
1278
|
+
if (t === 1) {
|
|
1279
|
+
if (this.buf.length < 16) return;
|
|
1280
|
+
let n = new DataView(e.buffer, e.byteOffset, e.byteLength);
|
|
1281
|
+
t = Number(n.getBigUint64(8)), r = 16;
|
|
1282
|
+
} else t === 0 && (t = 2 ** 53 - 1);
|
|
1283
|
+
if (t < r) throw new i("MP4_BAD_BOX", `invalid ${n} box size ${t}`);
|
|
1284
|
+
let a = this.bufStart;
|
|
1285
|
+
if (n === "mdat") {
|
|
1286
|
+
if (this.consume(r), this.pending.length === 0) {
|
|
1287
|
+
if (this.moovDone || (this.sawMdatFirst = !0), t === 2 ** 53 - 1) this.skipBytes = 2 ** 53 - 1;
|
|
1288
|
+
else {
|
|
1289
|
+
let e = a + t - this.bufStart;
|
|
1290
|
+
e > 0 && this.skipAhead(e);
|
|
1291
|
+
}
|
|
1292
|
+
continue;
|
|
1293
|
+
}
|
|
1294
|
+
this.mdatEnd = t === 2 ** 53 - 1 ? 2 ** 53 - 1 : a + t, this.state = "mdat";
|
|
1295
|
+
continue;
|
|
1296
|
+
}
|
|
1297
|
+
if (t === 2 ** 53 - 1) {
|
|
1298
|
+
this.skipBytes = 2 ** 53 - 1;
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
if (this.buf.length < t) return;
|
|
1302
|
+
let o = this.buf.peek(t).slice();
|
|
1303
|
+
this.consume(t), this.handleBox(n, o, a, r);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
pumpMdat() {
|
|
1307
|
+
for (; this.pendingIndex < this.pending.length;) {
|
|
1308
|
+
let e = this.pending[this.pendingIndex], t = this.bufStart + this.buf.length;
|
|
1309
|
+
if (e.offset + e.size > t) break;
|
|
1310
|
+
let n = e.offset - this.bufStart;
|
|
1311
|
+
if (n > 0 && this.consume(n), e.offset < this.bufStart) {
|
|
1312
|
+
this.pendingIndex++;
|
|
1313
|
+
continue;
|
|
1314
|
+
}
|
|
1315
|
+
let r = this.buf.consume(e.size);
|
|
1316
|
+
this.bufStart += e.size, this.pendingIndex++;
|
|
1317
|
+
let i = this.tracks.get(e.trackId);
|
|
1318
|
+
if (!i) continue;
|
|
1319
|
+
let a = {
|
|
1320
|
+
trackId: e.trackId,
|
|
1321
|
+
kind: i.kind,
|
|
1322
|
+
dts: e.dts,
|
|
1323
|
+
pts: e.pts,
|
|
1324
|
+
duration: e.duration,
|
|
1325
|
+
keyframe: e.keyframe,
|
|
1326
|
+
data: r
|
|
1327
|
+
};
|
|
1328
|
+
this.sink.onSample(a);
|
|
1329
|
+
}
|
|
1330
|
+
if (this.pendingIndex < this.pending.length) return this.skipBytes === 2 ** 53 - 1 && (this.pending = [], this.pendingIndex = 0), !1;
|
|
1331
|
+
if (this.pending = [], this.pendingIndex = 0, this.state = "boxes", this.mdatEnd !== 2 ** 53 - 1) {
|
|
1332
|
+
let e = this.mdatEnd - this.bufStart;
|
|
1333
|
+
e > 0 && this.skipAhead(e);
|
|
1334
|
+
}
|
|
1335
|
+
return !0;
|
|
1336
|
+
}
|
|
1337
|
+
handleBox(e, t, n, r) {
|
|
1338
|
+
switch (e) {
|
|
1339
|
+
case "moov":
|
|
1340
|
+
this.parseMoov(t.subarray(r));
|
|
1341
|
+
break;
|
|
1342
|
+
case "moof": this.sawMoof = !0, this.parseMoof(t.subarray(r), n, r);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
parseMoov(e) {
|
|
1346
|
+
let t = R(e), n = z(t, "mvhd");
|
|
1347
|
+
if (n) try {
|
|
1348
|
+
let e = new b(n.payload), t = e.u8();
|
|
1349
|
+
e.u24(), e.skip(t === 1 ? 16 : 8);
|
|
1350
|
+
let r = e.u32(), i = t === 1 ? e.u64() : e.u32();
|
|
1351
|
+
r > 0 && i > 0 && (this.mediaDuration = i / r);
|
|
1352
|
+
} catch {}
|
|
1353
|
+
for (let e of B(t, "trak")) this.parseTrak(e.payload);
|
|
1354
|
+
this.moovDone = !0, this.tracks.size > 0 && this.sink.onTracks([...this.tracks.values()]);
|
|
1355
|
+
}
|
|
1356
|
+
parseTrak(e) {
|
|
1357
|
+
let t = R(e), n = z(t, "tkhd"), r = z(t, "mdia");
|
|
1358
|
+
if (!n || !r) return;
|
|
1359
|
+
let a = 0, o = 0, s = 0;
|
|
1360
|
+
{
|
|
1361
|
+
let e = new b(n.payload), t = e.u8();
|
|
1362
|
+
e.u24(), t === 1 ? e.skip(16) : e.skip(8), a = e.u32(), e.skip(4), t === 1 ? e.skip(8) : e.skip(4), e.skip(8), e.skip(8), e.skip(36), o = e.u32() / 65536, s = e.u32() / 65536;
|
|
1363
|
+
}
|
|
1364
|
+
let c = R(r.payload), l = z(c, "mdhd"), u = z(c, "hdlr"), d = z(c, "minf");
|
|
1365
|
+
if (!l || !u || !d) return;
|
|
1366
|
+
let f = 1e3;
|
|
1367
|
+
{
|
|
1368
|
+
let e = new b(l.payload), t = e.u8();
|
|
1369
|
+
e.u24(), e.skip(t === 1 ? 16 : 8), f = e.u32() || 1e3;
|
|
1370
|
+
}
|
|
1371
|
+
let p = "";
|
|
1372
|
+
{
|
|
1373
|
+
let e = new b(u.payload);
|
|
1374
|
+
e.skip(8), p = e.fourcc();
|
|
1375
|
+
}
|
|
1376
|
+
if (p !== "vide" && p !== "soun") return;
|
|
1377
|
+
let m = z(R(d.payload), "stbl");
|
|
1378
|
+
if (!m) return;
|
|
1379
|
+
let h = R(m.payload), g = z(h, "stsd");
|
|
1380
|
+
if (!g) return;
|
|
1381
|
+
let _ = p === "vide" ? this.parseVideoTrack(g.payload, a, f, o, s) : this.parseAudioTrack(g.payload, a, f);
|
|
1382
|
+
if (!_ || (this.tracks.set(a, _), !z(h, "stsz") || !this.hasSampleTable(h))) return;
|
|
1383
|
+
let v = this.buildSampleTable(h, _, a);
|
|
1384
|
+
if (v.length > 0) {
|
|
1385
|
+
if (this.sawMdatFirst && v[0].offset < this.bufStart) throw new i("MP4_MOOV_AT_END", "this MP4 has its index (moov) at the end of the file; re-mux it with faststart, or use native playback");
|
|
1386
|
+
this.pending = this.pending.concat(v).sort((e, t) => e.offset - t.offset);
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
hasSampleTable(e) {
|
|
1390
|
+
let t = z(e, "stsz");
|
|
1391
|
+
return t ? new DataView(t.payload.buffer, t.payload.byteOffset, t.payload.byteLength).getUint32(8) > 0 : !1;
|
|
1392
|
+
}
|
|
1393
|
+
buildSampleTable(e, t, n) {
|
|
1394
|
+
let r = z(e, "stsz"), i = z(e, "stts"), a = z(e, "ctts"), o = z(e, "stsc"), s = z(e, "stss"), c = z(e, "stco"), l = z(e, "co64"), u = new b(r.payload);
|
|
1395
|
+
u.skip(4);
|
|
1396
|
+
let d = u.u32(), f = u.u32(), p = [];
|
|
1397
|
+
if (d === 0) for (let e = 0; e < f; e++) p.push(u.u32());
|
|
1398
|
+
let m = [];
|
|
1399
|
+
if (c) {
|
|
1400
|
+
let e = new b(c.payload);
|
|
1401
|
+
e.skip(4);
|
|
1402
|
+
let t = e.u32();
|
|
1403
|
+
for (let n = 0; n < t; n++) m.push(e.u32());
|
|
1404
|
+
} else if (l) {
|
|
1405
|
+
let e = new b(l.payload);
|
|
1406
|
+
e.skip(4);
|
|
1407
|
+
let t = e.u32();
|
|
1408
|
+
for (let n = 0; n < t; n++) m.push(e.u64());
|
|
1409
|
+
}
|
|
1410
|
+
let h = [];
|
|
1411
|
+
if (o) {
|
|
1412
|
+
let e = new b(o.payload);
|
|
1413
|
+
e.skip(4);
|
|
1414
|
+
let t = e.u32();
|
|
1415
|
+
for (let n = 0; n < t; n++) h.push({
|
|
1416
|
+
firstChunk: e.u32(),
|
|
1417
|
+
samplesPerChunk: e.u32()
|
|
1418
|
+
}), e.u32();
|
|
1419
|
+
}
|
|
1420
|
+
let g = [];
|
|
1421
|
+
if (i) {
|
|
1422
|
+
let e = new b(i.payload);
|
|
1423
|
+
e.skip(4);
|
|
1424
|
+
let t = e.u32();
|
|
1425
|
+
for (let n = 0; n < t; n++) g.push({
|
|
1426
|
+
count: e.u32(),
|
|
1427
|
+
value: e.u32()
|
|
1428
|
+
});
|
|
1429
|
+
}
|
|
1430
|
+
let _ = [];
|
|
1431
|
+
if (a) {
|
|
1432
|
+
let e = new b(a.payload), t = e.u8();
|
|
1433
|
+
e.u24();
|
|
1434
|
+
let n = e.u32();
|
|
1435
|
+
for (let r = 0; r < n; r++) {
|
|
1436
|
+
let n = e.u32();
|
|
1437
|
+
_.push({
|
|
1438
|
+
count: n,
|
|
1439
|
+
value: t === 1 ? e.i32() : e.u32()
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
let v = /* @__PURE__ */ new Set();
|
|
1444
|
+
if (s) {
|
|
1445
|
+
let e = new b(s.payload);
|
|
1446
|
+
e.skip(4);
|
|
1447
|
+
let t = e.u32();
|
|
1448
|
+
for (let n = 0; n < t; n++) v.add(e.u32());
|
|
1449
|
+
}
|
|
1450
|
+
let y = 0, x = g[0]?.count ?? 0, S = 0, C = _[0]?.count ?? 0, w = 0, T = 0, E = 0, D = [];
|
|
1451
|
+
for (let e = 1; e <= m.length && T < f; e++) {
|
|
1452
|
+
for (; E + 1 < h.length && h[E + 1].firstChunk <= e;) E++;
|
|
1453
|
+
let r = h[E]?.samplesPerChunk ?? 0, i = m[e - 1];
|
|
1454
|
+
for (let e = 0; e < r && T < f; e++) {
|
|
1455
|
+
let e = d || p[T] || 0, r = g[y]?.value ?? 0, a = 0;
|
|
1456
|
+
_.length > 0 && (a = _[S]?.value ?? 0, C--, C <= 0 && (S++, C = _[S]?.count ?? 0)), D.push({
|
|
1457
|
+
offset: i,
|
|
1458
|
+
size: e,
|
|
1459
|
+
dts: w,
|
|
1460
|
+
pts: w + a,
|
|
1461
|
+
duration: r,
|
|
1462
|
+
keyframe: v.size > 0 ? v.has(T + 1) : (t.kind, !0),
|
|
1463
|
+
trackId: n
|
|
1464
|
+
}), i += e, w += r, T++, x--, x <= 0 && (y++, x = g[y]?.count ?? 0);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return D;
|
|
1468
|
+
}
|
|
1469
|
+
parseVideoTrack(e, t, n, r, i) {
|
|
1470
|
+
let a = new b(e);
|
|
1471
|
+
if (a.skip(4), a.u32(), a.remaining < 8) return null;
|
|
1472
|
+
let o = a.u32(), s = a.fourcc();
|
|
1473
|
+
if (s !== "avc1" && s !== "avc3" && s !== "hvc1" && s !== "hev1") return null;
|
|
1474
|
+
let c = a.offset;
|
|
1475
|
+
a.skip(6), a.u16(), a.skip(16);
|
|
1476
|
+
let l = a.u16(), u = a.u16();
|
|
1477
|
+
a.skip(50);
|
|
1478
|
+
let d = c + 78, f = c - 8 + o, p = R(e.subarray(d, f));
|
|
1479
|
+
if (s === "avc1" || s === "avc3") {
|
|
1480
|
+
let e = p.find((e) => e.type === "avcC");
|
|
1481
|
+
if (!e) return null;
|
|
1482
|
+
let a = e.payload.slice(), o = M(a);
|
|
1483
|
+
return {
|
|
1484
|
+
kind: "video",
|
|
1485
|
+
id: t,
|
|
1486
|
+
codec: "avc",
|
|
1487
|
+
timescale: n,
|
|
1488
|
+
width: l || o.info?.width || r || 0,
|
|
1489
|
+
height: u || o.info?.height || i || 0,
|
|
1490
|
+
fps: o.info?.fps,
|
|
1491
|
+
codecPrivate: a,
|
|
1492
|
+
profile: o.info?.profile,
|
|
1493
|
+
level: o.info?.level
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
let m = p.find((e) => e.type === "hvcC");
|
|
1497
|
+
if (!m) return null;
|
|
1498
|
+
let h = m.payload.slice(), g = N(h);
|
|
1499
|
+
return {
|
|
1500
|
+
kind: "video",
|
|
1501
|
+
id: t,
|
|
1502
|
+
codec: "hevc",
|
|
1503
|
+
timescale: n,
|
|
1504
|
+
width: l || g.info?.width || r || 0,
|
|
1505
|
+
height: u || g.info?.height || i || 0,
|
|
1506
|
+
codecPrivate: h,
|
|
1507
|
+
profile: g.info?.profileIdc,
|
|
1508
|
+
level: g.info?.levelIdc
|
|
1509
|
+
};
|
|
1510
|
+
}
|
|
1511
|
+
parseAudioTrack(e, t, n) {
|
|
1512
|
+
let r = new b(e);
|
|
1513
|
+
if (r.skip(4), r.u32(), r.remaining < 8) return null;
|
|
1514
|
+
let a = r.u32(), o = r.fourcc(), s = r.offset;
|
|
1515
|
+
r.skip(6), r.u16(), r.skip(8);
|
|
1516
|
+
let c = r.u16();
|
|
1517
|
+
r.skip(6);
|
|
1518
|
+
let l = r.u32() >>> 16;
|
|
1519
|
+
if (o === "alaw" || o === "ulaw") return {
|
|
1520
|
+
kind: "audio",
|
|
1521
|
+
id: t,
|
|
1522
|
+
codec: o === "alaw" ? "g711a" : "g711u",
|
|
1523
|
+
timescale: n,
|
|
1524
|
+
sampleRate: l || 8e3,
|
|
1525
|
+
channels: c || 1
|
|
1526
|
+
};
|
|
1527
|
+
if (o === "ec-3" || o === "ac-3") return this.sink.onError?.(new i("UNSUPPORTED_AUDIO", `Dolby ${o} audio is not supported`)), null;
|
|
1528
|
+
if (o !== "mp4a") return null;
|
|
1529
|
+
let u = s + 28, d = R(e.subarray(u, s + (a - 8))).find((e) => e.type === "esds"), f;
|
|
1530
|
+
if (d) {
|
|
1531
|
+
let e = Oe(d.payload);
|
|
1532
|
+
e && (f = e);
|
|
1533
|
+
}
|
|
1534
|
+
let p = l || 44100, m = c || 2;
|
|
1535
|
+
if (f) try {
|
|
1536
|
+
let e = F(f);
|
|
1537
|
+
p = e.sampleRate || p, m = e.channels || m;
|
|
1538
|
+
} catch {}
|
|
1539
|
+
return {
|
|
1540
|
+
kind: "audio",
|
|
1541
|
+
id: t,
|
|
1542
|
+
codec: "aac",
|
|
1543
|
+
timescale: n,
|
|
1544
|
+
sampleRate: p,
|
|
1545
|
+
channels: m,
|
|
1546
|
+
codecPrivate: f ?? P(2, p, m)
|
|
1547
|
+
};
|
|
1548
|
+
}
|
|
1549
|
+
parseMoof(e, t, n = 8) {
|
|
1550
|
+
let r = R(e);
|
|
1551
|
+
for (let i of B(r, "traf")) {
|
|
1552
|
+
let r = R(i.payload), a = z(r, "tfhd");
|
|
1553
|
+
if (!a) continue;
|
|
1554
|
+
let o = new b(a.payload);
|
|
1555
|
+
o.u8();
|
|
1556
|
+
let s = o.u24(), c = o.u32(), l = t;
|
|
1557
|
+
s & 1 && (l = o.u64()), s & 2 && o.u32();
|
|
1558
|
+
let u = s & 8 ? o.u32() : 0, d = s & 16 ? o.u32() : 0, f = s & 32 ? o.u32() : 0, p = z(r, "tfdt"), m = 0;
|
|
1559
|
+
if (p) {
|
|
1560
|
+
let e = new b(p.payload), t = e.u8();
|
|
1561
|
+
e.u24(), m = t === 1 ? e.u64() : e.u32();
|
|
1562
|
+
}
|
|
1563
|
+
let h = 0, g = !0;
|
|
1564
|
+
for (let i of B(r, "trun")) {
|
|
1565
|
+
let r = new b(i.payload), a = r.u8(), o = r.u24(), s = r.u32(), p = 0;
|
|
1566
|
+
o & 1 && (p = r.i32());
|
|
1567
|
+
let _ = null;
|
|
1568
|
+
o & 4 && (_ = r.u32()), g ? (h = o & 1 ? l + p : t + e.length + n + 8, g = !1) : o & 1 && (h = l + p);
|
|
1569
|
+
for (let e = 0; e < s; e++) {
|
|
1570
|
+
let t = o & 256 ? r.u32() : u, n = o & 512 ? r.u32() : d, i = o & 1024 ? r.u32() : f;
|
|
1571
|
+
e === 0 && _ !== null && (i = _);
|
|
1572
|
+
let s = o & 2048 ? a === 1 ? r.i32() : r.u32() : 0, l = !(i & 65536);
|
|
1573
|
+
this.pending.push({
|
|
1574
|
+
offset: h,
|
|
1575
|
+
size: n,
|
|
1576
|
+
dts: m,
|
|
1577
|
+
pts: m + s,
|
|
1578
|
+
duration: t,
|
|
1579
|
+
keyframe: l,
|
|
1580
|
+
trackId: c
|
|
1581
|
+
}), h += n, m += t;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
this.pending.sort((e, t) => e.offset - t.offset);
|
|
1586
|
+
}
|
|
1587
|
+
};
|
|
1588
|
+
function Oe(e) {
|
|
1589
|
+
let t = 4, n = () => {
|
|
1590
|
+
let n = 0;
|
|
1591
|
+
for (let r = 0; r < 4; r++) {
|
|
1592
|
+
let r = e[t++];
|
|
1593
|
+
if (n = n << 7 | r & 127, !(r & 128)) break;
|
|
1594
|
+
}
|
|
1595
|
+
return n;
|
|
1596
|
+
};
|
|
1597
|
+
if (e[t] !== 3) return null;
|
|
1598
|
+
t++;
|
|
1599
|
+
let r = n(), i = Math.min(t + r, e.length);
|
|
1600
|
+
t += 2;
|
|
1601
|
+
let a = e[t++] ?? 0;
|
|
1602
|
+
for (a & 128 && (t += 2), a & 64 && (t += 1 + (e[t] ?? 0)), a & 32 && (t += 2); t < i;) {
|
|
1603
|
+
let r = e[t++], i = n();
|
|
1604
|
+
if (r === 4) {
|
|
1605
|
+
t += 13;
|
|
1606
|
+
let r = e[t++], i = n();
|
|
1607
|
+
if (r === 5) return e.subarray(t, t + i);
|
|
1608
|
+
t += i;
|
|
1609
|
+
} else t += i;
|
|
1610
|
+
}
|
|
1611
|
+
return null;
|
|
1612
|
+
}
|
|
1613
|
+
// src/io/http.ts
|
|
1614
|
+
var ke = class {
|
|
1615
|
+
constructor() {
|
|
1616
|
+
r(this, "controller", null), r(this, "paused", !1), r(this, "resumeWaiters", []);
|
|
1617
|
+
}
|
|
1618
|
+
pause() {
|
|
1619
|
+
this.paused = !0;
|
|
1620
|
+
}
|
|
1621
|
+
resume() {
|
|
1622
|
+
this.paused = !1;
|
|
1623
|
+
let e = this.resumeWaiters;
|
|
1624
|
+
this.resumeWaiters = [];
|
|
1625
|
+
for (let t of e) t();
|
|
1626
|
+
}
|
|
1627
|
+
waitIfPaused() {
|
|
1628
|
+
if (this.paused) return new Promise((e) => this.resumeWaiters.push(e));
|
|
1629
|
+
}
|
|
1630
|
+
async start(e, t, n = {}) {
|
|
1631
|
+
let r = new AbortController();
|
|
1632
|
+
this.controller = r;
|
|
1633
|
+
let a = n.retry ?? {
|
|
1634
|
+
maxRetries: 3,
|
|
1635
|
+
delay: 500,
|
|
1636
|
+
maxDelay: 5e3
|
|
1637
|
+
}, o = { ...n.headers ?? {} }, s = 0, c = 0;
|
|
1638
|
+
for (; !r.signal.aborted;) try {
|
|
1639
|
+
let n = await fetch(e, {
|
|
1640
|
+
method: "GET",
|
|
1641
|
+
headers: o,
|
|
1642
|
+
signal: r.signal,
|
|
1643
|
+
cache: "no-store",
|
|
1644
|
+
credentials: "omit",
|
|
1645
|
+
mode: "cors"
|
|
1646
|
+
});
|
|
1647
|
+
if (t.onResponse?.({
|
|
1648
|
+
status: n.status,
|
|
1649
|
+
contentType: n.headers.get("content-type"),
|
|
1650
|
+
contentLength: n.headers.get("content-length") ? Number(n.headers.get("content-length")) : null,
|
|
1651
|
+
url: n.url || e
|
|
1652
|
+
}), !n.ok) {
|
|
1653
|
+
let e = "";
|
|
1654
|
+
try {
|
|
1655
|
+
let t = await n.text();
|
|
1656
|
+
if (t) try {
|
|
1657
|
+
let n = JSON.parse(t), r = n.message ?? n.msg ?? n.error;
|
|
1658
|
+
typeof r == "string" && (e = r);
|
|
1659
|
+
} catch {
|
|
1660
|
+
e = t.slice(0, 200).replace(/\s+/g, " ").trim();
|
|
1661
|
+
}
|
|
1662
|
+
} catch {}
|
|
1663
|
+
throw new i("HTTP_STATUS", `HTTP ${n.status}${e ? `: ${e}` : ` ${n.statusText}`}`.trim(), { status: n.status });
|
|
1664
|
+
}
|
|
1665
|
+
if (!n.body) throw Error("response has no body (streaming not supported)");
|
|
1666
|
+
let a = n.body.getReader();
|
|
1667
|
+
for (;;) {
|
|
1668
|
+
await this.waitIfPaused();
|
|
1669
|
+
let { done: e, value: n } = await a.read();
|
|
1670
|
+
if (e) break;
|
|
1671
|
+
n && n.byteLength > 0 && (c += n.byteLength, t.onData(n));
|
|
1672
|
+
}
|
|
1673
|
+
r.signal.aborted || t.onEnd();
|
|
1674
|
+
return;
|
|
1675
|
+
} catch (e) {
|
|
1676
|
+
if (r.signal.aborted) return;
|
|
1677
|
+
if (e instanceof i && e.code === "HTTP_STATUS") {
|
|
1678
|
+
let n = e.detail?.status ?? 0;
|
|
1679
|
+
if (n >= 400 && n < 500 && n !== 408 && n !== 429) {
|
|
1680
|
+
t.onError(e);
|
|
1681
|
+
return;
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
if (c > 0 || s >= a.maxRetries) {
|
|
1685
|
+
t.onError(e instanceof Error ? e : Error(String(e)));
|
|
1686
|
+
return;
|
|
1687
|
+
}
|
|
1688
|
+
s++;
|
|
1689
|
+
let n = Math.min(a.delay * 2 ** (s - 1), a.maxDelay);
|
|
1690
|
+
await new Promise((e) => setTimeout(e, n));
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
stop() {
|
|
1694
|
+
this.controller?.abort(), this.controller = null, this.resume();
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
async function H(e, t = {}) {
|
|
1698
|
+
let n = new AbortController(), r = t.timeout ? setTimeout(() => n.abort(), t.timeout) : null;
|
|
1699
|
+
try {
|
|
1700
|
+
let r = await fetch(e, {
|
|
1701
|
+
headers: t.headers,
|
|
1702
|
+
signal: n.signal,
|
|
1703
|
+
cache: "no-store",
|
|
1704
|
+
credentials: "omit",
|
|
1705
|
+
mode: "cors"
|
|
1706
|
+
});
|
|
1707
|
+
if (!r.ok) throw Error(`HTTP ${r.status} for ${e}`);
|
|
1708
|
+
return await r.text();
|
|
1709
|
+
} finally {
|
|
1710
|
+
r && clearTimeout(r);
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
async function U(e, t = {}) {
|
|
1714
|
+
let n = new AbortController(), r = t.timeout ? setTimeout(() => n.abort(), t.timeout) : null;
|
|
1715
|
+
try {
|
|
1716
|
+
let r = await fetch(e, {
|
|
1717
|
+
headers: t.headers,
|
|
1718
|
+
signal: n.signal,
|
|
1719
|
+
cache: "no-store",
|
|
1720
|
+
credentials: "omit",
|
|
1721
|
+
mode: "cors"
|
|
1722
|
+
});
|
|
1723
|
+
if (!r.ok) throw Error(`HTTP ${r.status} for ${e}`);
|
|
1724
|
+
return new Uint8Array(await r.arrayBuffer());
|
|
1725
|
+
} finally {
|
|
1726
|
+
r && clearTimeout(r);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
// src/io/websocket.ts
|
|
1730
|
+
var Ae = class {
|
|
1731
|
+
constructor() {
|
|
1732
|
+
r(this, "socket", null), r(this, "closedByUs", !1), r(this, "onData", void 0), r(this, "onText", void 0), r(this, "onClose", void 0), r(this, "onError", void 0);
|
|
1733
|
+
}
|
|
1734
|
+
get readyState() {
|
|
1735
|
+
return this.socket?.readyState ?? WebSocket.CLOSED;
|
|
1736
|
+
}
|
|
1737
|
+
connect(e, t = {}) {
|
|
1738
|
+
return new Promise((n, r) => {
|
|
1739
|
+
let i = !1, a = new WebSocket(e, t.protocols);
|
|
1740
|
+
this.socket = a, a.binaryType = "arraybuffer";
|
|
1741
|
+
let o = setTimeout(() => {
|
|
1742
|
+
i || (i = !0, this.close(), r(/* @__PURE__ */ Error(`WebSocket connect timeout: ${e}`)));
|
|
1743
|
+
}, t.timeout ?? 1e4);
|
|
1744
|
+
a.onopen = () => {
|
|
1745
|
+
clearTimeout(o), i || (i = !0, n());
|
|
1746
|
+
}, a.onmessage = (e) => {
|
|
1747
|
+
typeof e.data == "string" ? this.onText?.(e.data) : e.data instanceof ArrayBuffer ? this.onData?.(new Uint8Array(e.data)) : typeof Blob < "u" && e.data instanceof Blob && e.data.arrayBuffer().then((e) => this.onData?.(new Uint8Array(e)));
|
|
1748
|
+
}, a.onerror = () => {
|
|
1749
|
+
let t = /* @__PURE__ */ Error(`WebSocket error: ${e}`);
|
|
1750
|
+
i || (i = !0, clearTimeout(o), r(t)), this.onError?.(t);
|
|
1751
|
+
}, a.onclose = (t) => {
|
|
1752
|
+
clearTimeout(o), this.socket = null, i ? this.onClose?.(t.wasClean || this.closedByUs) : (i = !0, r(/* @__PURE__ */ Error(`WebSocket closed before open: ${e}`)));
|
|
1753
|
+
};
|
|
1754
|
+
});
|
|
1755
|
+
}
|
|
1756
|
+
send(e) {
|
|
1757
|
+
this.socket && this.socket.readyState === WebSocket.OPEN && this.socket.send(e);
|
|
1758
|
+
}
|
|
1759
|
+
close() {
|
|
1760
|
+
this.closedByUs = !0;
|
|
1761
|
+
let e = this.socket;
|
|
1762
|
+
this.socket = null;
|
|
1763
|
+
try {
|
|
1764
|
+
e?.close();
|
|
1765
|
+
} catch {}
|
|
1766
|
+
}
|
|
1767
|
+
}, je = class {
|
|
1768
|
+
constructor() {
|
|
1769
|
+
r(this, "transport", null);
|
|
1770
|
+
}
|
|
1771
|
+
async start(e, t, n = {}) {
|
|
1772
|
+
let r = new Ae();
|
|
1773
|
+
this.transport = r, r.onData = t.onData, r.onError = t.onError, r.onClose = () => t.onEnd(), await r.connect(e, n);
|
|
1774
|
+
}
|
|
1775
|
+
stop() {
|
|
1776
|
+
this.transport?.close(), this.transport = null;
|
|
1777
|
+
}
|
|
1778
|
+
}, Me = class {
|
|
1779
|
+
constructor(e, t, n, i, a) {
|
|
1780
|
+
r(this, "url", void 0), r(this, "options", void 0), r(this, "ctx", void 0), r(this, "protocol", void 0), r(this, "demuxer", void 0), r(this, "http", new ke()), r(this, "ws", null), r(this, "_live", void 0), this.url = t, this.options = n, this.ctx = i, this.protocol = e, this._live = a ?? (e === "flv" || e === "ws-flv");
|
|
1781
|
+
let o = {
|
|
1782
|
+
onTracks: (e) => i.onTracks(e),
|
|
1783
|
+
onSample: (e) => i.onSample(e),
|
|
1784
|
+
onError: (e) => i.onError(e)
|
|
1785
|
+
};
|
|
1786
|
+
this.demuxer = e === "mp4" || e === "fmp4" ? new V(o) : new De(o);
|
|
1787
|
+
}
|
|
1788
|
+
get live() {
|
|
1789
|
+
return this._live;
|
|
1790
|
+
}
|
|
1791
|
+
get duration() {
|
|
1792
|
+
return this.demuxer instanceof V ? this.demuxer.duration : void 0;
|
|
1793
|
+
}
|
|
1794
|
+
async start() {
|
|
1795
|
+
if (this.protocol === "ws-flv") {
|
|
1796
|
+
let e = new je();
|
|
1797
|
+
this.ws = e, await e.start(this.url, {
|
|
1798
|
+
onData: (e) => {
|
|
1799
|
+
this.ctx.onBytes?.(e.byteLength), this.demuxer.push(e);
|
|
1800
|
+
},
|
|
1801
|
+
onEnd: () => this.ctx.onEnd(),
|
|
1802
|
+
onError: (e) => this.ctx.onError(e)
|
|
1803
|
+
}, { timeout: this.options.timeout });
|
|
1804
|
+
} else await this.http.start(this.url, {
|
|
1805
|
+
onData: (e) => {
|
|
1806
|
+
this.ctx.onBytes?.(e.byteLength), this.demuxer.push(e);
|
|
1807
|
+
},
|
|
1808
|
+
onEnd: () => {
|
|
1809
|
+
this.demuxer.flush(), this.ctx.onEnd();
|
|
1810
|
+
},
|
|
1811
|
+
onError: (e) => this.ctx.onError(e),
|
|
1812
|
+
onResponse: (e) => this.sniff(e.contentType)
|
|
1813
|
+
}, {
|
|
1814
|
+
headers: this.options.headers,
|
|
1815
|
+
timeout: this.options.timeout,
|
|
1816
|
+
retry: this.options.retry
|
|
1817
|
+
});
|
|
1818
|
+
}
|
|
1819
|
+
sniff(e) {
|
|
1820
|
+
let t = (e ?? "").toLowerCase(), n = (e) => {
|
|
1821
|
+
this.http.stop(), this.ctx.onError(new i("SWITCH_PROTOCOL", `server returned ${t}, switching to ${e}`, { protocol: e }));
|
|
1822
|
+
};
|
|
1823
|
+
t.includes("mpegurl") || t.includes("m3u8") ? this.protocol !== "hls" && n("hls") : t.includes("x-flv") || t.includes("flv") ? this.protocol !== "flv" && this.protocol !== "ws-flv" && n("flv") : (t.includes("mp4") || t.includes("iso.segment")) && this.protocol !== "mp4" && this.protocol !== "fmp4" && n("mp4");
|
|
1824
|
+
}
|
|
1825
|
+
pause() {
|
|
1826
|
+
this.http.pause();
|
|
1827
|
+
}
|
|
1828
|
+
resume() {
|
|
1829
|
+
this.http.resume();
|
|
1830
|
+
}
|
|
1831
|
+
stop() {
|
|
1832
|
+
this.http.stop(), this.ws?.stop(), this.ws = null;
|
|
1833
|
+
}
|
|
1834
|
+
};
|
|
1835
|
+
// src/demux/hls.ts
|
|
1836
|
+
function W(e, t) {
|
|
1837
|
+
try {
|
|
1838
|
+
return new URL(e, t).href;
|
|
1839
|
+
} catch {
|
|
1840
|
+
return e;
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
function G(e) {
|
|
1844
|
+
let t = {}, n = "", r = !1, i = [];
|
|
1845
|
+
for (let t = 0; t < e.length; t++) {
|
|
1846
|
+
let a = e[t];
|
|
1847
|
+
a === "\"" && (r = !r), a === "," && !r ? (i.push(n), n = "") : n += a;
|
|
1848
|
+
}
|
|
1849
|
+
i.push(n);
|
|
1850
|
+
for (let e of i) {
|
|
1851
|
+
let n = e.indexOf("=");
|
|
1852
|
+
if (n <= 0) continue;
|
|
1853
|
+
let r = e.slice(0, n).trim(), i = e.slice(n + 1).trim();
|
|
1854
|
+
i.startsWith("\"") && i.endsWith("\"") && (i = i.slice(1, -1)), t[r] = i;
|
|
1855
|
+
}
|
|
1856
|
+
return t;
|
|
1857
|
+
}
|
|
1858
|
+
function Ne(e) {
|
|
1859
|
+
return e.split(/\r?\n/).map((e) => e.trim()).filter((e) => e.length > 0);
|
|
1860
|
+
}
|
|
1861
|
+
function Pe(e, t) {
|
|
1862
|
+
let n = [], r = Ne(e);
|
|
1863
|
+
for (let e = 0; e < r.length; e++) {
|
|
1864
|
+
let i = r[e];
|
|
1865
|
+
if (!i.startsWith("#EXT-X-STREAM-INF:")) continue;
|
|
1866
|
+
let a = G(i.slice(18)), o = "";
|
|
1867
|
+
for (let t = e + 1; t < r.length; t++) if (!r[t].startsWith("#")) {
|
|
1868
|
+
o = r[t];
|
|
1869
|
+
break;
|
|
1870
|
+
}
|
|
1871
|
+
o && n.push({
|
|
1872
|
+
uri: W(o, t),
|
|
1873
|
+
bandwidth: Number(a.BANDWIDTH ?? 0),
|
|
1874
|
+
resolution: a.RESOLUTION,
|
|
1875
|
+
codecs: a.CODECS,
|
|
1876
|
+
frameRate: a["FRAME-RATE"] ? Number(a["FRAME-RATE"]) : void 0,
|
|
1877
|
+
audioGroup: a.AUDIO
|
|
1878
|
+
});
|
|
1879
|
+
}
|
|
1880
|
+
return n;
|
|
1881
|
+
}
|
|
1882
|
+
function Fe(e, t) {
|
|
1883
|
+
let n = [];
|
|
1884
|
+
for (let r of Ne(e)) {
|
|
1885
|
+
if (!r.startsWith("#EXT-X-MEDIA:")) continue;
|
|
1886
|
+
let e = G(r.slice(13));
|
|
1887
|
+
e.TYPE && e["GROUP-ID"] && n.push({
|
|
1888
|
+
type: e.TYPE,
|
|
1889
|
+
groupId: e["GROUP-ID"],
|
|
1890
|
+
name: e.NAME ?? "",
|
|
1891
|
+
uri: e.URI ? W(e.URI, t) : void 0,
|
|
1892
|
+
default: e.DEFAULT === "YES",
|
|
1893
|
+
autoselect: e.AUTOSELECT === "YES",
|
|
1894
|
+
language: e.LANGUAGE
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1897
|
+
return n;
|
|
1898
|
+
}
|
|
1899
|
+
function Ie(e, t) {
|
|
1900
|
+
let [n, r] = e.split("@"), i = Number(n), a = r === void 0 ? t : Number(r);
|
|
1901
|
+
return {
|
|
1902
|
+
offset: Number.isFinite(a) ? a : 0,
|
|
1903
|
+
length: Number.isFinite(i) ? i : 0
|
|
1904
|
+
};
|
|
1905
|
+
}
|
|
1906
|
+
function Le(e, t) {
|
|
1907
|
+
let n = Ne(e), r = {
|
|
1908
|
+
targetDuration: 4,
|
|
1909
|
+
mediaSequence: 0,
|
|
1910
|
+
segments: [],
|
|
1911
|
+
endList: !1,
|
|
1912
|
+
encrypted: !1
|
|
1913
|
+
}, i = 0, a = !1, o, s, c, l, u = /* @__PURE__ */ new Map();
|
|
1914
|
+
for (let e = 0; e < n.length; e++) {
|
|
1915
|
+
let d = n[e];
|
|
1916
|
+
if (d.startsWith("#EXT-X-TARGETDURATION:")) {
|
|
1917
|
+
r.targetDuration = Number(d.slice(22)) || 4;
|
|
1918
|
+
continue;
|
|
1919
|
+
}
|
|
1920
|
+
if (d.startsWith("#EXT-X-MEDIA-SEQUENCE:")) {
|
|
1921
|
+
r.mediaSequence = Number(d.slice(22)) || 0;
|
|
1922
|
+
continue;
|
|
1923
|
+
}
|
|
1924
|
+
if (d === "#EXT-X-ENDLIST") {
|
|
1925
|
+
r.endList = !0;
|
|
1926
|
+
continue;
|
|
1927
|
+
}
|
|
1928
|
+
if (d === "#EXT-X-DISCONTINUITY") {
|
|
1929
|
+
a = !0;
|
|
1930
|
+
continue;
|
|
1931
|
+
}
|
|
1932
|
+
if (d.startsWith("#EXTINF:")) {
|
|
1933
|
+
i = Number(d.slice(8).split(",")[0]) || 0;
|
|
1934
|
+
continue;
|
|
1935
|
+
}
|
|
1936
|
+
if (d.startsWith("#EXT-X-BYTERANGE:")) {
|
|
1937
|
+
let r = n[e + 1], i = r && !r.startsWith("#") ? u.get(W(r, t)) ?? 0 : 0;
|
|
1938
|
+
o = Ie(d.slice(17), i);
|
|
1939
|
+
continue;
|
|
1940
|
+
}
|
|
1941
|
+
if (d.startsWith("#EXT-X-MAP:")) {
|
|
1942
|
+
let e = G(d.slice(11));
|
|
1943
|
+
e.URI && (s = W(e.URI, t), c = e.BYTERANGE ? Ie(e.BYTERANGE, 0) : void 0, r.map || (r.map = s, r.mapByterange = c));
|
|
1944
|
+
continue;
|
|
1945
|
+
}
|
|
1946
|
+
if (d.startsWith("#EXT-X-KEY:")) {
|
|
1947
|
+
let e = G(d.slice(11));
|
|
1948
|
+
e.METHOD && e.METHOD !== "NONE" ? (r.encrypted = !0, l = {
|
|
1949
|
+
method: e.METHOD,
|
|
1950
|
+
uri: e.URI ? W(e.URI, t) : void 0
|
|
1951
|
+
}) : l = void 0;
|
|
1952
|
+
continue;
|
|
1953
|
+
}
|
|
1954
|
+
if (d.startsWith("#")) continue;
|
|
1955
|
+
let f = W(d, t), p = {
|
|
1956
|
+
uri: f,
|
|
1957
|
+
duration: i,
|
|
1958
|
+
sequence: r.mediaSequence + r.segments.length,
|
|
1959
|
+
discontinuity: a,
|
|
1960
|
+
map: s ?? r.map,
|
|
1961
|
+
mapByterange: c ?? r.mapByterange,
|
|
1962
|
+
key: l
|
|
1963
|
+
};
|
|
1964
|
+
o ? (p.byterange = o, u.set(f, o.offset + o.length)) : u.delete(f), r.segments.push(p), i = 0, a = !1, o = void 0;
|
|
1965
|
+
}
|
|
1966
|
+
return r;
|
|
1967
|
+
}
|
|
1968
|
+
function Re(e, t) {
|
|
1969
|
+
if (e.length === 0) return null;
|
|
1970
|
+
let n = t ? e.filter((e) => !e.bandwidth || e.bandwidth <= t) : e;
|
|
1971
|
+
return [...n.length > 0 ? n : e].sort((e, t) => t.bandwidth - e.bandwidth)[0] ?? null;
|
|
1972
|
+
}
|
|
1973
|
+
// src/demux/ts.ts
|
|
1974
|
+
var K = 9e4, ze = 188;
|
|
1975
|
+
function Be(e, t) {
|
|
1976
|
+
let n = e[t], r = e[t + 1], i = e[t + 2], a = e[t + 3], o = e[t + 4];
|
|
1977
|
+
return (n >> 1 & 7) * 1073741824 + (r << 22) + ((i >> 1 & 127) << 15) + (a << 7) + (o >> 1 & 127);
|
|
1978
|
+
}
|
|
1979
|
+
var Ve = class {
|
|
1980
|
+
constructor() {
|
|
1981
|
+
r(this, "buf", new g(8192)), r(this, "parsed", !1), r(this, "invalid", !1), r(this, "payloadOffset", 9), r(this, "declaredLength", 0), r(this, "pts", -1), r(this, "dts", -1);
|
|
1982
|
+
}
|
|
1983
|
+
get length() {
|
|
1984
|
+
return this.buf.length;
|
|
1985
|
+
}
|
|
1986
|
+
get hasData() {
|
|
1987
|
+
return this.buf.length > 0;
|
|
1988
|
+
}
|
|
1989
|
+
reset() {
|
|
1990
|
+
this.buf.clear(), this.parsed = !1, this.invalid = !1, this.payloadOffset = 9, this.declaredLength = 0, this.pts = -1, this.dts = -1;
|
|
1991
|
+
}
|
|
1992
|
+
push(e) {
|
|
1993
|
+
this.buf.append(e), this.parsed || this.parseHeader();
|
|
1994
|
+
}
|
|
1995
|
+
get complete() {
|
|
1996
|
+
return this.parsed && this.declaredLength > 0 && this.buf.length >= 6 + this.declaredLength;
|
|
1997
|
+
}
|
|
1998
|
+
parseHeader() {
|
|
1999
|
+
if (this.parsed || this.invalid || this.buf.length < 9) return;
|
|
2000
|
+
let e = this.buf.peek(9);
|
|
2001
|
+
if (e[0] !== 0 || e[1] !== 0 || e[2] !== 1) {
|
|
2002
|
+
this.invalid = !0;
|
|
2003
|
+
return;
|
|
2004
|
+
}
|
|
2005
|
+
this.declaredLength = e[4] << 8 | e[5];
|
|
2006
|
+
let t = e[7], n = e[8];
|
|
2007
|
+
if (this.payloadOffset = 9 + n, t & 128) {
|
|
2008
|
+
if (this.buf.length < 14) return;
|
|
2009
|
+
let e = this.buf.peek(14);
|
|
2010
|
+
if (this.pts = Be(e, 9), t & 64) {
|
|
2011
|
+
if (this.buf.length < 19) return;
|
|
2012
|
+
let e = this.buf.peek(19);
|
|
2013
|
+
this.dts = Be(e, 14);
|
|
2014
|
+
} else this.dts = this.pts;
|
|
2015
|
+
} else this.dts = -1;
|
|
2016
|
+
this.parsed = !0;
|
|
2017
|
+
}
|
|
2018
|
+
takePayload() {
|
|
2019
|
+
let e = this.buf.peek(this.buf.length), t = this.declaredLength > 0 ? Math.min(e.length, 6 + this.declaredLength) : e.length, n = e.slice(Math.min(this.payloadOffset, t), t);
|
|
2020
|
+
return this.reset(), n;
|
|
2021
|
+
}
|
|
2022
|
+
}, He = class {
|
|
2023
|
+
constructor(e) {
|
|
2024
|
+
r(this, "sink", void 0), r(this, "format", "ts"), r(this, "buf", new g(65536)), r(this, "packetSize", ze), r(this, "syncOffset", 0), r(this, "synced", !1), r(this, "pmtPid", -1), r(this, "streams", /* @__PURE__ */ new Map()), r(this, "tracks", /* @__PURE__ */ new Map()), r(this, "videoPid", -1), r(this, "audioPid", -1), r(this, "warned", /* @__PURE__ */ new Set()), this.sink = e;
|
|
2025
|
+
}
|
|
2026
|
+
push(e) {
|
|
2027
|
+
this.buf.append(e), this.parse();
|
|
2028
|
+
}
|
|
2029
|
+
flush() {
|
|
2030
|
+
for (let e of this.streams.values()) e.acc.hasData && this.finishPes(e), e.kind === "video" && this.flushAu(e);
|
|
2031
|
+
}
|
|
2032
|
+
reset() {
|
|
2033
|
+
this.buf.clear(), this.streams.clear(), this.tracks.clear(), this.pmtPid = -1, this.videoPid = -1, this.audioPid = -1, this.synced = !1, this.warned.clear();
|
|
2034
|
+
}
|
|
2035
|
+
parse() {
|
|
2036
|
+
if (!this.synced) {
|
|
2037
|
+
if (!this.detectPacketSize()) return;
|
|
2038
|
+
this.synced = !0;
|
|
2039
|
+
}
|
|
2040
|
+
for (; this.buf.length >= this.packetSize;) {
|
|
2041
|
+
let e = this.buf.peek(this.packetSize);
|
|
2042
|
+
if (e[this.syncOffset] !== 71) {
|
|
2043
|
+
if (!this.resync()) return;
|
|
2044
|
+
} else this.consumePacket(e), this.buf.skip(this.packetSize);
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
detectPacketSize() {
|
|
2048
|
+
let e = this.buf.peek(Math.min(this.buf.length, 752));
|
|
2049
|
+
for (let t of [{
|
|
2050
|
+
size: 188,
|
|
2051
|
+
offset: 0
|
|
2052
|
+
}, {
|
|
2053
|
+
size: 192,
|
|
2054
|
+
offset: 4
|
|
2055
|
+
}]) if (!(e.length <= t.offset) && e[t.offset] === 71 && !(e.length >= t.size + t.offset && e[t.size + t.offset] !== 71)) return this.packetSize = t.size, this.syncOffset = t.offset, !0;
|
|
2056
|
+
for (let t = 0; t < e.length; t++) if (e[t] === 71) return this.buf.skip(t), this.packetSize = 188, this.syncOffset = 0, !0;
|
|
2057
|
+
return this.buf.skip(this.buf.length), !1;
|
|
2058
|
+
}
|
|
2059
|
+
resync() {
|
|
2060
|
+
let e = this.buf.peek(this.buf.length);
|
|
2061
|
+
for (let t = 1; t < e.length; t++) if (e[t] === 71) return this.buf.skip(t), !0;
|
|
2062
|
+
return this.buf.skip(e.length), !1;
|
|
2063
|
+
}
|
|
2064
|
+
consumePacket(e) {
|
|
2065
|
+
let t = this.syncOffset;
|
|
2066
|
+
if (e[t + 1] & 128) return;
|
|
2067
|
+
let n = !!(e[t + 1] & 64), r = (e[t + 1] & 31) << 8 | e[t + 2];
|
|
2068
|
+
if (e[t + 3] >> 6 & 3) {
|
|
2069
|
+
this.warnOnce("scrambled", "scrambled MPEG-TS packets are not supported");
|
|
2070
|
+
return;
|
|
2071
|
+
}
|
|
2072
|
+
let i = e[t + 3] >> 4 & 3;
|
|
2073
|
+
if (i === 0 || !(e[t + 3] & 16)) return;
|
|
2074
|
+
let a = t + 4;
|
|
2075
|
+
if (i === 3) {
|
|
2076
|
+
let t = e[a];
|
|
2077
|
+
a += 1 + t;
|
|
2078
|
+
}
|
|
2079
|
+
let o = t + 188;
|
|
2080
|
+
if (a >= o) return;
|
|
2081
|
+
let s = e.subarray(a, o);
|
|
2082
|
+
if (r === 0 || r === this.pmtPid) {
|
|
2083
|
+
n && this.handleSection(r, s);
|
|
2084
|
+
return;
|
|
2085
|
+
}
|
|
2086
|
+
let c = this.streams.get(r);
|
|
2087
|
+
c && (n ? (c.acc.hasData && this.finishPes(c), c.acc.reset(), c.acc.push(s)) : c.acc.hasData && c.acc.push(s), c.acc.complete && this.finishPes(c));
|
|
2088
|
+
}
|
|
2089
|
+
handleSection(e, t) {
|
|
2090
|
+
if (t.length < 4) return;
|
|
2091
|
+
let n = t[0], r = t.subarray(1 + n);
|
|
2092
|
+
if (r.length < 8) return;
|
|
2093
|
+
let i = r[0];
|
|
2094
|
+
if (e === 0 && i === 0) {
|
|
2095
|
+
let e = (r[1] & 15) << 8 | r[2], t = Math.min(3 + e - 4, r.length);
|
|
2096
|
+
for (let e = 8; e + 4 <= t; e += 4) {
|
|
2097
|
+
let t = r[e] << 8 | r[e + 1], n = (r[e + 2] & 31) << 8 | r[e + 3];
|
|
2098
|
+
if (t !== 0) {
|
|
2099
|
+
this.pmtPid = n;
|
|
2100
|
+
return;
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
} else e === this.pmtPid && i === 2 && this.parsePmt(r);
|
|
2104
|
+
}
|
|
2105
|
+
parsePmt(e) {
|
|
2106
|
+
if (e.length < 12) return;
|
|
2107
|
+
let t = (e[1] & 15) << 8 | e[2], n = Math.min(3 + t - 4, e.length), r = 12 + ((e[10] & 15) << 8 | e[11]);
|
|
2108
|
+
for (; r + 5 <= n;) {
|
|
2109
|
+
let t = e[r], n = (e[r + 1] & 31) << 8 | e[r + 2], i = (e[r + 3] & 15) << 8 | e[r + 4];
|
|
2110
|
+
r += 5 + i, t === 27 ? this.addStream(n, "video", "avc") : t === 36 ? this.addStream(n, "video", "hevc") : t === 15 ? this.addStream(n, "audio", "aac") : (t === 3 || t === 4) && this.warnOnce("mp3", "MP3 audio in MPEG-TS is not supported");
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
addStream(e, t, n) {
|
|
2114
|
+
this.streams.has(e) || t === "video" && this.videoPid >= 0 || t === "audio" && this.audioPid >= 0 || (t === "video" ? this.videoPid = e : this.audioPid = e, this.streams.set(e, {
|
|
2115
|
+
pid: e,
|
|
2116
|
+
kind: t,
|
|
2117
|
+
codec: n,
|
|
2118
|
+
acc: new Ve(),
|
|
2119
|
+
lastRawDts: -1,
|
|
2120
|
+
wrapOffset: 0,
|
|
2121
|
+
auNals: [],
|
|
2122
|
+
auHasVcl: !1,
|
|
2123
|
+
auPts: 0,
|
|
2124
|
+
auDts: 0,
|
|
2125
|
+
auIndex: 0,
|
|
2126
|
+
curPesPts: 0,
|
|
2127
|
+
curPesDts: 0,
|
|
2128
|
+
frameDuration: 3600
|
|
2129
|
+
}));
|
|
2130
|
+
}
|
|
2131
|
+
finishPes(e) {
|
|
2132
|
+
let t = e.acc.dts, n = e.acc.pts, r = e.acc.takePayload();
|
|
2133
|
+
if (r.length === 0) return;
|
|
2134
|
+
let i, a;
|
|
2135
|
+
t >= 0 ? (i = this.unwrap(e, t), a = n >= 0 ? n + e.wrapOffset : i, a < i && (a = i)) : (i = e.auDts || 0, a = i), e.kind === "video" ? this.handleVideoPes(e, r, a, i) : this.handleAudioPes(e, r, a);
|
|
2136
|
+
}
|
|
2137
|
+
unwrap(e, t) {
|
|
2138
|
+
if (e.lastRawDts < 0) return e.lastRawDts = t, t;
|
|
2139
|
+
let n = t - e.lastRawDts;
|
|
2140
|
+
return n < -1 ? e.wrapOffset += 2 : n > 1 && (e.wrapOffset -= 2), e.lastRawDts = t, t + e.wrapOffset;
|
|
2141
|
+
}
|
|
2142
|
+
handleVideoPes(e, t, n, r) {
|
|
2143
|
+
e.curPesPts = n, e.curPesDts = r, e.auIndex = 0;
|
|
2144
|
+
let i = A(t);
|
|
2145
|
+
for (let t of i) {
|
|
2146
|
+
if (t.length === 0) continue;
|
|
2147
|
+
let n = e.codec === "avc" ? t[0] & 31 : t[0] >> 1 & 63;
|
|
2148
|
+
if (e.codec === "avc") {
|
|
2149
|
+
if (n === 7) {
|
|
2150
|
+
e.sps = t, this.maybeBuildVideoTrack(e);
|
|
2151
|
+
continue;
|
|
2152
|
+
}
|
|
2153
|
+
if (n === 8) {
|
|
2154
|
+
e.pps = t, this.maybeBuildVideoTrack(e);
|
|
2155
|
+
continue;
|
|
2156
|
+
}
|
|
2157
|
+
} else {
|
|
2158
|
+
if (n === 32) {
|
|
2159
|
+
e.vps = t, this.maybeBuildVideoTrack(e);
|
|
2160
|
+
continue;
|
|
2161
|
+
}
|
|
2162
|
+
if (n === 33) {
|
|
2163
|
+
e.sps = t, this.maybeBuildVideoTrack(e);
|
|
2164
|
+
continue;
|
|
2165
|
+
}
|
|
2166
|
+
if (n === 34) {
|
|
2167
|
+
e.pps = t, this.maybeBuildVideoTrack(e);
|
|
2168
|
+
continue;
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
(e.codec === "avc" ? n >= 1 && n <= 5 : n <= 31) ? (e.auHasVcl && this.flushAu(e), e.auNals.push(t), e.auHasVcl = !0) : (e.auHasVcl && this.flushAu(e), e.auNals.push(t));
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
maybeBuildVideoTrack(e) {
|
|
2175
|
+
if (!e.sps || !e.pps || this.tracks.has(1)) return;
|
|
2176
|
+
let t = null;
|
|
2177
|
+
if (e.codec === "avc") {
|
|
2178
|
+
let n = ce(e.sps), r = le([e.sps], [e.pps], n);
|
|
2179
|
+
t = {
|
|
2180
|
+
kind: "video",
|
|
2181
|
+
id: 1,
|
|
2182
|
+
codec: "avc",
|
|
2183
|
+
timescale: K,
|
|
2184
|
+
width: n.width,
|
|
2185
|
+
height: n.height,
|
|
2186
|
+
fps: n.fps,
|
|
2187
|
+
codecPrivate: r,
|
|
2188
|
+
profile: n.profile,
|
|
2189
|
+
level: n.level
|
|
2190
|
+
}, e.frameDuration = Math.round(K / (n.fps && n.fps > 1 ? n.fps : 25));
|
|
2191
|
+
} else {
|
|
2192
|
+
if (!e.vps) return;
|
|
2193
|
+
let n = _e(e.sps), r = ve(n, e.vps, e.sps, e.pps);
|
|
2194
|
+
t = {
|
|
2195
|
+
kind: "video",
|
|
2196
|
+
id: 1,
|
|
2197
|
+
codec: "hevc",
|
|
2198
|
+
timescale: K,
|
|
2199
|
+
width: n.width,
|
|
2200
|
+
height: n.height,
|
|
2201
|
+
codecPrivate: r,
|
|
2202
|
+
profile: n.profileIdc,
|
|
2203
|
+
level: n.levelIdc
|
|
2204
|
+
}, e.frameDuration = Math.round(K / 25);
|
|
2205
|
+
}
|
|
2206
|
+
this.tracks.set(1, t), this.sink.onTracks([...this.tracks.values()]);
|
|
2207
|
+
}
|
|
2208
|
+
flushAu(e) {
|
|
2209
|
+
if (!e.auHasVcl || e.auNals.length === 0) {
|
|
2210
|
+
e.auNals = [], e.auHasVcl = !1;
|
|
2211
|
+
return;
|
|
2212
|
+
}
|
|
2213
|
+
let t = oe(e.auNals), n = e.auIndex === 0 ? e.curPesPts : e.curPesPts + e.auIndex * e.frameDuration, r = e.auIndex === 0 ? e.curPesDts : e.curPesDts + e.auIndex * e.frameDuration, i = e.auNals.find((t) => {
|
|
2214
|
+
let n = e.codec === "avc" ? t[0] & 31 : t[0] >> 1 & 63;
|
|
2215
|
+
return e.codec === "avc" ? n >= 1 && n <= 5 : n <= 31;
|
|
2216
|
+
}), a = !1;
|
|
2217
|
+
if (i) {
|
|
2218
|
+
let t = e.codec === "avc" ? i[0] & 31 : i[0] >> 1 & 63;
|
|
2219
|
+
a = e.codec === "avc" ? t === 5 : t >= 16 && t <= 23;
|
|
2220
|
+
}
|
|
2221
|
+
this.sink.onSample({
|
|
2222
|
+
trackId: 1,
|
|
2223
|
+
kind: "video",
|
|
2224
|
+
dts: r,
|
|
2225
|
+
pts: n,
|
|
2226
|
+
duration: e.frameDuration,
|
|
2227
|
+
keyframe: a,
|
|
2228
|
+
data: t
|
|
2229
|
+
}), e.auNals = [], e.auHasVcl = !1, e.auIndex++;
|
|
2230
|
+
}
|
|
2231
|
+
handleAudioPes(e, t, n) {
|
|
2232
|
+
let r = 0, i = 0;
|
|
2233
|
+
for (; r + 7 <= t.length;) {
|
|
2234
|
+
let e = Se(t, r);
|
|
2235
|
+
if (!e || r + e.frameLength > t.length) break;
|
|
2236
|
+
this.tracks.has(2) || this.buildAudioTrack(e.sampleRate, e.channels, e.objectType);
|
|
2237
|
+
let a = Math.max(1, Math.round(1024 * K / e.sampleRate));
|
|
2238
|
+
this.sink.onSample({
|
|
2239
|
+
trackId: 2,
|
|
2240
|
+
kind: "audio",
|
|
2241
|
+
dts: n + i * a,
|
|
2242
|
+
pts: n + i * a,
|
|
2243
|
+
duration: a,
|
|
2244
|
+
keyframe: !0,
|
|
2245
|
+
data: t.slice(r + e.headerLength, r + e.frameLength)
|
|
2246
|
+
}), r += e.frameLength, i++;
|
|
2247
|
+
}
|
|
2248
|
+
r === 0 && t.length > 0 && this.warnOnce("adts", "could not find ADTS sync in audio PES");
|
|
2249
|
+
}
|
|
2250
|
+
buildAudioTrack(e, t, n) {
|
|
2251
|
+
let r = {
|
|
2252
|
+
kind: "audio",
|
|
2253
|
+
id: 2,
|
|
2254
|
+
codec: "aac",
|
|
2255
|
+
timescale: K,
|
|
2256
|
+
sampleRate: e,
|
|
2257
|
+
channels: t,
|
|
2258
|
+
codecPrivate: P(n, e, t)
|
|
2259
|
+
};
|
|
2260
|
+
this.tracks.set(2, r), this.sink.onTracks([...this.tracks.values()]);
|
|
2261
|
+
}
|
|
2262
|
+
warnOnce(e, t) {
|
|
2263
|
+
this.warned.has(e) || (this.warned.add(e), this.sink.onError?.(Error(t)));
|
|
2264
|
+
}
|
|
2265
|
+
}, Ue = class {
|
|
2266
|
+
constructor(e, t, n, i = 0) {
|
|
2267
|
+
r(this, "url", void 0), r(this, "options", void 0), r(this, "ctx", void 0), r(this, "startMs", void 0), r(this, "protocol", "hls"), r(this, "tracks", []), r(this, "stopped", !1), r(this, "_live", !1), r(this, "mediaDuration", 0), r(this, "audioOnly", !1), this.url = e, this.options = t, this.ctx = n, this.startMs = i;
|
|
2268
|
+
}
|
|
2269
|
+
get duration() {
|
|
2270
|
+
return this.mediaDuration;
|
|
2271
|
+
}
|
|
2272
|
+
get live() {
|
|
2273
|
+
return this._live;
|
|
2274
|
+
}
|
|
2275
|
+
async start() {
|
|
2276
|
+
let e = await H(this.url, {
|
|
2277
|
+
headers: this.options.headers,
|
|
2278
|
+
timeout: this.options.timeout
|
|
2279
|
+
}), t = this.url, n = null, r = [];
|
|
2280
|
+
if (e.includes("#EXT-X-STREAM-INF")) {
|
|
2281
|
+
if (n = Re(Pe(e, this.url), this.options.hls.maxBandwidth), !n) throw new i("HLS_NO_VARIANTS", "master playlist has no playable variant");
|
|
2282
|
+
t = n.uri;
|
|
2283
|
+
let a = Fe(e, this.url).filter((e) => e.type === "AUDIO" && e.uri);
|
|
2284
|
+
if (n.audioGroup) {
|
|
2285
|
+
let e = a.filter((e) => e.groupId === n.audioGroup), t = a.filter((e) => e.groupId !== n.audioGroup);
|
|
2286
|
+
r = [...e, ...t].map((e) => e.uri);
|
|
2287
|
+
}
|
|
2288
|
+
} else e.includes("#EXT-X-MEDIA:") && (r = Fe(e, this.url).filter((e) => e.type === "AUDIO" && e.uri).map((e) => e.uri));
|
|
2289
|
+
r = [...new Set(r)];
|
|
2290
|
+
let a = !!n && !n.resolution && /mp4a/i.test(n.codecs ?? "") && !/avc1|hvc1|hev1/i.test(n.codecs ?? "");
|
|
2291
|
+
this.audioOnly = a;
|
|
2292
|
+
let o = await this.createTrack(t, "main", !1);
|
|
2293
|
+
if (this.tracks.push(o), r.length > 0 && !a) {
|
|
2294
|
+
let e = null;
|
|
2295
|
+
for (let t of r) try {
|
|
2296
|
+
let n = await this.createTrack(t, "audio", !0);
|
|
2297
|
+
this.tracks.push(n), e = null;
|
|
2298
|
+
break;
|
|
2299
|
+
} catch (t) {
|
|
2300
|
+
e = t;
|
|
2301
|
+
}
|
|
2302
|
+
e && this.ctx.onError(new i("HLS_AUDIO_FAILED", "no playable alternate audio rendition", e));
|
|
2303
|
+
}
|
|
2304
|
+
this._live = this.tracks.some((e) => !e.playlist.endList), this.mediaDuration = Math.max(0, ...this.tracks.map((e) => e.playlist.segments.reduce((e, t) => e + t.duration, 0)));
|
|
2305
|
+
for (let e of this.tracks) this.runLoop(e);
|
|
2306
|
+
}
|
|
2307
|
+
async createTrack(e, t, n) {
|
|
2308
|
+
let r = Le(await H(e, {
|
|
2309
|
+
headers: this.options.headers,
|
|
2310
|
+
timeout: this.options.timeout
|
|
2311
|
+
}), e);
|
|
2312
|
+
if (r.segments.length === 0) throw new i("HLS_EMPTY", `HLS playlist ${e} has no segments`);
|
|
2313
|
+
let a = !!r.map || r.segments.some((e) => /\.(m4s|mp4|cmfv|cmfa)$/i.test(Ge(e.uri))), o = !1, s = {
|
|
2314
|
+
onTracks: (e) => {
|
|
2315
|
+
e.length > 0 && (o = !0), this.ctx.onTracks(n ? e.map(We) : e);
|
|
2316
|
+
},
|
|
2317
|
+
onSample: (e) => this.ctx.onSample(n ? {
|
|
2318
|
+
...e,
|
|
2319
|
+
kind: "audio",
|
|
2320
|
+
trackId: 2
|
|
2321
|
+
} : e),
|
|
2322
|
+
onError: (e) => {
|
|
2323
|
+
n && e instanceof i && e.code === "UNSUPPORTED_AUDIO" || this.ctx.onError(e);
|
|
2324
|
+
}
|
|
2325
|
+
}, c = a ? new V(s) : new He(s);
|
|
2326
|
+
if (a && r.map) {
|
|
2327
|
+
let t = { ...this.options.headers };
|
|
2328
|
+
if (r.mapByterange) {
|
|
2329
|
+
let { offset: e, length: n } = r.mapByterange;
|
|
2330
|
+
t.Range = `bytes=${e}-${e + n - 1}`;
|
|
2331
|
+
}
|
|
2332
|
+
let n = await U(r.map, {
|
|
2333
|
+
headers: t,
|
|
2334
|
+
timeout: this.options.timeout
|
|
2335
|
+
});
|
|
2336
|
+
if (c.push(n), !o) throw new i("HLS_UNSUPPORTED_TRACK", `the init segment of ${e} contains no supported track`);
|
|
2337
|
+
}
|
|
2338
|
+
let l = 0;
|
|
2339
|
+
if (this.startMs > 0) {
|
|
2340
|
+
let e = 0;
|
|
2341
|
+
for (; l < r.segments.length - 1 && e * 1e3 < this.startMs;) e += r.segments[l].duration, l++;
|
|
2342
|
+
}
|
|
2343
|
+
return {
|
|
2344
|
+
label: t,
|
|
2345
|
+
url: e,
|
|
2346
|
+
playlist: r,
|
|
2347
|
+
demuxer: c,
|
|
2348
|
+
index: l,
|
|
2349
|
+
ended: !1,
|
|
2350
|
+
isAudio: n
|
|
2351
|
+
};
|
|
2352
|
+
}
|
|
2353
|
+
async runLoop(e) {
|
|
2354
|
+
try {
|
|
2355
|
+
for (; !this.stopped;) {
|
|
2356
|
+
if (e.index >= e.playlist.segments.length) {
|
|
2357
|
+
if (e.playlist.endList) break;
|
|
2358
|
+
await f(Math.max(300, e.playlist.targetDuration * 500)), await this.reload(e);
|
|
2359
|
+
continue;
|
|
2360
|
+
}
|
|
2361
|
+
let t = e.playlist.segments[e.index++];
|
|
2362
|
+
if (t.key && t.key.method !== "NONE") throw new i("HLS_ENCRYPTED", "AES-128 encrypted HLS is not supported yet");
|
|
2363
|
+
t.discontinuity && e.demuxer.flush();
|
|
2364
|
+
let n = await this.fetchSegment(t);
|
|
2365
|
+
if (this.stopped) return;
|
|
2366
|
+
this.ctx.onBytes?.(n.byteLength), e.demuxer.push(n), e.playlist.endList ? await this.waitForDemand() : await this.paceLive();
|
|
2367
|
+
}
|
|
2368
|
+
if (this.stopped) return;
|
|
2369
|
+
e.ended = !0, e.demuxer.flush(), this.tracks.every((e) => e.ended) && this.ctx.onEnd();
|
|
2370
|
+
} catch (e) {
|
|
2371
|
+
this.stopped || this.ctx.onError(e instanceof Error ? e : Error(String(e)));
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
async fetchSegment(e) {
|
|
2375
|
+
let t = { ...this.options.headers };
|
|
2376
|
+
return e.byterange && (t.Range = `bytes=${e.byterange.offset}-${e.byterange.offset + e.byterange.length - 1}`), U(e.uri, {
|
|
2377
|
+
headers: t,
|
|
2378
|
+
timeout: this.options.timeout
|
|
2379
|
+
});
|
|
2380
|
+
}
|
|
2381
|
+
async reload(e) {
|
|
2382
|
+
let t = Le(await H(e.url, {
|
|
2383
|
+
headers: this.options.headers,
|
|
2384
|
+
timeout: this.options.timeout
|
|
2385
|
+
}), e.url), n = new Set(e.playlist.segments.map((e) => e.sequence));
|
|
2386
|
+
e.playlist.endList = t.endList, e.playlist.targetDuration = t.targetDuration || e.playlist.targetDuration;
|
|
2387
|
+
for (let r of t.segments) n.has(r.sequence) || e.playlist.segments.push(r);
|
|
2388
|
+
this._live = this.tracks.some((e) => !e.playlist.endList);
|
|
2389
|
+
}
|
|
2390
|
+
async waitForDemand() {
|
|
2391
|
+
for (;;) {
|
|
2392
|
+
if (this.stopped || (this.ctx.getBufferAhead?.() ?? 0) < 30) return;
|
|
2393
|
+
await f(250);
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
async paceLive() {
|
|
2397
|
+
(this.ctx.getBufferAhead?.() ?? 0) > Math.max(2, this.options.liveBufferTarget * 3) && await f(250);
|
|
2398
|
+
}
|
|
2399
|
+
get isAudioOnly() {
|
|
2400
|
+
return this.audioOnly;
|
|
2401
|
+
}
|
|
2402
|
+
stop() {
|
|
2403
|
+
this.stopped = !0;
|
|
2404
|
+
}
|
|
2405
|
+
};
|
|
2406
|
+
function We(e) {
|
|
2407
|
+
return e.kind === "audio" ? {
|
|
2408
|
+
...e,
|
|
2409
|
+
id: 2
|
|
2410
|
+
} : e;
|
|
2411
|
+
}
|
|
2412
|
+
function Ge(e) {
|
|
2413
|
+
try {
|
|
2414
|
+
return new URL(e).pathname;
|
|
2415
|
+
} catch {
|
|
2416
|
+
return e;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
// src/remux/fmp4.ts
|
|
2420
|
+
var Ke = [
|
|
2421
|
+
65536,
|
|
2422
|
+
0,
|
|
2423
|
+
0,
|
|
2424
|
+
0,
|
|
2425
|
+
65536,
|
|
2426
|
+
0,
|
|
2427
|
+
0,
|
|
2428
|
+
0,
|
|
2429
|
+
1073741824
|
|
2430
|
+
], qe = 1e3;
|
|
2431
|
+
function Je(e) {
|
|
2432
|
+
for (let t of Ke) e.u32(t);
|
|
2433
|
+
}
|
|
2434
|
+
function Ye() {
|
|
2435
|
+
let e = new x();
|
|
2436
|
+
e.fourcc("iso5"), e.u32(512);
|
|
2437
|
+
for (let t of [
|
|
2438
|
+
"iso5",
|
|
2439
|
+
"iso6",
|
|
2440
|
+
"mp41",
|
|
2441
|
+
"avc1",
|
|
2442
|
+
"hvc1"
|
|
2443
|
+
]) e.fourcc(t);
|
|
2444
|
+
return S("ftyp", e.toUint8Array());
|
|
2445
|
+
}
|
|
2446
|
+
function Xe() {
|
|
2447
|
+
let e = new x();
|
|
2448
|
+
e.u32(0).u32(0), e.u32(qe).u32(0), e.u32(65536), e.u16(256), e.u16(0), e.u32(0).u32(0), Je(e);
|
|
2449
|
+
for (let t = 0; t < 6; t++) e.u32(0);
|
|
2450
|
+
return e.u32(3), C("mvhd", 0, 0, e.toUint8Array());
|
|
2451
|
+
}
|
|
2452
|
+
function Ze(e, t) {
|
|
2453
|
+
let n = new x();
|
|
2454
|
+
return n.u32(0).u32(0), n.u32(t), n.u32(0), n.u32(0), n.u32(0).u32(0), n.u16(0), n.u16(0), n.u16(e.kind === "audio" ? 256 : 0), n.u16(0), Je(n), n.u32(e.kind === "video" ? w(e.width) : 0), n.u32(e.kind === "video" ? w(e.height) : 0), C("tkhd", 0, 3, n.toUint8Array());
|
|
2455
|
+
}
|
|
2456
|
+
function Qe(e) {
|
|
2457
|
+
let t = new x();
|
|
2458
|
+
return t.u32(0).u32(0), t.u32(e.timescale), t.u32(0), t.u16(21956), t.u16(0), C("mdhd", 0, 0, t.toUint8Array());
|
|
2459
|
+
}
|
|
2460
|
+
function $e(e) {
|
|
2461
|
+
let t = new x();
|
|
2462
|
+
return t.u32(0), t.fourcc(e.kind === "video" ? "vide" : "soun"), t.u32(0).u32(0).u32(0), t.cstring(e.kind === "video" ? "VideoHandler" : "SoundHandler"), C("hdlr", 0, 0, t.toUint8Array());
|
|
2463
|
+
}
|
|
2464
|
+
function et() {
|
|
2465
|
+
let e = new x();
|
|
2466
|
+
return e.u16(0), e.u16(0).u16(0).u16(0), C("vmhd", 0, 1, e.toUint8Array());
|
|
2467
|
+
}
|
|
2468
|
+
function tt() {
|
|
2469
|
+
let e = new x();
|
|
2470
|
+
return e.u16(0), e.u16(0), C("smhd", 0, 0, e.toUint8Array());
|
|
2471
|
+
}
|
|
2472
|
+
function nt() {
|
|
2473
|
+
return S("dinf", C("dref", 0, 0, rt(1), C("url ", 0, 1)));
|
|
2474
|
+
}
|
|
2475
|
+
function rt(e) {
|
|
2476
|
+
let t = new x(4);
|
|
2477
|
+
return t.u32(e), t.toUint8Array();
|
|
2478
|
+
}
|
|
2479
|
+
function it(e, t, n) {
|
|
2480
|
+
let r = new x();
|
|
2481
|
+
return r.raw(/* @__PURE__ */ new Uint8Array(6)), r.u16(1), r.u16(0), r.u16(0), r.u32(0).u32(0).u32(0), r.u16(t.width || 1920), r.u16(t.height || 1080), r.u32(4718592), r.u32(4718592), r.u32(0), r.u16(1), r.raw(/* @__PURE__ */ new Uint8Array(32)), r.u16(24), r.i16(-1), S(e, r.toUint8Array(), n);
|
|
2482
|
+
}
|
|
2483
|
+
function at(e, t) {
|
|
2484
|
+
let n = new x();
|
|
2485
|
+
return n.raw(/* @__PURE__ */ new Uint8Array(6)), n.u16(1), n.u32(0).u32(0), n.u16(e.channels || 2), n.u16(16), n.u16(0), n.u16(0), n.u32((e.sampleRate || 44100) << 16), S("mp4a", n.toUint8Array(), t);
|
|
2486
|
+
}
|
|
2487
|
+
function q(e, t) {
|
|
2488
|
+
let n = [], r = t.length;
|
|
2489
|
+
do
|
|
2490
|
+
n.unshift(r & 127), r >>= 7;
|
|
2491
|
+
while (r > 0);
|
|
2492
|
+
for (let e = 0; e < n.length - 1; e++) n[e] |= 128;
|
|
2493
|
+
return _(Uint8Array.of(e, ...n), t);
|
|
2494
|
+
}
|
|
2495
|
+
function ot(e) {
|
|
2496
|
+
let t = new x();
|
|
2497
|
+
t.u8(64), t.u8(21), t.u24(0), t.u32(e.sampleRate * e.channels * 2), t.u32(e.sampleRate * e.channels * 2);
|
|
2498
|
+
let n = q(5, e.codecPrivate ?? Uint8Array.of(17, 144)), r = q(4, _(t.toUint8Array(), n)), i = new x();
|
|
2499
|
+
return i.u16(1), i.u8(0), C("esds", 0, 0, q(3, _(i.toUint8Array(), r, q(6, Uint8Array.of(2)))));
|
|
2500
|
+
}
|
|
2501
|
+
function st(e) {
|
|
2502
|
+
let t;
|
|
2503
|
+
return t = e.kind === "video" ? it(e.codec === "hevc" ? "hvc1" : "avc1", e, S(e.codec === "hevc" ? "hvcC" : "avcC", e.codecPrivate)) : at(e, ot(e)), C("stsd", 0, 0, rt(1), t);
|
|
2504
|
+
}
|
|
2505
|
+
function ct(e) {
|
|
2506
|
+
let t = (e) => C(e, 0, 0, rt(0)), n = (() => {
|
|
2507
|
+
let e = new x();
|
|
2508
|
+
return e.u32(0), e.u32(0), C("stsz", 0, 0, e.toUint8Array());
|
|
2509
|
+
})();
|
|
2510
|
+
return S("stbl", st(e), t("stts"), t("stsc"), n, t("stco"));
|
|
2511
|
+
}
|
|
2512
|
+
function lt(e) {
|
|
2513
|
+
return S("minf", e.kind === "video" ? et() : tt(), nt(), ct(e));
|
|
2514
|
+
}
|
|
2515
|
+
function ut(e) {
|
|
2516
|
+
let t = new x();
|
|
2517
|
+
return t.u32(e), t.u32(1), t.u32(0), t.u32(0), t.u32(0), S("mvex", C("trex", 0, 0, t.toUint8Array()));
|
|
2518
|
+
}
|
|
2519
|
+
function dt(e) {
|
|
2520
|
+
let t = e.id || 1, n = S("moov", Xe(), S("trak", Ze(e, t), S("mdia", Qe(e), $e(e), lt(e))), ut(t));
|
|
2521
|
+
return _(Ye(), n);
|
|
2522
|
+
}
|
|
2523
|
+
function ft(e) {
|
|
2524
|
+
return `video/mp4; codecs="${e.codec === "hevc" ? j(e.codecPrivate) : ue(e.codecPrivate)}"`;
|
|
2525
|
+
}
|
|
2526
|
+
function pt(e) {
|
|
2527
|
+
return e.codec === "g711a" ? "audio/mp4; codecs=\"alaw\"" : e.codec === "g711u" ? "audio/mp4; codecs=\"ulaw\"" : e.codec === "opus" ? "audio/mp4; codecs=\"opus\"" : `audio/mp4; codecs="${e.codec === "aac" ? I(e.codecPrivate) : "mp4a.40.34"}"`;
|
|
2528
|
+
}
|
|
2529
|
+
function J(e) {
|
|
2530
|
+
return e.kind === "video" ? ft(e) : pt(e);
|
|
2531
|
+
}
|
|
2532
|
+
function mt(e) {
|
|
2533
|
+
if (e.kind === "video") {
|
|
2534
|
+
if (e.codec === "hevc") return `HEVC (${j(e.codecPrivate)})`;
|
|
2535
|
+
let { profile: t, level: n } = de(e.codecPrivate);
|
|
2536
|
+
return `H.264 (avc1.${t.toString(16).padStart(2, "0")}${n.toString(16).padStart(2, "0")})`;
|
|
2537
|
+
}
|
|
2538
|
+
return e.codec === "aac" ? "AAC" : e.codec === "g711a" ? "G.711 A-law" : e.codec === "g711u" ? "G.711 μ-law" : e.codec.toUpperCase();
|
|
2539
|
+
}
|
|
2540
|
+
function ht(e) {
|
|
2541
|
+
if (e.kind === "video") {
|
|
2542
|
+
let t = e.fps && e.fps > 1 ? e.fps : 25;
|
|
2543
|
+
return Math.max(1, Math.round(e.timescale / t));
|
|
2544
|
+
}
|
|
2545
|
+
return Math.max(1, Math.round(1024 * e.timescale / (e.sampleRate || 48e3)));
|
|
2546
|
+
}
|
|
2547
|
+
function gt(e, t, n, r, i) {
|
|
2548
|
+
return S("moof", (() => {
|
|
2549
|
+
let t = new x();
|
|
2550
|
+
return t.u32(e), C("mfhd", 0, 0, t.toUint8Array());
|
|
2551
|
+
})(), S("traf", (() => {
|
|
2552
|
+
let e = new x();
|
|
2553
|
+
return e.u32(t.id || 1), C("tfhd", 0, 131072, e.toUint8Array());
|
|
2554
|
+
})(), (() => {
|
|
2555
|
+
let e = new x();
|
|
2556
|
+
return e.u64(n), C("tfdt", 1, 0, e.toUint8Array());
|
|
2557
|
+
})(), (() => {
|
|
2558
|
+
let e = new x();
|
|
2559
|
+
e.u32(r.length), e.i32(i);
|
|
2560
|
+
for (let t of r) e.u32(t.duration), e.u32(t.size), e.u32(t.flags), e.i32(t.cts);
|
|
2561
|
+
return C("trun", 1, 3841, e.toUint8Array());
|
|
2562
|
+
})()));
|
|
2563
|
+
}
|
|
2564
|
+
var _t = class {
|
|
2565
|
+
constructor(e) {
|
|
2566
|
+
r(this, "track", void 0), r(this, "sequence", 1), r(this, "fallbackDuration", void 0), this.track = e, this.fallbackDuration = ht(e);
|
|
2567
|
+
}
|
|
2568
|
+
initSegment() {
|
|
2569
|
+
return dt(this.track);
|
|
2570
|
+
}
|
|
2571
|
+
reset() {
|
|
2572
|
+
this.sequence = 1;
|
|
2573
|
+
}
|
|
2574
|
+
fragment(e) {
|
|
2575
|
+
if (e.length === 0) return /* @__PURE__ */ new Uint8Array();
|
|
2576
|
+
let t = this.normalize(e), n = t[0].dts, r = t.map((e) => ({
|
|
2577
|
+
duration: e.duration > 0 ? Math.round(e.duration) : this.fallbackDuration,
|
|
2578
|
+
size: e.data.length,
|
|
2579
|
+
flags: e.keyframe ? 33554432 : 16842752,
|
|
2580
|
+
cts: Math.round(e.pts - e.dts)
|
|
2581
|
+
})), i = this.sequence++, a = gt(i, this.track, n, r, 0).length + 8;
|
|
2582
|
+
return _(gt(i, this.track, n, r, a), S("mdat", ...t.map((e) => e.data)));
|
|
2583
|
+
}
|
|
2584
|
+
normalize(e) {
|
|
2585
|
+
if (e.length <= 1) return e;
|
|
2586
|
+
let t = [...e].sort((e, t) => e.dts - t.dts), n = t[0].dts;
|
|
2587
|
+
for (let e = 1; e < t.length; e++) t[e].dts <= n && (t[e] = {
|
|
2588
|
+
...t[e],
|
|
2589
|
+
dts: n + 1
|
|
2590
|
+
}), n = t[e].dts;
|
|
2591
|
+
return t;
|
|
2592
|
+
}
|
|
2593
|
+
}, vt = /* @__PURE__ */ St(), yt = /* @__PURE__ */ Ct();
|
|
2594
|
+
function bt(e) {
|
|
2595
|
+
let t = e ^ 85, n = (t & 15) << 4, r = (t & 112) >> 4;
|
|
2596
|
+
return r === 0 ? n += 8 : r === 1 ? n += 264 : (n += 264, n <<= r - 1), t & 128 ? n : -n;
|
|
2597
|
+
}
|
|
2598
|
+
function xt(e) {
|
|
2599
|
+
let t = ~e & 255, n = ((t & 15) << 3) + 132;
|
|
2600
|
+
return n <<= (t & 112) >> 4, t & 128 ? 132 - n : n - 132;
|
|
2601
|
+
}
|
|
2602
|
+
function St() {
|
|
2603
|
+
let e = /* @__PURE__ */ new Int16Array(256);
|
|
2604
|
+
for (let t = 0; t < 256; t++) e[t] = bt(t);
|
|
2605
|
+
return e;
|
|
2606
|
+
}
|
|
2607
|
+
function Ct() {
|
|
2608
|
+
let e = /* @__PURE__ */ new Int16Array(256);
|
|
2609
|
+
for (let t = 0; t < 256; t++) e[t] = xt(t);
|
|
2610
|
+
return e;
|
|
2611
|
+
}
|
|
2612
|
+
function wt(e, t) {
|
|
2613
|
+
let n = t === "alaw" ? vt : yt, r = new Float32Array(e.length);
|
|
2614
|
+
for (let t = 0; t < e.length; t++) r[t] = n[e[t]] / 32768;
|
|
2615
|
+
return r;
|
|
2616
|
+
}
|
|
2617
|
+
// src/render/audio.ts
|
|
2618
|
+
var Y = globalThis, Tt = class {
|
|
2619
|
+
constructor(e) {
|
|
2620
|
+
r(this, "options", void 0), r(this, "decoder", null), r(this, "track", null), r(this, "manualLaw", null), r(this, "ctx", null), r(this, "gain", null), r(this, "anchorMedia", 0), r(this, "anchorCtx", 0), r(this, "anchored", !1), r(this, "volume", void 0), r(this, "destroyed", !1), r(this, "clockSource", null), r(this, "decodedFrames", 0), this.options = e, this.volume = e.volume;
|
|
2621
|
+
}
|
|
2622
|
+
get configured() {
|
|
2623
|
+
return this.decoder !== null || this.manualLaw !== null;
|
|
2624
|
+
}
|
|
2625
|
+
configure(e) {
|
|
2626
|
+
if (this.track = e, e.codec === "g711a" || e.codec === "g711u") {
|
|
2627
|
+
this.manualLaw = e.codec === "g711a" ? "alaw" : "ulaw", this.decoder = null;
|
|
2628
|
+
return;
|
|
2629
|
+
}
|
|
2630
|
+
let t = Y.AudioDecoder;
|
|
2631
|
+
if (t && e.codecPrivate) try {
|
|
2632
|
+
this.decoder = new t({
|
|
2633
|
+
output: (e) => this.onData(e),
|
|
2634
|
+
error: (e) => {
|
|
2635
|
+
this.options.onError?.(/* @__PURE__ */ Error(`audio decoder: ${e?.message ?? e}`));
|
|
2636
|
+
}
|
|
2637
|
+
}), this.decoder.configure({
|
|
2638
|
+
codec: I(e.codecPrivate),
|
|
2639
|
+
sampleRate: e.sampleRate,
|
|
2640
|
+
numberOfChannels: e.channels,
|
|
2641
|
+
description: e.codecPrivate
|
|
2642
|
+
});
|
|
2643
|
+
} catch (e) {
|
|
2644
|
+
this.decoder = null, this.options.onError?.(e instanceof Error ? e : Error(String(e)));
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
write(e) {
|
|
2648
|
+
if (this.destroyed || e.length === 0) return;
|
|
2649
|
+
if (this.manualLaw) {
|
|
2650
|
+
this.writeManual(e, this.manualLaw);
|
|
2651
|
+
return;
|
|
2652
|
+
}
|
|
2653
|
+
let t = this.decoder, n = Y.EncodedAudioChunk, r = this.track;
|
|
2654
|
+
if (t && n && r) for (let i of e) {
|
|
2655
|
+
if (t.decodeQueueSize > 60) return;
|
|
2656
|
+
try {
|
|
2657
|
+
t.decode(new n({
|
|
2658
|
+
type: "key",
|
|
2659
|
+
timestamp: Math.round(i.pts * 1e6 / r.timescale),
|
|
2660
|
+
duration: Math.round(i.duration * 1e6 / r.timescale),
|
|
2661
|
+
data: i.data
|
|
2662
|
+
}));
|
|
2663
|
+
} catch {
|
|
2664
|
+
return;
|
|
2665
|
+
}
|
|
2666
|
+
}
|
|
2667
|
+
}
|
|
2668
|
+
writeManual(e, t) {
|
|
2669
|
+
let n = this.ensureAudioContext(), r = this.track;
|
|
2670
|
+
if (n && r && n.state === "running") for (let n of e) {
|
|
2671
|
+
if (n.data.length === 0) continue;
|
|
2672
|
+
let e = wt(n.data, t);
|
|
2673
|
+
this.schedule([e], r.sampleRate, n.pts / r.timescale), this.decodedFrames++;
|
|
2674
|
+
}
|
|
2675
|
+
}
|
|
2676
|
+
get clock() {
|
|
2677
|
+
return this.clockSource ? this.clockSource() : !this.anchored || !this.ctx ? null : this.anchorMedia + (this.ctx.currentTime - this.anchorCtx);
|
|
2678
|
+
}
|
|
2679
|
+
setClockSource(e) {
|
|
2680
|
+
this.clockSource = e;
|
|
2681
|
+
}
|
|
2682
|
+
ensureAudioContext() {
|
|
2683
|
+
if (this.ctx) return this.ctx;
|
|
2684
|
+
let e = Y.AudioContext || Y.webkitAudioContext;
|
|
2685
|
+
return e ? (this.ctx = new e(), this.gain = this.ctx.createGain(), this.gain.gain.value = this.volume, this.gain.connect(this.ctx.destination), this.ctx) : null;
|
|
2686
|
+
}
|
|
2687
|
+
onData(e) {
|
|
2688
|
+
let t = this.ensureAudioContext();
|
|
2689
|
+
if (!t || !this.gain || t.state !== "running") try {
|
|
2690
|
+
e.close();
|
|
2691
|
+
} catch {}
|
|
2692
|
+
else try {
|
|
2693
|
+
let t = e.numberOfChannels, n = e.numberOfFrames, r = [];
|
|
2694
|
+
for (let i = 0; i < t; i++) {
|
|
2695
|
+
let t = new Float32Array(n);
|
|
2696
|
+
try {
|
|
2697
|
+
e.copyTo(t, {
|
|
2698
|
+
planeIndex: i,
|
|
2699
|
+
format: "f32-planar"
|
|
2700
|
+
});
|
|
2701
|
+
} catch {
|
|
2702
|
+
e.copyTo(t, { planeIndex: i });
|
|
2703
|
+
}
|
|
2704
|
+
r.push(t);
|
|
2705
|
+
}
|
|
2706
|
+
this.schedule(r, e.sampleRate, e.timestamp / 1e6), this.decodedFrames++;
|
|
2707
|
+
} finally {
|
|
2708
|
+
try {
|
|
2709
|
+
e.close();
|
|
2710
|
+
} catch {}
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
schedule(e, t, n) {
|
|
2714
|
+
let r = this.ensureAudioContext();
|
|
2715
|
+
if (!r || !this.gain) return;
|
|
2716
|
+
let i = r.currentTime, a, o = this.clockSource?.();
|
|
2717
|
+
if (o != null) {
|
|
2718
|
+
let e = n - o;
|
|
2719
|
+
if (e < -.3 || e > 2) return;
|
|
2720
|
+
a = i + Math.max(0, e) + .02;
|
|
2721
|
+
} else if (this.anchored || (this.anchored = !0, this.anchorMedia = n, this.anchorCtx = i + .06), a = this.anchorCtx + (n - this.anchorMedia), a < i + .01 && (a = i + .04, this.anchorCtx = a, this.anchorMedia = n), a > i + 1.5) return;
|
|
2722
|
+
let s = r.createBuffer(e.length, e[0].length, t);
|
|
2723
|
+
for (let t = 0; t < e.length; t++) s.copyToChannel(e[t], t);
|
|
2724
|
+
let c = r.createBufferSource();
|
|
2725
|
+
c.buffer = s, c.connect(this.gain), c.onended = () => {
|
|
2726
|
+
try {
|
|
2727
|
+
c.disconnect();
|
|
2728
|
+
} catch {}
|
|
2729
|
+
}, c.start(a);
|
|
2730
|
+
}
|
|
2731
|
+
resume() {
|
|
2732
|
+
return this.ensureAudioContext()?.resume();
|
|
2733
|
+
}
|
|
2734
|
+
setPaused(e) {
|
|
2735
|
+
this.ctx && (e ? this.ctx.suspend() : this.ctx.resume());
|
|
2736
|
+
}
|
|
2737
|
+
setVolume(e) {
|
|
2738
|
+
this.volume = e, this.gain && (this.gain.gain.value = e);
|
|
2739
|
+
}
|
|
2740
|
+
reset() {
|
|
2741
|
+
try {
|
|
2742
|
+
this.decoder?.reset();
|
|
2743
|
+
} catch {}
|
|
2744
|
+
this.anchored = !1;
|
|
2745
|
+
}
|
|
2746
|
+
destroy() {
|
|
2747
|
+
this.destroyed = !0;
|
|
2748
|
+
try {
|
|
2749
|
+
this.decoder?.close();
|
|
2750
|
+
} catch {}
|
|
2751
|
+
this.decoder = null, this.manualLaw = null, this.ctx && (this.ctx.close(), this.ctx = null), this.gain = null;
|
|
2752
|
+
}
|
|
2753
|
+
}, X = class {
|
|
2754
|
+
constructor(e, t) {
|
|
2755
|
+
r(this, "video", void 0), r(this, "options", void 0), r(this, "mode", "mse"), r(this, "mediaSource", null), r(this, "objectUrl", null), r(this, "sourceOpen", !1), r(this, "buffers", /* @__PURE__ */ new Map()), r(this, "queues", /* @__PURE__ */ new Map()), r(this, "muxers", /* @__PURE__ */ new Map()), r(this, "attached", /* @__PURE__ */ new Set()), r(this, "trackList", /* @__PURE__ */ new Map()), r(this, "fallbackTracks", /* @__PURE__ */ new Set()), r(this, "fallbackAudio", null), r(this, "endPending", !1), r(this, "destroyed", !1), this.video = e, this.options = t;
|
|
2756
|
+
}
|
|
2757
|
+
get stats() {
|
|
2758
|
+
let e = this.video.getVideoPlaybackQuality?.();
|
|
2759
|
+
return {
|
|
2760
|
+
decodedVideoFrames: e?.totalVideoFrames ?? 0,
|
|
2761
|
+
droppedVideoFrames: e?.droppedVideoFrames ?? 0,
|
|
2762
|
+
decodedAudioFrames: this.fallbackAudio?.decodedFrames ?? 0
|
|
2763
|
+
};
|
|
2764
|
+
}
|
|
2765
|
+
async attach(e) {
|
|
2766
|
+
let t = e.filter((e) => !this.attached.has(e.id));
|
|
2767
|
+
if (t.length === 0) return;
|
|
2768
|
+
let n = (e) => {
|
|
2769
|
+
try {
|
|
2770
|
+
return MediaSource.isTypeSupported(J(e));
|
|
2771
|
+
} catch {
|
|
2772
|
+
return !1;
|
|
2773
|
+
}
|
|
2774
|
+
};
|
|
2775
|
+
t.some(n) && !this.mediaSource && await this.initMediaSource();
|
|
2776
|
+
for (let e of t) if (this.attached.add(e.id), this.trackList.set(e.id, e), !n(e)) {
|
|
2777
|
+
if (e.kind !== "audio") throw new i("MSE_UNSUPPORTED_CODEC", `MediaSource does not support ${J(e)}`);
|
|
2778
|
+
this.ensureFallbackAudio().configure(e), this.fallbackTracks.add(e.id);
|
|
2779
|
+
} else this.addTrack(e);
|
|
2780
|
+
}
|
|
2781
|
+
ensureFallbackAudio() {
|
|
2782
|
+
return this.fallbackAudio || (this.fallbackAudio = new Tt({
|
|
2783
|
+
volume: this.video.muted ? 0 : this.video.volume,
|
|
2784
|
+
onError: this.options.onError
|
|
2785
|
+
}), this.fallbackAudio.setClockSource(() => Number.isFinite(this.video.currentTime) ? this.video.currentTime : 0)), this.fallbackAudio;
|
|
2786
|
+
}
|
|
2787
|
+
async initMediaSource() {
|
|
2788
|
+
if (typeof MediaSource > "u") throw new i("MSE_UNAVAILABLE", "MediaSource is not available in this browser");
|
|
2789
|
+
this.mediaSource = new MediaSource(), this.objectUrl = URL.createObjectURL(this.mediaSource), this.video.src = this.objectUrl, await new Promise((e, t) => {
|
|
2790
|
+
let n = this.mediaSource, r = setTimeout(() => t(new i("MSE_TIMEOUT", "MediaSource did not open")), 1e4);
|
|
2791
|
+
n.addEventListener("sourceopen", () => {
|
|
2792
|
+
clearTimeout(r), this.sourceOpen = !0, e();
|
|
2793
|
+
}, { once: !0 }), n.readyState === "open" && (clearTimeout(r), this.sourceOpen = !0, e());
|
|
2794
|
+
});
|
|
2795
|
+
}
|
|
2796
|
+
addTrack(e) {
|
|
2797
|
+
let t = this.mediaSource, n = J(e);
|
|
2798
|
+
if (!MediaSource.isTypeSupported(n)) throw new i("MSE_UNSUPPORTED_CODEC", `MediaSource does not support ${n}. Try decoder: 'webcodecs'.`);
|
|
2799
|
+
let r = t.addSourceBuffer(n);
|
|
2800
|
+
r.mode = "segments", this.buffers.set(e.id, r), this.queues.set(e.id, []);
|
|
2801
|
+
let a = new _t(e);
|
|
2802
|
+
this.muxers.set(e.id, a), r.addEventListener("updateend", () => this.pump(e.id)), r.addEventListener("error", () => {
|
|
2803
|
+
this.options.onError?.(new i("MSE_APPEND_ERROR", `SourceBuffer error for track ${e.id}`));
|
|
2804
|
+
}), this.enqueue(e.id, a.initSegment());
|
|
2805
|
+
}
|
|
2806
|
+
write(e, t) {
|
|
2807
|
+
if (t.length === 0 || this.destroyed) return;
|
|
2808
|
+
if (this.fallbackTracks.has(e.id)) {
|
|
2809
|
+
this.fallbackAudio?.write(t);
|
|
2810
|
+
return;
|
|
2811
|
+
}
|
|
2812
|
+
let n = this.muxers.get(e.id);
|
|
2813
|
+
if (!n) return;
|
|
2814
|
+
let r = n.fragment(t);
|
|
2815
|
+
r.length !== 0 && this.enqueue(e.id, r);
|
|
2816
|
+
}
|
|
2817
|
+
enqueue(e, t) {
|
|
2818
|
+
let n = this.queues.get(e);
|
|
2819
|
+
n && (n.push({ data: t }), this.pump(e));
|
|
2820
|
+
}
|
|
2821
|
+
pump(e) {
|
|
2822
|
+
let t = this.buffers.get(e), n = this.queues.get(e);
|
|
2823
|
+
if (!t || !n || !this.sourceOpen || this.destroyed || t.updating) return;
|
|
2824
|
+
let r = n.shift();
|
|
2825
|
+
if (r) try {
|
|
2826
|
+
t.appendBuffer(r.data);
|
|
2827
|
+
} catch (t) {
|
|
2828
|
+
t instanceof DOMException && (t.name === "QuotaExceededError" || t.code === 22) ? (this.trimBuffer(e), n.unshift(r)) : this.options.onError?.(t instanceof Error ? t : Error(String(t)));
|
|
2829
|
+
}
|
|
2830
|
+
else if (this.endPending && n.length === 0 && this.nothingBuffering() && this.mediaSource?.readyState === "open") {
|
|
2831
|
+
try {
|
|
2832
|
+
this.mediaSource.endOfStream();
|
|
2833
|
+
} catch {}
|
|
2834
|
+
this.endPending = !1;
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
nothingBuffering() {
|
|
2838
|
+
for (let e of this.buffers.values()) if (e.updating) return !1;
|
|
2839
|
+
for (let e of this.queues.values()) if (e.length > 0) return !1;
|
|
2840
|
+
return !0;
|
|
2841
|
+
}
|
|
2842
|
+
trimOld(e = 60) {
|
|
2843
|
+
let t = this.video.currentTime - e;
|
|
2844
|
+
if (!(t <= 10)) {
|
|
2845
|
+
for (let e of this.buffers.values()) if (!e.updating) try {
|
|
2846
|
+
e.buffered.length > 0 && e.buffered.start(0) < t && e.remove(0, t);
|
|
2847
|
+
} catch {}
|
|
2848
|
+
}
|
|
2849
|
+
}
|
|
2850
|
+
trimBuffer(e) {
|
|
2851
|
+
let t = this.buffers.get(e);
|
|
2852
|
+
if (!t || t.updating) return;
|
|
2853
|
+
let n = this.options.live ? 20 : 120, r = Math.max(0, this.video.currentTime - n);
|
|
2854
|
+
if (!(r <= 0)) try {
|
|
2855
|
+
t.buffered.length > 0 && t.buffered.start(0) < r && t.remove(0, r);
|
|
2856
|
+
} catch {}
|
|
2857
|
+
}
|
|
2858
|
+
endOfStream() {
|
|
2859
|
+
this.endPending = !0;
|
|
2860
|
+
for (let e of this.buffers.keys()) this.pump(e);
|
|
2861
|
+
}
|
|
2862
|
+
flush() {}
|
|
2863
|
+
reset() {
|
|
2864
|
+
for (let e of this.queues.values()) e.length = 0;
|
|
2865
|
+
for (let e of this.muxers.values()) e.reset();
|
|
2866
|
+
this.fallbackAudio?.reset(), this.endPending = !1;
|
|
2867
|
+
}
|
|
2868
|
+
setVolume(e) {
|
|
2869
|
+
let t = Math.min(1, Math.max(0, e));
|
|
2870
|
+
this.video.volume = t, this.fallbackAudio?.setVolume(this.video.muted ? 0 : t);
|
|
2871
|
+
}
|
|
2872
|
+
resumeAudio() {
|
|
2873
|
+
return this.fallbackAudio?.resume();
|
|
2874
|
+
}
|
|
2875
|
+
setPaused(e) {
|
|
2876
|
+
this.fallbackAudio?.setPaused(e);
|
|
2877
|
+
}
|
|
2878
|
+
destroy() {
|
|
2879
|
+
this.destroyed = !0;
|
|
2880
|
+
for (let e of this.buffers.values()) try {
|
|
2881
|
+
this.mediaSource?.readyState === "open" && !e.updating && this.mediaSource.removeSourceBuffer(e);
|
|
2882
|
+
} catch {}
|
|
2883
|
+
this.fallbackAudio?.destroy(), this.fallbackAudio = null, this.fallbackTracks.clear(), this.buffers.clear(), this.queues.clear(), this.muxers.clear(), this.attached.clear(), this.objectUrl && URL.revokeObjectURL(this.objectUrl), this.objectUrl = null, this.mediaSource = null, this.sourceOpen = !1;
|
|
2884
|
+
try {
|
|
2885
|
+
this.video.removeAttribute("src"), this.video.load();
|
|
2886
|
+
} catch {}
|
|
2887
|
+
}
|
|
2888
|
+
get bufferAhead() {
|
|
2889
|
+
try {
|
|
2890
|
+
let e = this.video.buffered;
|
|
2891
|
+
if (e.length === 0) return 0;
|
|
2892
|
+
for (let t = 0; t < e.length; t++) if (e.start(t) <= this.video.currentTime && e.end(t) >= this.video.currentTime) return e.end(t) - this.video.currentTime;
|
|
2893
|
+
return Math.max(0, e.end(e.length - 1) - this.video.currentTime);
|
|
2894
|
+
} catch {
|
|
2895
|
+
return 0;
|
|
2896
|
+
}
|
|
2897
|
+
}
|
|
2898
|
+
}, Z = globalThis;
|
|
2899
|
+
function Et() {
|
|
2900
|
+
return Z.VideoDecoder !== void 0 && Z.EncodedVideoChunk !== void 0;
|
|
2901
|
+
}
|
|
2902
|
+
var Q = class {
|
|
2903
|
+
constructor(e, t) {
|
|
2904
|
+
r(this, "options", void 0), r(this, "mode", "webcodecs"), r(this, "videoDecoder", null), r(this, "audio", void 0), r(this, "canvas", void 0), r(this, "ctx", null), r(this, "frames", []), r(this, "videoBacklog", []), r(this, "rafId", 0), r(this, "paused", !1), r(this, "anchored", !1), r(this, "anchorWall", 0), r(this, "anchorMedia", 0), r(this, "decodedVideo", 0), r(this, "droppedVideo", 0), r(this, "hardware", "unknown"), r(this, "firstFrameReported", !1), r(this, "destroyed", !1), r(this, "tick", () => {
|
|
2905
|
+
if (this.rafId = 0, this.paused || !this.anchored || this.destroyed) return;
|
|
2906
|
+
let e = this.mediaTime(), t = null;
|
|
2907
|
+
for (; this.frames.length > 0 && this.frames[0].timestamp / 1e6 <= e + .002;) t && (t.close(), this.droppedVideo++), t = this.frames.shift();
|
|
2908
|
+
t && this.render(t), this.pumpVideo(), this.frames.length > 0 && (this.rafId = requestAnimationFrame(this.tick));
|
|
2909
|
+
}), this.options = e, this.canvas = t ?? document.createElement("canvas"), this.canvas.style.width = "100%", this.canvas.style.height = "100%", this.canvas.style.display = "block", this.ctx = this.canvas.getContext("2d", {
|
|
2910
|
+
alpha: !1,
|
|
2911
|
+
desynchronized: !0
|
|
2912
|
+
}), this.audio = new Tt({
|
|
2913
|
+
volume: e.volume,
|
|
2914
|
+
onError: e.onError
|
|
2915
|
+
});
|
|
2916
|
+
}
|
|
2917
|
+
get stats() {
|
|
2918
|
+
return {
|
|
2919
|
+
decodedVideoFrames: this.decodedVideo,
|
|
2920
|
+
droppedVideoFrames: this.droppedVideo,
|
|
2921
|
+
decodedAudioFrames: this.audio.decodedFrames
|
|
2922
|
+
};
|
|
2923
|
+
}
|
|
2924
|
+
get currentTime() {
|
|
2925
|
+
return this.anchored ? this.mediaTime() : 0;
|
|
2926
|
+
}
|
|
2927
|
+
get framesPending() {
|
|
2928
|
+
return this.frames.length + this.videoBacklog.length;
|
|
2929
|
+
}
|
|
2930
|
+
get acceleration() {
|
|
2931
|
+
return this.hardware;
|
|
2932
|
+
}
|
|
2933
|
+
get element() {
|
|
2934
|
+
return this.canvas;
|
|
2935
|
+
}
|
|
2936
|
+
async attach(e) {
|
|
2937
|
+
let t = e.find((e) => e.kind === "video");
|
|
2938
|
+
t && !this.videoDecoder && await this.configureVideo(t);
|
|
2939
|
+
let n = e.find((e) => e.kind === "audio");
|
|
2940
|
+
n && !this.audio.configured && this.audio.configure(n);
|
|
2941
|
+
}
|
|
2942
|
+
async configureVideo(e) {
|
|
2943
|
+
let t = Z.VideoDecoder;
|
|
2944
|
+
if (!t) throw new i("WEBCODECS_UNAVAILABLE", "WebCodecs VideoDecoder is not available");
|
|
2945
|
+
let n = e.codec === "hevc" ? j(e.codecPrivate) : ue(e.codecPrivate), r = {
|
|
2946
|
+
codec: n,
|
|
2947
|
+
description: e.codecPrivate,
|
|
2948
|
+
optimizeForLatency: !0
|
|
2949
|
+
}, a = [];
|
|
2950
|
+
this.options.hardwareAcceleration !== "no-preference" && a.push({
|
|
2951
|
+
...r,
|
|
2952
|
+
hardwareAcceleration: this.options.hardwareAcceleration
|
|
2953
|
+
}), a.push(r);
|
|
2954
|
+
try {
|
|
2955
|
+
let e = null, r = null;
|
|
2956
|
+
for (let n of a) {
|
|
2957
|
+
if (typeof t.isConfigSupported != "function") {
|
|
2958
|
+
e = n;
|
|
2959
|
+
break;
|
|
2960
|
+
}
|
|
2961
|
+
let i = await t.isConfigSupported(n);
|
|
2962
|
+
if (r = i, i?.supported) {
|
|
2963
|
+
e = n;
|
|
2964
|
+
break;
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
if (!e) throw new i("WEBCODECS_UNSUPPORTED", `WebCodecs cannot decode ${n}`, r);
|
|
2968
|
+
this.hardware = e.hardwareAcceleration === "prefer-hardware" ? "preferred" : "software", this.videoDecoder = new t({
|
|
2969
|
+
output: (e) => this.onFrame(e),
|
|
2970
|
+
error: (e) => {
|
|
2971
|
+
let t = `video decoder: ${e?.message ?? e}`;
|
|
2972
|
+
this.options.onError?.(Error(t));
|
|
2973
|
+
}
|
|
2974
|
+
}), this.videoDecoder.configure(e);
|
|
2975
|
+
} catch (e) {
|
|
2976
|
+
throw e instanceof i ? e : new i("WEBCODECS_CONFIG_FAILED", `failed to configure VideoDecoder for ${n}`, e);
|
|
2977
|
+
}
|
|
2978
|
+
}
|
|
2979
|
+
write(e, t) {
|
|
2980
|
+
this.destroyed || t.length === 0 || (e.kind === "video" ? this.writeVideo(e, t) : this.audio.write(t));
|
|
2981
|
+
}
|
|
2982
|
+
writeVideo(e, t) {
|
|
2983
|
+
for (let n of t) this.videoBacklog.push({
|
|
2984
|
+
track: e,
|
|
2985
|
+
sample: n
|
|
2986
|
+
});
|
|
2987
|
+
this.pumpVideo();
|
|
2988
|
+
}
|
|
2989
|
+
pumpVideo() {
|
|
2990
|
+
let e = this.videoDecoder, t = Z.EncodedVideoChunk;
|
|
2991
|
+
if (e && t && !this.destroyed) for (; this.videoBacklog.length > 0 && !(this.frames.length + e.decodeQueueSize >= 8);) {
|
|
2992
|
+
let { track: n, sample: r } = this.videoBacklog.shift(), i = new t({
|
|
2993
|
+
type: r.keyframe ? "key" : "delta",
|
|
2994
|
+
timestamp: Math.round(r.pts * 1e6 / n.timescale),
|
|
2995
|
+
duration: Math.round(r.duration * 1e6 / n.timescale),
|
|
2996
|
+
data: r.data
|
|
2997
|
+
});
|
|
2998
|
+
try {
|
|
2999
|
+
e.decode(i);
|
|
3000
|
+
} catch (e) {
|
|
3001
|
+
this.options.onError?.(e instanceof Error ? e : Error(String(e)));
|
|
3002
|
+
return;
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
}
|
|
3006
|
+
get backlogSeconds() {
|
|
3007
|
+
if (this.videoBacklog.length === 0) return 0;
|
|
3008
|
+
let e = this.videoBacklog[0];
|
|
3009
|
+
return Math.max(0, e.sample.pts / e.track.timescale - this.currentTime);
|
|
3010
|
+
}
|
|
3011
|
+
onFrame(e) {
|
|
3012
|
+
for (this.decodedVideo++, this.frames.push(e); this.frames.length > 16;) this.frames.shift()?.close(), this.droppedVideo++;
|
|
3013
|
+
this.anchored || (this.anchored = !0, this.anchorWall = performance.now(), this.anchorMedia = e.timestamp / 1e6), this.startRaf(), this.pumpVideo();
|
|
3014
|
+
}
|
|
3015
|
+
mediaTime() {
|
|
3016
|
+
let e = this.audio.clock;
|
|
3017
|
+
return e === null ? this.anchorMedia + (performance.now() - this.anchorWall) / 1e3 : e;
|
|
3018
|
+
}
|
|
3019
|
+
startRaf() {
|
|
3020
|
+
this.rafId !== 0 || this.paused || this.destroyed || (this.rafId = requestAnimationFrame(this.tick));
|
|
3021
|
+
}
|
|
3022
|
+
render(e) {
|
|
3023
|
+
let t = this.ctx;
|
|
3024
|
+
if (!t) {
|
|
3025
|
+
e.close();
|
|
3026
|
+
return;
|
|
3027
|
+
}
|
|
3028
|
+
let n = e.displayWidth, r = e.displayHeight;
|
|
3029
|
+
!this.firstFrameReported && n > 0 && r > 0 && (this.firstFrameReported = !0, this.options.onFirstFrame?.(n, r)), (this.canvas.width !== n || this.canvas.height !== r) && (this.canvas.width = n, this.canvas.height = r);
|
|
3030
|
+
let i = this.canvas.width, a = this.canvas.height;
|
|
3031
|
+
t.fillStyle = "#000", t.fillRect(0, 0, i, a);
|
|
3032
|
+
let o = i, s = a;
|
|
3033
|
+
if (this.options.fit === "contain") {
|
|
3034
|
+
let e = Math.min(i / n, a / r);
|
|
3035
|
+
o = n * e, s = r * e;
|
|
3036
|
+
} else if (this.options.fit === "cover") {
|
|
3037
|
+
let e = Math.max(i / n, a / r);
|
|
3038
|
+
o = n * e, s = r * e;
|
|
3039
|
+
}
|
|
3040
|
+
t.drawImage(e, (i - o) / 2, (a - s) / 2, o, s), e.close();
|
|
3041
|
+
}
|
|
3042
|
+
setPaused(e) {
|
|
3043
|
+
if (e !== this.paused && (this.paused = e, this.audio.setPaused(e), !e)) {
|
|
3044
|
+
this.anchored = !1;
|
|
3045
|
+
let e = this.frames[0];
|
|
3046
|
+
e && (this.anchored = !0, this.anchorWall = performance.now(), this.anchorMedia = e.timestamp / 1e6), this.startRaf();
|
|
3047
|
+
}
|
|
3048
|
+
}
|
|
3049
|
+
resumeAudio() {
|
|
3050
|
+
return this.audio.resume();
|
|
3051
|
+
}
|
|
3052
|
+
setVolume(e) {
|
|
3053
|
+
this.audio.setVolume(e);
|
|
3054
|
+
}
|
|
3055
|
+
flush() {}
|
|
3056
|
+
endOfStream() {}
|
|
3057
|
+
reset() {
|
|
3058
|
+
try {
|
|
3059
|
+
this.videoDecoder?.reset();
|
|
3060
|
+
} catch {}
|
|
3061
|
+
this.audio.reset();
|
|
3062
|
+
for (let e of this.frames) e.close();
|
|
3063
|
+
this.frames = [], this.videoBacklog = [], this.anchored = !1, this.firstFrameReported = !1;
|
|
3064
|
+
}
|
|
3065
|
+
destroy() {
|
|
3066
|
+
this.destroyed = !0, this.rafId && cancelAnimationFrame(this.rafId), this.rafId = 0;
|
|
3067
|
+
for (let e of this.frames) try {
|
|
3068
|
+
e.close();
|
|
3069
|
+
} catch {}
|
|
3070
|
+
this.frames = [], this.videoBacklog = [];
|
|
3071
|
+
try {
|
|
3072
|
+
this.videoDecoder?.close();
|
|
3073
|
+
} catch {}
|
|
3074
|
+
this.videoDecoder = null, this.audio.destroy();
|
|
3075
|
+
}
|
|
3076
|
+
};
|
|
3077
|
+
// src/soft/libde265.ts
|
|
3078
|
+
function Dt(e) {
|
|
3079
|
+
return {
|
|
3080
|
+
width: e.width,
|
|
3081
|
+
height: e.height,
|
|
3082
|
+
stride: e.stride,
|
|
3083
|
+
bytes: e.bytes.slice(0, e.stride * e.height)
|
|
3084
|
+
};
|
|
3085
|
+
}
|
|
3086
|
+
var Ot = class {
|
|
3087
|
+
constructor() {
|
|
3088
|
+
r(this, "name", "libde265"), r(this, "module", null), r(this, "decoder", null), r(this, "parameterSets", []);
|
|
3089
|
+
}
|
|
3090
|
+
async configure(e) {
|
|
3091
|
+
let t = N(e.codecPrivate);
|
|
3092
|
+
if (this.parameterSets = [
|
|
3093
|
+
...t.vps,
|
|
3094
|
+
...t.sps,
|
|
3095
|
+
...t.pps
|
|
3096
|
+
].filter((e) => e.length > 0), this.parameterSets.length === 0) throw new i("SOFT_DECODER_NO_CONFIG", "hvcC has no VPS/SPS/PPS; cannot start the software decoder");
|
|
3097
|
+
}
|
|
3098
|
+
async init(e) {
|
|
3099
|
+
let t = await e(), n = t?.default ?? t?.initialize ?? t;
|
|
3100
|
+
if (typeof n != "function") throw new i("SOFT_DECODER_MISSING", "libde265 module does not export an initializer");
|
|
3101
|
+
this.module = await n(), this.decoder = new this.module.Decoder();
|
|
3102
|
+
}
|
|
3103
|
+
decode(e, t, n) {
|
|
3104
|
+
if (!this.decoder) return [];
|
|
3105
|
+
let r = BigInt(Math.round(t));
|
|
3106
|
+
if (n) for (let e of this.parameterSets) this.decoder.pushNal(e, r);
|
|
3107
|
+
for (let t of A(e)) t.length > 0 && this.decoder.pushNal(t, r);
|
|
3108
|
+
return this.decoder.pushEndOfFrame(), this.drain();
|
|
3109
|
+
}
|
|
3110
|
+
flush() {
|
|
3111
|
+
if (!this.decoder) return [];
|
|
3112
|
+
try {
|
|
3113
|
+
this.decoder.flushData();
|
|
3114
|
+
} catch {}
|
|
3115
|
+
return this.drain();
|
|
3116
|
+
}
|
|
3117
|
+
drain() {
|
|
3118
|
+
let e = [];
|
|
3119
|
+
for (let t = 0; t < 16; t++) {
|
|
3120
|
+
let t = this.decoder.decode();
|
|
3121
|
+
if (t.error === this.module.Error.ERROR_WAITING_FOR_INPUT_DATA || !this.module.isOk(t.error)) break;
|
|
3122
|
+
let n = this.decoder.getNextPicture();
|
|
3123
|
+
if (n) {
|
|
3124
|
+
let t = n.getWidth(0), r = n.getHeight(0), i = n.getImagePlane(0), a = n.getImagePlane(1), o = n.getImagePlane(2);
|
|
3125
|
+
e.push({
|
|
3126
|
+
ptsUs: Number(n.pts),
|
|
3127
|
+
width: t,
|
|
3128
|
+
height: r,
|
|
3129
|
+
crop: {
|
|
3130
|
+
left: 0,
|
|
3131
|
+
top: 0,
|
|
3132
|
+
width: t,
|
|
3133
|
+
height: r
|
|
3134
|
+
},
|
|
3135
|
+
y: Dt(i),
|
|
3136
|
+
u: Dt(a),
|
|
3137
|
+
v: Dt(o)
|
|
3138
|
+
}), n.delete();
|
|
3139
|
+
}
|
|
3140
|
+
if (!t.more) break;
|
|
3141
|
+
}
|
|
3142
|
+
return e;
|
|
3143
|
+
}
|
|
3144
|
+
close() {
|
|
3145
|
+
try {
|
|
3146
|
+
this.decoder?.delete();
|
|
3147
|
+
} catch {}
|
|
3148
|
+
this.decoder = null, this.module = null;
|
|
3149
|
+
}
|
|
3150
|
+
};
|
|
3151
|
+
// src/soft/h264bsd.ts
|
|
3152
|
+
function kt(e) {
|
|
3153
|
+
return _(Uint8Array.of(0, 0, 0, 1), e);
|
|
3154
|
+
}
|
|
3155
|
+
var At = class {
|
|
3156
|
+
constructor() {
|
|
3157
|
+
r(this, "name", "h264bsd"), r(this, "module", null), r(this, "decoder", null), r(this, "parameterSets", []), r(this, "picId", 0), r(this, "ptsById", /* @__PURE__ */ new Map());
|
|
3158
|
+
}
|
|
3159
|
+
async configure(e) {
|
|
3160
|
+
let t = M(e.codecPrivate), n = e.codecPrivate[1] ?? 0;
|
|
3161
|
+
if (n !== 66 && n !== 88) throw new i("SOFT_PROFILE_UNSUPPORTED", `h264bsd only decodes Baseline H.264 (profile ${n}); use MSE/WebCodecs or transcode on the server`);
|
|
3162
|
+
if (this.parameterSets = [...t.sps, ...t.pps].filter((e) => e.length > 0), this.parameterSets.length === 0) throw new i("SOFT_DECODER_NO_CONFIG", "avcC has no SPS/PPS; cannot start the software decoder");
|
|
3163
|
+
}
|
|
3164
|
+
async init(e) {
|
|
3165
|
+
let t = await e(), n = t?.default ?? t?.initialize ?? t;
|
|
3166
|
+
if (typeof n != "function") throw new i("SOFT_DECODER_MISSING", "h264bsd module does not export an initializer");
|
|
3167
|
+
this.module = await n(), this.decoder = new this.module.Decoder();
|
|
3168
|
+
}
|
|
3169
|
+
decode(e, t, n) {
|
|
3170
|
+
if (!this.decoder) return [];
|
|
3171
|
+
let r = [];
|
|
3172
|
+
if (n) for (let e of this.parameterSets) r.push(kt(e));
|
|
3173
|
+
r.push(e);
|
|
3174
|
+
let i = this.picId++;
|
|
3175
|
+
this.ptsById.set(i, t);
|
|
3176
|
+
let a = this.decoder.decode(_(...r), i), o = [];
|
|
3177
|
+
if (a.picture) {
|
|
3178
|
+
o.push(a.picture);
|
|
3179
|
+
for (let e = 0; e < (a.extraPictureCount ?? 0); e++) {
|
|
3180
|
+
let e = this.decoder.getNextPicture();
|
|
3181
|
+
e && o.push(e);
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
return o.map((e) => this.toFrame(e));
|
|
3185
|
+
}
|
|
3186
|
+
flush() {
|
|
3187
|
+
return [];
|
|
3188
|
+
}
|
|
3189
|
+
toFrame(e) {
|
|
3190
|
+
let t = e.info, n = t.picWidth, r = t.picHeight, i = Math.ceil(n / 2), a = Math.ceil(r / 2), o = n * r, s = i * a, c = e.bytes, l = t.cropParams ?? {
|
|
3191
|
+
cropLeftOffset: 0,
|
|
3192
|
+
cropTopOffset: 0,
|
|
3193
|
+
cropOutWidth: n,
|
|
3194
|
+
cropOutHeight: r
|
|
3195
|
+
};
|
|
3196
|
+
return {
|
|
3197
|
+
ptsUs: this.ptsById.get(e.picId) ?? 0,
|
|
3198
|
+
width: n,
|
|
3199
|
+
height: r,
|
|
3200
|
+
crop: {
|
|
3201
|
+
left: l.cropLeftOffset ?? 0,
|
|
3202
|
+
top: l.cropTopOffset ?? 0,
|
|
3203
|
+
width: l.cropOutWidth ?? n,
|
|
3204
|
+
height: l.cropOutHeight ?? r
|
|
3205
|
+
},
|
|
3206
|
+
y: {
|
|
3207
|
+
bytes: c.subarray(0, o),
|
|
3208
|
+
width: n,
|
|
3209
|
+
height: r,
|
|
3210
|
+
stride: n
|
|
3211
|
+
},
|
|
3212
|
+
u: {
|
|
3213
|
+
bytes: c.subarray(o, o + s),
|
|
3214
|
+
width: i,
|
|
3215
|
+
height: a,
|
|
3216
|
+
stride: i
|
|
3217
|
+
},
|
|
3218
|
+
v: {
|
|
3219
|
+
bytes: c.subarray(o + s, o + s * 2),
|
|
3220
|
+
width: i,
|
|
3221
|
+
height: a,
|
|
3222
|
+
stride: i
|
|
3223
|
+
}
|
|
3224
|
+
};
|
|
3225
|
+
}
|
|
3226
|
+
close() {
|
|
3227
|
+
try {
|
|
3228
|
+
this.decoder?.delete();
|
|
3229
|
+
} catch {}
|
|
3230
|
+
this.decoder = null, this.module = null, this.ptsById.clear();
|
|
3231
|
+
}
|
|
3232
|
+
}, jt = () => import(
|
|
3233
|
+
/* @vite-ignore */
|
|
3234
|
+
"@yume-chan/libde265"
|
|
3235
|
+
), Mt = () => import(
|
|
3236
|
+
/* @vite-ignore */
|
|
3237
|
+
"@yume-chan/h264bsd"
|
|
3238
|
+
);
|
|
3239
|
+
async function Nt(e, t = {}) {
|
|
3240
|
+
let n = e.codec === "hevc", r = n ? new Ot() : new At();
|
|
3241
|
+
await r.configure(e);
|
|
3242
|
+
let a = n ? t.hevc ?? jt : t.avc ?? Mt;
|
|
3243
|
+
try {
|
|
3244
|
+
await r.init(a);
|
|
3245
|
+
} catch (e) {
|
|
3246
|
+
throw e instanceof i ? e : new i("SOFT_DECODER_MISSING", `failed to load the WASM ${n ? "HEVC" : "H.264"} decoder. Install @yume-chan/${n ? "libde265" : "h264bsd"} or provide softDecode.loaders.`, e);
|
|
3247
|
+
}
|
|
3248
|
+
return r;
|
|
3249
|
+
}
|
|
3250
|
+
// src/render/software.ts
|
|
3251
|
+
var Pt = "\nattribute vec2 aPos;\nuniform vec4 uTexRange;\nvarying vec2 vTex;\nvoid main() {\n vTex = mix(uTexRange.xy, uTexRange.zw, aPos * 0.5 + 0.5);\n gl_Position = vec4(aPos, 0.0, 1.0);\n}", Ft = "\nprecision mediump float;\nvarying vec2 vTex;\nuniform sampler2D uY;\nuniform sampler2D uU;\nuniform sampler2D uV;\nvoid main() {\n vec2 uv = vec2(vTex.x, 1.0 - vTex.y);\n float y = texture2D(uY, uv).r;\n float u = texture2D(uU, uv).r - 0.5;\n float v = texture2D(uV, uv).r - 0.5;\n y = 1.164 * (y - 0.0625);\n gl_FragColor = vec4(y + 1.596 * v, y - 0.813 * v - 0.391 * u, y + 2.018 * u, 1.0);\n}", $ = class {
|
|
3252
|
+
constructor(e, t) {
|
|
3253
|
+
r(this, "options", void 0), r(this, "mode", "software"), r(this, "decoder", null), r(this, "videoTrack", null), r(this, "audio", void 0), r(this, "canvas", void 0), r(this, "backlog", []), r(this, "present", []), r(this, "decoding", !1), r(this, "rafId", 0), r(this, "timer", null), r(this, "paused", !1), r(this, "anchored", !1), r(this, "anchorMedia", 0), r(this, "anchorWall", 0), r(this, "decoded", 0), r(this, "dropped", 0), r(this, "firstFrameReported", !1), r(this, "destroyed", !1), r(this, "ended", !1), r(this, "gl", null), r(this, "glFailed", !1), r(this, "ctx2d", null), r(this, "tmpCanvas", null), r(this, "tmpCtx", null), r(this, "imageData", null), r(this, "resizeObserver", null), r(this, "decodeStep", () => {
|
|
3254
|
+
if (this.timer = null, this.decoding = !1, this.destroyed || this.paused || !this.decoder || this.present.length >= this.options.maxDecodeAhead) return;
|
|
3255
|
+
let e = this.backlog.shift();
|
|
3256
|
+
if (!e) return;
|
|
3257
|
+
let t = [];
|
|
3258
|
+
try {
|
|
3259
|
+
t = this.decoder.decode(e.annexB, e.ptsUs, e.keyframe);
|
|
3260
|
+
} catch (e) {
|
|
3261
|
+
this.options.onError?.(e instanceof Error ? e : Error(String(e)));
|
|
3262
|
+
}
|
|
3263
|
+
for (let e of t) this.decoded++, this.present.push(e);
|
|
3264
|
+
this.present.length > 1 && this.present.sort((e, t) => e.ptsUs - t.ptsUs), !this.anchored && this.present.length > 0 && (this.anchored = !0, this.anchorMedia = this.present[0].ptsUs / 1e6, this.anchorWall = performance.now()), this.present.length < this.options.maxDecodeAhead && this.scheduleDecode(), this.startRaf();
|
|
3265
|
+
}), r(this, "tick", () => {
|
|
3266
|
+
if (this.rafId = 0, this.paused || this.destroyed) return;
|
|
3267
|
+
if (!this.anchored) {
|
|
3268
|
+
this.startRaf();
|
|
3269
|
+
return;
|
|
3270
|
+
}
|
|
3271
|
+
let e = this.mediaTime(), t = null;
|
|
3272
|
+
for (; this.present.length > 0 && this.present[0].ptsUs / 1e6 <= e + .005;) t && this.options.dropLateFrames && this.dropped++, t = this.present.shift();
|
|
3273
|
+
t && this.render(t), this.scheduleDecode(), (this.present.length > 0 || this.backlog.length > 0) && (this.rafId = requestAnimationFrame(this.tick));
|
|
3274
|
+
}), this.options = e, this.canvas = t, this.audio = new Tt({
|
|
3275
|
+
volume: e.volume,
|
|
3276
|
+
onError: e.onError
|
|
3277
|
+
});
|
|
3278
|
+
}
|
|
3279
|
+
get stats() {
|
|
3280
|
+
return {
|
|
3281
|
+
decodedVideoFrames: this.decoded,
|
|
3282
|
+
droppedVideoFrames: this.dropped,
|
|
3283
|
+
decodedAudioFrames: this.audio.decodedFrames
|
|
3284
|
+
};
|
|
3285
|
+
}
|
|
3286
|
+
get currentTime() {
|
|
3287
|
+
return this.anchored ? this.mediaTime() : 0;
|
|
3288
|
+
}
|
|
3289
|
+
get framesPending() {
|
|
3290
|
+
return this.present.length + this.backlog.length + +!!this.decoding;
|
|
3291
|
+
}
|
|
3292
|
+
get backlogSeconds() {
|
|
3293
|
+
let e = this.backlog.length > 0 ? this.backlog[this.backlog.length - 1].ptsUs : this.present.length > 0 ? this.present[this.present.length - 1].ptsUs : 0;
|
|
3294
|
+
return e > 0 ? Math.max(0, e / 1e6 - this.mediaTime()) : 0;
|
|
3295
|
+
}
|
|
3296
|
+
get decoderName() {
|
|
3297
|
+
return this.decoder?.name ?? "none";
|
|
3298
|
+
}
|
|
3299
|
+
async attach(e) {
|
|
3300
|
+
let t = e.find((e) => e.kind === "video");
|
|
3301
|
+
t && !this.decoder && (this.decoder = await Nt(t, this.options.loaders), this.videoTrack = t, this.resize(), this.observeResize());
|
|
3302
|
+
let n = e.find((e) => e.kind === "audio");
|
|
3303
|
+
n && !this.audio.configured && this.audio.configure(n);
|
|
3304
|
+
}
|
|
3305
|
+
write(e, t) {
|
|
3306
|
+
if (!(this.destroyed || t.length === 0)) {
|
|
3307
|
+
if (e.kind === "video") {
|
|
3308
|
+
if (!this.decoder) return;
|
|
3309
|
+
for (let n of t) this.backlog.push({
|
|
3310
|
+
annexB: fe(n.data),
|
|
3311
|
+
ptsUs: Math.round(n.pts * 1e6 / e.timescale),
|
|
3312
|
+
keyframe: n.keyframe
|
|
3313
|
+
});
|
|
3314
|
+
this.scheduleDecode();
|
|
3315
|
+
} else this.audio.write(t);
|
|
3316
|
+
}
|
|
3317
|
+
}
|
|
3318
|
+
scheduleDecode() {
|
|
3319
|
+
this.decoding || this.destroyed || this.paused || !this.decoder || this.present.length >= this.options.maxDecodeAhead || this.backlog.length !== 0 && (this.decoding = !0, this.timer = setTimeout(this.decodeStep, 0));
|
|
3320
|
+
}
|
|
3321
|
+
mediaTime() {
|
|
3322
|
+
let e = this.audio.clock;
|
|
3323
|
+
return e === null ? this.anchorMedia + (performance.now() - this.anchorWall) / 1e3 : e;
|
|
3324
|
+
}
|
|
3325
|
+
startRaf() {
|
|
3326
|
+
this.rafId !== 0 || this.paused || this.destroyed || (this.rafId = requestAnimationFrame(this.tick));
|
|
3327
|
+
}
|
|
3328
|
+
resize() {
|
|
3329
|
+
let e = this.canvas.parentElement, t = e?.clientWidth || this.videoTrack?.width || 640, n = e?.clientHeight || this.videoTrack?.height || 360, r = Math.min(2, typeof window < "u" && window.devicePixelRatio || 1), i = Math.max(1, Math.round(t * r)), a = Math.max(1, Math.round(n * r));
|
|
3330
|
+
(this.canvas.width !== i || this.canvas.height !== a) && (this.canvas.width = i, this.canvas.height = a);
|
|
3331
|
+
}
|
|
3332
|
+
observeResize() {
|
|
3333
|
+
if (this.resizeObserver || typeof ResizeObserver > "u") return;
|
|
3334
|
+
let e = this.canvas.parentElement;
|
|
3335
|
+
e && (this.resizeObserver = new ResizeObserver(() => this.resize()), this.resizeObserver.observe(e));
|
|
3336
|
+
}
|
|
3337
|
+
render(e) {
|
|
3338
|
+
this.firstFrameReported || (this.firstFrameReported = !0, this.options.onFirstFrame?.(e.crop.width, e.crop.height)), this.resize(), !this.gl && !this.glFailed && this.initGl(), this.gl ? this.renderGl(e) : this.render2d(e);
|
|
3339
|
+
}
|
|
3340
|
+
initGl() {
|
|
3341
|
+
try {
|
|
3342
|
+
let e = this.canvas.getContext("webgl", {
|
|
3343
|
+
alpha: !1,
|
|
3344
|
+
antialias: !1,
|
|
3345
|
+
depth: !1,
|
|
3346
|
+
stencil: !1,
|
|
3347
|
+
preserveDrawingBuffer: !1,
|
|
3348
|
+
desynchronized: !0
|
|
3349
|
+
});
|
|
3350
|
+
if (!e) {
|
|
3351
|
+
this.glFailed = !0;
|
|
3352
|
+
return;
|
|
3353
|
+
}
|
|
3354
|
+
let t = (t, n) => {
|
|
3355
|
+
let r = e.createShader(t);
|
|
3356
|
+
if (e.shaderSource(r, n), e.compileShader(r), !e.getShaderParameter(r, e.COMPILE_STATUS)) throw Error(`shader compile failed: ${e.getShaderInfoLog(r)}`);
|
|
3357
|
+
return r;
|
|
3358
|
+
}, n = e.createProgram();
|
|
3359
|
+
if (e.attachShader(n, t(e.VERTEX_SHADER, Pt)), e.attachShader(n, t(e.FRAGMENT_SHADER, Ft)), e.linkProgram(n), !e.getProgramParameter(n, e.LINK_STATUS)) throw Error(`program link failed: ${e.getProgramInfoLog(n)}`);
|
|
3360
|
+
e.useProgram(n);
|
|
3361
|
+
let r = e.getUniformLocation(n, "uTexRange"), i = e.getUniformLocation(n, "uY"), a = e.getUniformLocation(n, "uU"), o = e.getUniformLocation(n, "uV");
|
|
3362
|
+
if (!r || !i || !a || !o) throw Error("missing uniforms");
|
|
3363
|
+
let s = e.createBuffer();
|
|
3364
|
+
e.bindBuffer(e.ARRAY_BUFFER, s), e.bufferData(e.ARRAY_BUFFER, new Float32Array([
|
|
3365
|
+
-1,
|
|
3366
|
+
-1,
|
|
3367
|
+
1,
|
|
3368
|
+
-1,
|
|
3369
|
+
-1,
|
|
3370
|
+
1,
|
|
3371
|
+
1,
|
|
3372
|
+
1
|
|
3373
|
+
]), e.STATIC_DRAW);
|
|
3374
|
+
let c = e.getAttribLocation(n, "aPos");
|
|
3375
|
+
e.enableVertexAttribArray(c), e.vertexAttribPointer(c, 2, e.FLOAT, !1, 0, 0);
|
|
3376
|
+
let l = (t) => {
|
|
3377
|
+
let n = e.createTexture();
|
|
3378
|
+
return e.activeTexture(e.TEXTURE0 + t), e.bindTexture(e.TEXTURE_2D, n), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), n;
|
|
3379
|
+
};
|
|
3380
|
+
this.gl = {
|
|
3381
|
+
gl: e,
|
|
3382
|
+
program: n,
|
|
3383
|
+
quad: s,
|
|
3384
|
+
textures: [
|
|
3385
|
+
l(0),
|
|
3386
|
+
l(1),
|
|
3387
|
+
l(2)
|
|
3388
|
+
],
|
|
3389
|
+
uniforms: {
|
|
3390
|
+
texRange: r,
|
|
3391
|
+
y: i,
|
|
3392
|
+
u: a,
|
|
3393
|
+
v: o
|
|
3394
|
+
}
|
|
3395
|
+
}, e.uniform1i(i, 0), e.uniform1i(a, 1), e.uniform1i(o, 2);
|
|
3396
|
+
} catch {
|
|
3397
|
+
this.gl = null, this.glFailed = !0;
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3400
|
+
uploadPlane(e, t, n) {
|
|
3401
|
+
let r = this.gl.gl;
|
|
3402
|
+
r.activeTexture(r.TEXTURE0 + t), r.bindTexture(r.TEXTURE_2D, e), r.pixelStorei(r.UNPACK_ALIGNMENT, 1);
|
|
3403
|
+
let i = n.bytes;
|
|
3404
|
+
if (n.stride !== n.width) {
|
|
3405
|
+
let e = new Uint8Array(n.width * n.height);
|
|
3406
|
+
for (let t = 0; t < n.height; t++) e.set(n.bytes.subarray(t * n.stride, t * n.stride + n.width), t * n.width);
|
|
3407
|
+
i = e;
|
|
3408
|
+
}
|
|
3409
|
+
r.texImage2D(r.TEXTURE_2D, 0, r.LUMINANCE, n.width, n.height, 0, r.LUMINANCE, r.UNSIGNED_BYTE, i);
|
|
3410
|
+
}
|
|
3411
|
+
renderGl(e) {
|
|
3412
|
+
let t = this.gl, n = t.gl, r = this.canvas, i = r.width, a = r.height;
|
|
3413
|
+
n.viewport(0, 0, i, a), n.clearColor(0, 0, 0, 1), n.clear(n.COLOR_BUFFER_BIT);
|
|
3414
|
+
let o = e.crop.width, s = e.crop.height, c = i, l = a;
|
|
3415
|
+
if (this.options.fit === "contain") {
|
|
3416
|
+
let e = Math.min(i / o, a / s);
|
|
3417
|
+
c = o * e, l = s * e;
|
|
3418
|
+
} else if (this.options.fit === "cover") {
|
|
3419
|
+
let e = Math.max(i / o, a / s);
|
|
3420
|
+
c = o * e, l = s * e;
|
|
3421
|
+
}
|
|
3422
|
+
let u = (i - c) / 2, d = (a - l) / 2;
|
|
3423
|
+
n.viewport(Math.round(u), Math.round(d), Math.round(c), Math.round(l)), n.useProgram(t.program), n.uniform4f(t.uniforms.texRange, e.crop.left / e.width, e.crop.top / e.height, (e.crop.left + e.crop.width) / e.width, (e.crop.top + e.crop.height) / e.height), this.uploadPlane(t.textures[0], 0, e.y), this.uploadPlane(t.textures[1], 1, e.u), this.uploadPlane(t.textures[2], 2, e.v), n.bindBuffer(n.ARRAY_BUFFER, t.quad), n.drawArrays(n.TRIANGLE_STRIP, 0, 4);
|
|
3424
|
+
}
|
|
3425
|
+
render2d(e) {
|
|
3426
|
+
if (!this.ctx2d && (this.ctx2d = this.canvas.getContext("2d"), !this.ctx2d)) return;
|
|
3427
|
+
let t = e.crop, n = t.width, r = t.height;
|
|
3428
|
+
if (this.tmpCanvas && this.tmpCanvas.width === n && this.tmpCanvas.height === r || (this.tmpCanvas = document.createElement("canvas"), this.tmpCanvas.width = n, this.tmpCanvas.height = r, this.tmpCtx = this.tmpCanvas.getContext("2d"), this.imageData = this.tmpCtx?.createImageData(n, r) ?? null), !this.tmpCtx || !this.imageData) return;
|
|
3429
|
+
let i = this.imageData.data, { y: a, u: o, v: s } = e, c = 0;
|
|
3430
|
+
for (let e = 0; e < r; e++) {
|
|
3431
|
+
let r = (t.top + e) * a.stride + t.left, l = (t.top + e >> 1) * o.stride + (t.left >> 1);
|
|
3432
|
+
for (let e = 0; e < n; e++) {
|
|
3433
|
+
let t = a.bytes[r + e] - 16, n = o.bytes[l + (e >> 1)] - 128, u = s.bytes[l + (e >> 1)] - 128, d = 1.164 * t;
|
|
3434
|
+
i[c++] = Math.max(0, Math.min(255, d + 1.596 * u)), i[c++] = Math.max(0, Math.min(255, d - .813 * u - .391 * n)), i[c++] = Math.max(0, Math.min(255, d + 2.018 * n)), i[c++] = 255;
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
this.tmpCtx.putImageData(this.imageData, 0, 0);
|
|
3438
|
+
let l = this.ctx2d, u = this.canvas.width, d = this.canvas.height;
|
|
3439
|
+
l.fillStyle = "#000", l.fillRect(0, 0, u, d);
|
|
3440
|
+
let f = u, p = d;
|
|
3441
|
+
if (this.options.fit === "contain") {
|
|
3442
|
+
let e = Math.min(u / n, d / r);
|
|
3443
|
+
f = n * e, p = r * e;
|
|
3444
|
+
} else if (this.options.fit === "cover") {
|
|
3445
|
+
let e = Math.max(u / n, d / r);
|
|
3446
|
+
f = n * e, p = r * e;
|
|
3447
|
+
}
|
|
3448
|
+
l.drawImage(this.tmpCanvas, (u - f) / 2, (d - p) / 2, f, p);
|
|
3449
|
+
}
|
|
3450
|
+
setPaused(e) {
|
|
3451
|
+
e !== this.paused && (this.paused = e, this.audio.setPaused(e), e ? (this.rafId && cancelAnimationFrame(this.rafId), this.rafId = 0) : (this.anchored = !1, this.present.length > 0 && (this.anchored = !0, this.anchorMedia = this.present[0].ptsUs / 1e6, this.anchorWall = performance.now()), this.scheduleDecode(), this.startRaf()));
|
|
3452
|
+
}
|
|
3453
|
+
resumeAudio() {
|
|
3454
|
+
return this.audio.resume();
|
|
3455
|
+
}
|
|
3456
|
+
setVolume(e) {
|
|
3457
|
+
this.audio.setVolume(e);
|
|
3458
|
+
}
|
|
3459
|
+
flush() {}
|
|
3460
|
+
endOfStream() {
|
|
3461
|
+
if (!this.ended && this.decoder) {
|
|
3462
|
+
this.ended = !0;
|
|
3463
|
+
try {
|
|
3464
|
+
for (let e of this.decoder.flush()) this.decoded++, this.present.push(e);
|
|
3465
|
+
} catch {}
|
|
3466
|
+
this.startRaf();
|
|
3467
|
+
}
|
|
3468
|
+
}
|
|
3469
|
+
reset() {
|
|
3470
|
+
this.backlog = [], this.present = [], this.anchored = !1, this.ended = !1, this.audio.reset();
|
|
3471
|
+
}
|
|
3472
|
+
destroy() {
|
|
3473
|
+
this.destroyed = !0, this.timer !== null && clearTimeout(this.timer), this.rafId && cancelAnimationFrame(this.rafId), this.resizeObserver?.disconnect(), this.resizeObserver = null;
|
|
3474
|
+
try {
|
|
3475
|
+
this.decoder?.close();
|
|
3476
|
+
} catch {}
|
|
3477
|
+
if (this.decoder = null, this.audio.destroy(), this.gl) {
|
|
3478
|
+
let { gl: e } = this.gl;
|
|
3479
|
+
for (let t of this.gl.textures) e.deleteTexture(t);
|
|
3480
|
+
e.deleteBuffer(this.gl.quad), e.deleteProgram(this.gl.program), this.gl = null;
|
|
3481
|
+
}
|
|
3482
|
+
}
|
|
3483
|
+
}, It = "0.1.0", Lt = {
|
|
3484
|
+
bytesReceived: 0,
|
|
3485
|
+
kbps: 0,
|
|
3486
|
+
protocol: "flv",
|
|
3487
|
+
decoder: "none",
|
|
3488
|
+
hardwareAcceleration: "unknown",
|
|
3489
|
+
fps: 0,
|
|
3490
|
+
decodedVideoFrames: 0,
|
|
3491
|
+
droppedVideoFrames: 0,
|
|
3492
|
+
decodedAudioFrames: 0,
|
|
3493
|
+
bufferAhead: 0,
|
|
3494
|
+
latency: 0,
|
|
3495
|
+
live: !1,
|
|
3496
|
+
droppedAudioFrames: 0
|
|
3497
|
+
};
|
|
3498
|
+
function Rt(e) {
|
|
3499
|
+
let t = typeof e == "string" ? document.querySelector(e) : e;
|
|
3500
|
+
if (!t) throw new i("ELEMENT_NOT_FOUND", `element not found: ${String(e)}`);
|
|
3501
|
+
let n = t instanceof HTMLVideoElement ? t : null, r = t instanceof HTMLCanvasElement ? t : null, a = n || r ? t.parentElement ?? t : t, o = [];
|
|
3502
|
+
getComputedStyle(a).position === "static" && (a.style.position = "relative");
|
|
3503
|
+
let s = n ?? document.createElement("video");
|
|
3504
|
+
n || (s.style.cssText = "display:block;width:100%;height:100%;background:#000;object-fit:contain", s.setAttribute("playsinline", ""), s.preload = "auto", a.appendChild(s), o.push(s)), s.playsInline = !0, s.style.objectFit || (s.style.objectFit = "contain");
|
|
3505
|
+
let c = r ?? document.createElement("canvas");
|
|
3506
|
+
return r || (c.style.cssText = "position:absolute;inset:0;width:100%;height:100%;display:none;background:#000", a.appendChild(c), o.push(c)), c.style.position = "absolute", c.style.inset = "0", {
|
|
3507
|
+
container: a,
|
|
3508
|
+
video: s,
|
|
3509
|
+
canvas: c,
|
|
3510
|
+
created: o
|
|
3511
|
+
};
|
|
3512
|
+
}
|
|
3513
|
+
function zt(e, t, n = .25) {
|
|
3514
|
+
for (let r = 0; r < e.length; r++) if (t >= e.start(r) - n && t <= e.end(r) + n) return !0;
|
|
3515
|
+
return !1;
|
|
3516
|
+
}
|
|
3517
|
+
var Bt = class {
|
|
3518
|
+
static isSupported() {
|
|
3519
|
+
return typeof window > "u" ? !1 : typeof MediaSource < "u" || Et();
|
|
3520
|
+
}
|
|
3521
|
+
constructor(e, t = {}) {
|
|
3522
|
+
r(this, "options", void 0), r(this, "logger", void 0), r(this, "emitter", new c()), r(this, "stage", void 0), r(this, "video", void 0), r(this, "canvas", void 0), r(this, "tracks", /* @__PURE__ */ new Map()), r(this, "timeline", void 0), r(this, "segmenter", void 0), r(this, "source", null), r(this, "renderer", null), r(this, "rendererPromise", null), r(this, "lastRendererError", null), r(this, "url", ""), r(this, "protocol", "flv"), r(this, "live", !1), r(this, "startMs", 0), r(this, "nativeMode", !1), r(this, "loadId", 0), r(this, "destroyed", !1), r(this, "preBuffer", []), r(this, "readyDeferred", null), r(this, "switchAttempts", /* @__PURE__ */ new Set()), r(this, "pendingSeek", null), r(this, "statsTimer", 0), r(this, "lastStats", Lt), r(this, "bytes", 0), r(this, "lastBytes", 0), r(this, "lastStatsAt", 0), r(this, "lastFrames", 0), r(this, "lastFpsAt", 0), r(this, "lastWidth", 0), r(this, "lastHeight", 0), r(this, "audioOnlyTimer", 0), r(this, "wcPaused", !1), r(this, "sourcePaused", !1), this.options = o(t), this.logger = new u(this.options.logLevel, this.options.logger, (e, t) => this.emitter.emit("log", {
|
|
3523
|
+
level: e,
|
|
3524
|
+
message: t
|
|
3525
|
+
})), this.stage = Rt(e), this.video = this.stage.video, this.canvas = this.stage.canvas, this.video.muted = this.options.muted, this.video.loop = this.options.loop, this.video.volume = this.options.volume, this.video.playsInline = !0, this.timeline = new m(this.tracks), this.segmenter = new h({
|
|
3526
|
+
durationMs: this.options.segmentDuration,
|
|
3527
|
+
onSegment: (e, t) => this.renderer?.write(e, t),
|
|
3528
|
+
getTrack: (e) => this.tracks.get(e)
|
|
3529
|
+
}), this.bindVideoEvents();
|
|
3530
|
+
}
|
|
3531
|
+
on(e, t) {
|
|
3532
|
+
return this.emitter.on(e, t);
|
|
3533
|
+
}
|
|
3534
|
+
once(e, t) {
|
|
3535
|
+
return this.emitter.once(e, t);
|
|
3536
|
+
}
|
|
3537
|
+
off(e, t) {
|
|
3538
|
+
this.emitter.off(e, t);
|
|
3539
|
+
}
|
|
3540
|
+
get videoElement() {
|
|
3541
|
+
return this.video;
|
|
3542
|
+
}
|
|
3543
|
+
get canvasElement() {
|
|
3544
|
+
return this.canvas;
|
|
3545
|
+
}
|
|
3546
|
+
get currentProtocol() {
|
|
3547
|
+
return this.protocol;
|
|
3548
|
+
}
|
|
3549
|
+
get mediaTracks() {
|
|
3550
|
+
return [...this.tracks.values()];
|
|
3551
|
+
}
|
|
3552
|
+
get isLive() {
|
|
3553
|
+
return this.live;
|
|
3554
|
+
}
|
|
3555
|
+
get decoderMode() {
|
|
3556
|
+
return this.renderer?.mode ?? (this.nativeMode ? "native" : "none");
|
|
3557
|
+
}
|
|
3558
|
+
get stats() {
|
|
3559
|
+
return this.lastStats;
|
|
3560
|
+
}
|
|
3561
|
+
get currentTime() {
|
|
3562
|
+
return this.nativeMode || this.renderer?.mode === "mse" ? (Number.isFinite(this.video.currentTime) ? this.video.currentTime : 0) * 1e3 : this.renderer instanceof Q || this.renderer instanceof $ ? this.renderer.currentTime * 1e3 : 0;
|
|
3563
|
+
}
|
|
3564
|
+
get duration() {
|
|
3565
|
+
if (this.nativeMode || this.renderer?.mode === "mse") {
|
|
3566
|
+
let e = this.video.duration;
|
|
3567
|
+
if (Number.isFinite(e)) return e * 1e3;
|
|
3568
|
+
}
|
|
3569
|
+
return !this.live && this.source && typeof this.source.duration == "number" ? this.source.duration * 1e3 : Infinity;
|
|
3570
|
+
}
|
|
3571
|
+
get paused() {
|
|
3572
|
+
return this.nativeMode || this.renderer?.mode === "mse" ? this.video.paused : this.renderer?.mode === "webcodecs" || this.renderer?.mode === "software" ? this.wcPaused : !0;
|
|
3573
|
+
}
|
|
3574
|
+
get volume() {
|
|
3575
|
+
return this.video.volume;
|
|
3576
|
+
}
|
|
3577
|
+
setVolume(e) {
|
|
3578
|
+
let t = Math.min(1, Math.max(0, e));
|
|
3579
|
+
this.video.volume = t, this.renderer?.setVolume?.(this.video.muted ? 0 : t);
|
|
3580
|
+
}
|
|
3581
|
+
setMuted(e) {
|
|
3582
|
+
this.video.muted = e, this.renderer?.setVolume?.(e ? 0 : this.video.volume), e || this.renderer?.resumeAudio?.();
|
|
3583
|
+
}
|
|
3584
|
+
setDecoderMode(e) {
|
|
3585
|
+
this.options.decoder = e ?? "auto";
|
|
3586
|
+
}
|
|
3587
|
+
setNativeFallback(e) {
|
|
3588
|
+
this.options.allowNativeFallback = e;
|
|
3589
|
+
}
|
|
3590
|
+
setSoftDecode(e) {
|
|
3591
|
+
this.options.softDecode.enabled = e;
|
|
3592
|
+
}
|
|
3593
|
+
async load(e, t = {}) {
|
|
3594
|
+
if (this.destroyed) throw new i("DESTROYED", "this player has been destroyed");
|
|
3595
|
+
let n = ++this.loadId;
|
|
3596
|
+
if (this.stopInternal(), this.url = e, this.startMs = t.startTime ?? 0, this.live = !1, this.nativeMode = !1, this.wcPaused = !1, this.sourcePaused = !1, this.pendingSeek = this.startMs > 0 ? this.startMs : null, this.switchAttempts.clear(), this.tracks.clear(), this.preBuffer = [], this.timeline.setStart(this.startMs), this.readyDeferred = d(), this.lastStats = {
|
|
3597
|
+
...Lt,
|
|
3598
|
+
protocol: this.protocol
|
|
3599
|
+
}, this.bytes = 0, this.lastBytes = 0, this.protocol = this.options.protocol === "auto" ? s(e) : this.options.protocol, this.logger.info(`loading ${this.protocol} ${e}`), this.shouldUseNative()) await this.startNative(n);
|
|
3600
|
+
else {
|
|
3601
|
+
this.source = this.createSource(this.protocol, this.startMs), this.live = this.source.live, this.lastStats.live = this.live;
|
|
3602
|
+
try {
|
|
3603
|
+
await this.source.start();
|
|
3604
|
+
} catch (e) {
|
|
3605
|
+
let t = e instanceof Error ? e : Error(String(e));
|
|
3606
|
+
this.emitter.emit("error", t), this.readyDeferred.reject(t);
|
|
3607
|
+
}
|
|
3608
|
+
}
|
|
3609
|
+
try {
|
|
3610
|
+
await p(this.readyDeferred.promise, Math.max(15e3, this.options.timeout), "first frame");
|
|
3611
|
+
} catch (e) {
|
|
3612
|
+
if (n === this.loadId) throw e;
|
|
3613
|
+
}
|
|
3614
|
+
}
|
|
3615
|
+
async play() {
|
|
3616
|
+
if (!this.destroyed) {
|
|
3617
|
+
if (this.nativeMode || this.renderer?.mode === "mse") {
|
|
3618
|
+
this.renderer?.setPaused?.(!1), this.renderer?.resumeAudio?.();
|
|
3619
|
+
try {
|
|
3620
|
+
await this.video.play();
|
|
3621
|
+
} catch (e) {
|
|
3622
|
+
this.emitter.emit("error", new i("AUTOPLAY_BLOCKED", "playback was blocked; call play() from a user gesture", e));
|
|
3623
|
+
}
|
|
3624
|
+
} else (this.renderer?.mode === "webcodecs" || this.renderer?.mode === "software") && (this.wcPaused = !1, this.renderer.setPaused?.(!1), await this.renderer.resumeAudio?.(), this.emitter.emit("play", void 0));
|
|
3625
|
+
}
|
|
3626
|
+
}
|
|
3627
|
+
pause() {
|
|
3628
|
+
this.nativeMode || this.renderer?.mode === "mse" ? (this.video.pause(), this.renderer?.setPaused?.(!0)) : (this.renderer?.mode === "webcodecs" || this.renderer?.mode === "software") && (this.wcPaused = !0, this.renderer.setPaused?.(!0), this.emitter.emit("pause", void 0));
|
|
3629
|
+
}
|
|
3630
|
+
async seek(e) {
|
|
3631
|
+
if (!(!Number.isFinite(e) || e < 0)) {
|
|
3632
|
+
if (this.nativeMode) this.video.currentTime = e / 1e3, this.emitter.emit("seeked", { time: e });
|
|
3633
|
+
else {
|
|
3634
|
+
if (this.renderer?.mode === "mse") {
|
|
3635
|
+
let t = e / 1e3;
|
|
3636
|
+
if (zt(this.video.buffered, t)) {
|
|
3637
|
+
this.video.currentTime = t, this.emitter.emit("seeked", { time: e });
|
|
3638
|
+
return;
|
|
3639
|
+
}
|
|
3640
|
+
if (!this.live && this.protocol === "hls") {
|
|
3641
|
+
this.pendingSeek = e, await this.restartAt(e);
|
|
3642
|
+
return;
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
this.emitter.emit("error", new i("SEEK_UNSUPPORTED", "this stream cannot be seeked outside the buffered range"));
|
|
3646
|
+
}
|
|
3647
|
+
}
|
|
3648
|
+
}
|
|
3649
|
+
stop() {
|
|
3650
|
+
this.loadId++, this.stopInternal(), this.readyDeferred?.reject(new i("STOPPED", "playback stopped")), this.readyDeferred = null;
|
|
3651
|
+
}
|
|
3652
|
+
destroy() {
|
|
3653
|
+
if (!this.destroyed) {
|
|
3654
|
+
this.destroyed = !0, this.stopInternal(), this.emitter.removeAll();
|
|
3655
|
+
for (let e of this.stage.created) e.remove();
|
|
3656
|
+
}
|
|
3657
|
+
}
|
|
3658
|
+
shouldUseNative() {
|
|
3659
|
+
return this.options.decoder === "native" || this.protocol === "hls" && this.options.hls.mode === "native";
|
|
3660
|
+
}
|
|
3661
|
+
createSource(e, t) {
|
|
3662
|
+
let n = this.buildSourceContext();
|
|
3663
|
+
switch (e) {
|
|
3664
|
+
case "hls": return new Ue(this.url, this.options, n, t);
|
|
3665
|
+
default: return new Me(e, this.url, this.options, n);
|
|
3666
|
+
}
|
|
3667
|
+
}
|
|
3668
|
+
onTracks(e) {
|
|
3669
|
+
let t = !1;
|
|
3670
|
+
for (let n of e) {
|
|
3671
|
+
let e = this.tracks.get(n.id);
|
|
3672
|
+
e ? e.kind === "video" && n.kind === "video" && (e.width !== n.width || e.height !== n.height || e.codec !== n.codec) && (this.tracks.set(n.id, n), t = !0) : (this.tracks.set(n.id, n), t = !0);
|
|
3673
|
+
}
|
|
3674
|
+
if (!t) return;
|
|
3675
|
+
let n = [...this.tracks.values()];
|
|
3676
|
+
this.emitMetadata(n), !this.renderer && !this.rendererPromise ? n.some((e) => e.kind === "video") ? (this.clearAudioOnlyTimer(), this.startRenderer()) : this.audioOnlyTimer || (this.audioOnlyTimer = window.setTimeout(() => {
|
|
3677
|
+
this.audioOnlyTimer = 0, !this.renderer && this.tracks.size > 0 && this.startRenderer();
|
|
3678
|
+
}, 2e3)) : this.renderer && this.renderer.attach(n);
|
|
3679
|
+
}
|
|
3680
|
+
emitMetadata(e) {
|
|
3681
|
+
let t = e.find((e) => e.kind === "video");
|
|
3682
|
+
t && (t.width !== this.lastWidth || t.height !== this.lastHeight) && (this.lastWidth = t.width, this.lastHeight = t.height, this.emitter.emit("metadata", {
|
|
3683
|
+
width: t.width,
|
|
3684
|
+
height: t.height,
|
|
3685
|
+
duration: Number.isFinite(this.duration) ? this.duration / 1e3 : void 0,
|
|
3686
|
+
live: this.live
|
|
3687
|
+
}));
|
|
3688
|
+
}
|
|
3689
|
+
async startRenderer() {
|
|
3690
|
+
if (this.rendererPromise) return;
|
|
3691
|
+
let e = this.loadId;
|
|
3692
|
+
this.rendererPromise = this.createRenderer(e).catch((e) => this.onSourceError(e instanceof Error ? e : Error(String(e)))).finally(() => {
|
|
3693
|
+
this.rendererPromise = null;
|
|
3694
|
+
}), await this.rendererPromise;
|
|
3695
|
+
}
|
|
3696
|
+
async createRenderer(e) {
|
|
3697
|
+
let t = [...this.tracks.values()];
|
|
3698
|
+
if (t.length === 0) return;
|
|
3699
|
+
let n = null, r = this.options.decoder, a = (e) => {
|
|
3700
|
+
try {
|
|
3701
|
+
return MediaSource.isTypeSupported(J(e));
|
|
3702
|
+
} catch {
|
|
3703
|
+
return !1;
|
|
3704
|
+
}
|
|
3705
|
+
}, o = t.find((e) => e.kind === "video");
|
|
3706
|
+
if (typeof MediaSource < "u" && r !== "webcodecs" && r !== "software" && r !== "native" && (o ? a(o) : t.some(a))) try {
|
|
3707
|
+
let e = new X(this.video, {
|
|
3708
|
+
live: this.live,
|
|
3709
|
+
liveBufferTarget: this.options.liveBufferTarget,
|
|
3710
|
+
onError: (e) => this.emitter.emit("error", e)
|
|
3711
|
+
});
|
|
3712
|
+
await e.attach(t), n = e, this.logger.info("decoder: MSE");
|
|
3713
|
+
} catch (e) {
|
|
3714
|
+
this.lastRendererError = e instanceof Error ? e : Error(String(e)), this.logger.warn(`MSE unavailable: ${this.lastRendererError.message}`);
|
|
3715
|
+
}
|
|
3716
|
+
if (!n && (r === "auto" || r === "webcodecs") && Et()) try {
|
|
3717
|
+
let e = new Q({
|
|
3718
|
+
hardwareAcceleration: this.options.hardwareAcceleration,
|
|
3719
|
+
fit: this.options.webcodecs.fit,
|
|
3720
|
+
volume: this.options.volume,
|
|
3721
|
+
onError: (e) => this.emitter.emit("error", e),
|
|
3722
|
+
onFirstFrame: (e, t) => this.emitter.emit("loaded", {
|
|
3723
|
+
width: e,
|
|
3724
|
+
height: t
|
|
3725
|
+
})
|
|
3726
|
+
}, this.canvas);
|
|
3727
|
+
await e.attach(t), n = e, this.video.muted = !0, this.logger.info("decoder: WebCodecs");
|
|
3728
|
+
} catch (e) {
|
|
3729
|
+
this.lastRendererError = e instanceof Error ? e : Error(String(e)), this.logger.warn(`WebCodecs unavailable: ${this.lastRendererError.message}`);
|
|
3730
|
+
}
|
|
3731
|
+
if (!n && (r === "auto" || r === "software") && this.options.softDecode.enabled && t.some((e) => e.kind === "video")) try {
|
|
3732
|
+
let e = new $({
|
|
3733
|
+
fit: this.options.webcodecs.fit,
|
|
3734
|
+
volume: this.options.volume,
|
|
3735
|
+
maxDecodeAhead: this.options.softDecode.maxDecodeAhead,
|
|
3736
|
+
dropLateFrames: this.options.softDecode.dropLateFrames,
|
|
3737
|
+
loaders: this.options.softDecode.loaders,
|
|
3738
|
+
onError: (e) => this.emitter.emit("error", e),
|
|
3739
|
+
onFirstFrame: (e, t) => this.emitter.emit("loaded", {
|
|
3740
|
+
width: e,
|
|
3741
|
+
height: t
|
|
3742
|
+
})
|
|
3743
|
+
}, this.canvas);
|
|
3744
|
+
await e.attach(t), n = e, this.video.muted = !0, this.logger.info(`decoder: software (${e.decoderName})`);
|
|
3745
|
+
} catch (e) {
|
|
3746
|
+
this.lastRendererError = e instanceof Error ? e : Error(String(e)), this.logger.warn(`software decode unavailable: ${this.lastRendererError.message}`);
|
|
3747
|
+
}
|
|
3748
|
+
if (!n) {
|
|
3749
|
+
if (this.options.allowNativeFallback && (this.protocol === "mp4" || this.protocol === "hls" || this.protocol === "native")) {
|
|
3750
|
+
this.logger.warn("falling back to native playback"), await this.startNative(e);
|
|
3751
|
+
return;
|
|
3752
|
+
}
|
|
3753
|
+
throw this.lastRendererError ?? new i("NO_DECODER", "no decoder is available for this stream");
|
|
3754
|
+
}
|
|
3755
|
+
if (e !== this.loadId) {
|
|
3756
|
+
n.destroy();
|
|
3757
|
+
return;
|
|
3758
|
+
}
|
|
3759
|
+
this.renderer = n, this.showRenderer(n.mode);
|
|
3760
|
+
let s = [...this.tracks.values()];
|
|
3761
|
+
await n.attach(s), this.emitter.emit("ready", {
|
|
3762
|
+
tracks: s,
|
|
3763
|
+
protocol: this.protocol,
|
|
3764
|
+
decoder: n.mode
|
|
3765
|
+
});
|
|
3766
|
+
let c = s.find((e) => e.kind === "video");
|
|
3767
|
+
c && this.emitter.emit("metadata", {
|
|
3768
|
+
width: c.width,
|
|
3769
|
+
height: c.height,
|
|
3770
|
+
live: this.live
|
|
3771
|
+
});
|
|
3772
|
+
let l = this.preBuffer;
|
|
3773
|
+
this.preBuffer = [];
|
|
3774
|
+
for (let e of l) this.segmenter.push(e);
|
|
3775
|
+
this.readyDeferred?.resolve(), this.startStats(), this.options.autoplay && this.safeAutoplay();
|
|
3776
|
+
}
|
|
3777
|
+
onSample(e) {
|
|
3778
|
+
if (!this.tracks.get(e.trackId)) return;
|
|
3779
|
+
let t = this.timeline.normalize(e);
|
|
3780
|
+
this.renderer ? this.segmenter.push(t) : this.preBuffer.length < 4e3 && this.preBuffer.push(t);
|
|
3781
|
+
}
|
|
3782
|
+
onSourceEnd() {
|
|
3783
|
+
if (this.segmenter.flush(), this.renderer?.endOfStream?.(), (this.renderer?.mode === "webcodecs" || this.renderer?.mode === "software") && !this.live) {
|
|
3784
|
+
let e = window.setInterval(() => {
|
|
3785
|
+
(this.renderer instanceof Q || this.renderer instanceof $ ? this.renderer.framesPending : 0) > 0 || (window.clearInterval(e), this.destroyed || this.emitter.emit("ended", void 0));
|
|
3786
|
+
}, 200);
|
|
3787
|
+
}
|
|
3788
|
+
}
|
|
3789
|
+
onSourceError(e) {
|
|
3790
|
+
if (!this.destroyed) {
|
|
3791
|
+
if (e instanceof i && e.code === "SWITCH_PROTOCOL") {
|
|
3792
|
+
let t = e.detail?.protocol;
|
|
3793
|
+
if (t && !this.switchAttempts.has(t)) {
|
|
3794
|
+
this.switchAttempts.add(t), this.logger.info(`content-type says ${t}; retrying`), this.switchProtocol(t);
|
|
3795
|
+
return;
|
|
3796
|
+
}
|
|
3797
|
+
}
|
|
3798
|
+
this.tryNativeFallback(e) || (this.logger.error(e.message), this.emitter.emit("error", e), this.readyDeferred?.reject(e));
|
|
3799
|
+
}
|
|
3800
|
+
}
|
|
3801
|
+
tryNativeFallback(e) {
|
|
3802
|
+
if (!this.options.allowNativeFallback || this.nativeMode || this.protocol !== "mp4" && this.protocol !== "hls") return !1;
|
|
3803
|
+
let t = e instanceof i ? e.code : "";
|
|
3804
|
+
return [
|
|
3805
|
+
"MP4_MOOV_AT_END",
|
|
3806
|
+
"MSE_UNSUPPORTED_CODEC",
|
|
3807
|
+
"MSE_APPEND_ERROR",
|
|
3808
|
+
"WEBCODECS_UNSUPPORTED",
|
|
3809
|
+
"WEBCODECS_CONFIG_FAILED",
|
|
3810
|
+
"SOFT_DECODER_MISSING",
|
|
3811
|
+
"SOFT_DECODER_NO_CONFIG",
|
|
3812
|
+
"SOFT_PROFILE_UNSUPPORTED",
|
|
3813
|
+
"SOFT_DECODE_FAILED",
|
|
3814
|
+
"NO_DECODER"
|
|
3815
|
+
].includes(t) ? (this.logger.warn(`decoder failed (${t}); falling back to native playback`), this.startNative(this.loadId), !0) : !1;
|
|
3816
|
+
}
|
|
3817
|
+
async switchProtocol(e) {
|
|
3818
|
+
let t = this.loadId;
|
|
3819
|
+
this.stopPipeline(), this.tracks.clear(), this.preBuffer = [], this.timeline.setStart(this.startMs), this.readyDeferred = d(), this.protocol = e, this.source = this.createSource(e, this.startMs), this.live = this.source.live;
|
|
3820
|
+
try {
|
|
3821
|
+
await this.source.start(), await this.readyDeferred.promise;
|
|
3822
|
+
} catch (e) {
|
|
3823
|
+
t === this.loadId && this.onSourceError(e instanceof Error ? e : Error(String(e)));
|
|
3824
|
+
}
|
|
3825
|
+
}
|
|
3826
|
+
async restartAt(e) {
|
|
3827
|
+
let t = this.loadId;
|
|
3828
|
+
this.stopPipeline(), this.tracks.clear(), this.preBuffer = [], this.startMs = e, this.timeline.setStart(e), this.readyDeferred = d(), this.lastWidth = 0, this.lastHeight = 0, this.source = new Ue(this.url, this.options, this.buildSourceContext(), e), this.live = this.source.live;
|
|
3829
|
+
try {
|
|
3830
|
+
await this.source.start(), await this.readyDeferred.promise, t === this.loadId && (this.pendingSeek = e);
|
|
3831
|
+
} catch (e) {
|
|
3832
|
+
this.onSourceError(e instanceof Error ? e : Error(String(e)));
|
|
3833
|
+
}
|
|
3834
|
+
}
|
|
3835
|
+
buildSourceContext() {
|
|
3836
|
+
return {
|
|
3837
|
+
onTracks: (e) => this.onTracks(e),
|
|
3838
|
+
onSample: (e) => this.onSample(e),
|
|
3839
|
+
onEnd: () => this.onSourceEnd(),
|
|
3840
|
+
onError: (e) => this.onSourceError(e),
|
|
3841
|
+
getBufferAhead: () => this.bufferAhead,
|
|
3842
|
+
getCurrentTime: () => this.currentTime / 1e3,
|
|
3843
|
+
onBytes: (e) => {
|
|
3844
|
+
this.bytes += e;
|
|
3845
|
+
}
|
|
3846
|
+
};
|
|
3847
|
+
}
|
|
3848
|
+
async startNative(e) {
|
|
3849
|
+
this.stopPipeline(), this.nativeMode = !0, this.showRenderer("native"), this.video.src = this.url, this.video.load(), await new Promise((e, t) => {
|
|
3850
|
+
let n = window.setTimeout(() => o(new i("NATIVE_TIMEOUT", "native playback did not start")), Math.max(15e3, this.options.timeout)), r = () => o(), a = () => o(new i("NATIVE_FAILED", "native playback failed")), o = (i) => {
|
|
3851
|
+
window.clearTimeout(n), this.video.removeEventListener("loadedmetadata", r), this.video.removeEventListener("error", a), i ? t(i) : e();
|
|
3852
|
+
};
|
|
3853
|
+
this.video.addEventListener("loadedmetadata", r), this.video.addEventListener("error", a);
|
|
3854
|
+
}), e === this.loadId && (this.emitter.emit("ready", {
|
|
3855
|
+
tracks: [],
|
|
3856
|
+
protocol: this.protocol,
|
|
3857
|
+
decoder: "native"
|
|
3858
|
+
}), this.emitter.emit("metadata", {
|
|
3859
|
+
width: this.video.videoWidth,
|
|
3860
|
+
height: this.video.videoHeight,
|
|
3861
|
+
duration: Number.isFinite(this.video.duration) ? this.video.duration : void 0,
|
|
3862
|
+
live: this.live
|
|
3863
|
+
}), this.readyDeferred?.resolve(), this.startStats(), this.options.autoplay && this.safeAutoplay());
|
|
3864
|
+
}
|
|
3865
|
+
safeAutoplay() {
|
|
3866
|
+
this.renderer?.mode === "webcodecs" ? this.play() : this.video.play().catch((e) => {
|
|
3867
|
+
this.logger.info(`autoplay blocked: ${String(e)}`);
|
|
3868
|
+
});
|
|
3869
|
+
}
|
|
3870
|
+
startStats() {
|
|
3871
|
+
this.statsTimer || (this.lastStatsAt = performance.now(), this.lastFpsAt = performance.now(), this.lastFrames = this.renderer?.stats.decodedVideoFrames ?? 0, this.lastStats = {
|
|
3872
|
+
...Lt,
|
|
3873
|
+
protocol: this.protocol
|
|
3874
|
+
}, this.statsTimer = window.setInterval(() => this.updateStats(), 1e3));
|
|
3875
|
+
}
|
|
3876
|
+
get bufferAhead() {
|
|
3877
|
+
if (this.nativeMode) {
|
|
3878
|
+
let e = this.video.buffered;
|
|
3879
|
+
for (let t = 0; t < e.length; t++) if (e.start(t) <= this.video.currentTime && e.end(t) >= this.video.currentTime) return e.end(t) - this.video.currentTime;
|
|
3880
|
+
return 0;
|
|
3881
|
+
}
|
|
3882
|
+
return this.renderer instanceof X ? this.renderer.bufferAhead : this.renderer instanceof Q || this.renderer instanceof $ ? this.renderer.backlogSeconds : 0;
|
|
3883
|
+
}
|
|
3884
|
+
updateStats() {
|
|
3885
|
+
if (this.destroyed) return;
|
|
3886
|
+
let e = performance.now(), t = this.renderer?.stats ?? {
|
|
3887
|
+
decodedVideoFrames: 0,
|
|
3888
|
+
droppedVideoFrames: 0,
|
|
3889
|
+
decodedAudioFrames: 0
|
|
3890
|
+
}, n = Math.max(.001, (e - this.lastFpsAt) / 1e3), r = Math.max(0, (t.decodedVideoFrames - this.lastFrames) / n);
|
|
3891
|
+
this.lastFrames = t.decodedVideoFrames, this.lastFpsAt = e;
|
|
3892
|
+
let i = Math.max(.001, (e - this.lastStatsAt) / 1e3), a = (this.bytes - this.lastBytes) * 8 / 1e3 / i;
|
|
3893
|
+
this.lastBytes = this.bytes, this.lastStatsAt = e;
|
|
3894
|
+
let o = this.bufferAhead;
|
|
3895
|
+
if (this.lastStats = {
|
|
3896
|
+
bytesReceived: this.bytes,
|
|
3897
|
+
kbps: Math.round(a),
|
|
3898
|
+
protocol: this.protocol,
|
|
3899
|
+
decoder: this.decoderMode,
|
|
3900
|
+
hardwareAcceleration: this.renderer instanceof Q ? this.renderer.acceleration : this.renderer?.mode === "software" ? "software" : this.renderer?.mode === "mse" ? "preferred" : "unknown",
|
|
3901
|
+
videoCodec: this.findCodec("video"),
|
|
3902
|
+
audioCodec: this.findCodec("audio"),
|
|
3903
|
+
width: this.lastWidth || this.video.videoWidth || void 0,
|
|
3904
|
+
height: this.lastHeight || this.video.videoHeight || void 0,
|
|
3905
|
+
fps: Math.round(r * 10) / 10,
|
|
3906
|
+
decodedVideoFrames: t.decodedVideoFrames,
|
|
3907
|
+
droppedVideoFrames: t.droppedVideoFrames,
|
|
3908
|
+
decodedAudioFrames: t.decodedAudioFrames,
|
|
3909
|
+
bufferAhead: o,
|
|
3910
|
+
latency: this.live ? o : 0,
|
|
3911
|
+
live: this.live,
|
|
3912
|
+
droppedAudioFrames: 0
|
|
3913
|
+
}, this.emitter.emit("stats", this.lastStats), this.renderer instanceof X && !this.live && this.renderer.trimOld(60), (this.renderer instanceof Q || this.renderer instanceof $) && !this.live) {
|
|
3914
|
+
let e = this.renderer.backlogSeconds;
|
|
3915
|
+
e > 10 && !this.sourcePaused ? (this.source?.pause?.(), this.sourcePaused = !0) : e < 4 && this.sourcePaused && (this.source?.resume?.(), this.sourcePaused = !1);
|
|
3916
|
+
}
|
|
3917
|
+
if (this.live && this.options.catchUp && this.renderer?.mode === "mse") {
|
|
3918
|
+
let e = this.options.liveBufferTarget;
|
|
3919
|
+
o > e * 2 ? this.video.playbackRate = 1.06 : o < e && (this.video.playbackRate = 1);
|
|
3920
|
+
}
|
|
3921
|
+
if (this.pendingSeek !== null && !this.nativeMode) {
|
|
3922
|
+
let e = this.pendingSeek / 1e3;
|
|
3923
|
+
this.renderer?.mode === "mse" && zt(this.video.buffered, e, .5) && (this.video.currentTime = e, this.emitter.emit("seeked", { time: this.pendingSeek }), this.pendingSeek = null);
|
|
3924
|
+
}
|
|
3925
|
+
}
|
|
3926
|
+
findCodec(e) {
|
|
3927
|
+
for (let t of this.tracks.values()) if (t.kind === e) return t.kind === "video" ? t.codec === "hevc" ? "HEVC" : "H.264" : t.codec === "aac" ? "AAC" : t.codec === "g711a" ? "G.711 A-law" : t.codec === "g711u" ? "G.711 μ-law" : t.codec.toUpperCase();
|
|
3928
|
+
}
|
|
3929
|
+
bindVideoEvents() {
|
|
3930
|
+
this.video.addEventListener("play", () => this.emitter.emit("play", void 0)), this.video.addEventListener("pause", () => this.emitter.emit("pause", void 0)), this.video.addEventListener("ended", () => {
|
|
3931
|
+
this.options.loop && !this.live ? (this.video.currentTime = 0, this.video.play()) : this.emitter.emit("ended", void 0);
|
|
3932
|
+
}), this.video.addEventListener("error", () => {
|
|
3933
|
+
this.nativeMode && this.emitter.emit("error", new i("MEDIA_ERROR", "the media element reported an error", this.video.error));
|
|
3934
|
+
}), this.video.addEventListener("loadedmetadata", () => {
|
|
3935
|
+
this.nativeMode && this.emitter.emit("metadata", {
|
|
3936
|
+
width: this.video.videoWidth,
|
|
3937
|
+
height: this.video.videoHeight,
|
|
3938
|
+
duration: Number.isFinite(this.video.duration) ? this.video.duration : void 0,
|
|
3939
|
+
live: this.live
|
|
3940
|
+
});
|
|
3941
|
+
});
|
|
3942
|
+
}
|
|
3943
|
+
showRenderer(e) {
|
|
3944
|
+
e === "webcodecs" || e === "software" ? (this.canvas.style.display = "block", this.video.style.display = "none") : (this.canvas.style.display = "none", this.video.style.display = "block");
|
|
3945
|
+
}
|
|
3946
|
+
clearAudioOnlyTimer() {
|
|
3947
|
+
this.audioOnlyTimer && (window.clearTimeout(this.audioOnlyTimer), this.audioOnlyTimer = 0);
|
|
3948
|
+
}
|
|
3949
|
+
stopPipeline() {
|
|
3950
|
+
this.sourcePaused = !1, this.source?.stop(), this.source = null, this.renderer?.destroy(), this.renderer = null, this.rendererPromise = null, this.clearAudioOnlyTimer(), this.statsTimer && (window.clearInterval(this.statsTimer), this.statsTimer = 0);
|
|
3951
|
+
try {
|
|
3952
|
+
this.video.pause(), this.video.removeAttribute("src"), this.video.load();
|
|
3953
|
+
} catch {}
|
|
3954
|
+
this.nativeMode = !1;
|
|
3955
|
+
}
|
|
3956
|
+
stopInternal() {
|
|
3957
|
+
this.stopPipeline(), this.segmenter.flush();
|
|
3958
|
+
}
|
|
3959
|
+
};
|
|
3960
|
+
r(Bt, "version", It);
|
|
3961
|
+
export { a as DEFAULTS, L as FLV_TIMESCALE, Bt as FluxKit, Bt as default, i as FluxKitError, De as FlvDemuxer, Ee as FlvTagParser, _t as Fmp4Muxer, At as H264BsdDecoder, Ue as HlsSource, ke as HttpStream, Ot as Libde265Decoder, V as Mp4Demuxer, X as MseRenderer, h as Segmenter, $ as SoftwareRenderer, Me as StreamSource, K as TS_TIMESCALE, m as Timeline, He as TsDemuxer, It as VERSION, Q as WebCodecsRenderer, je as WsStream, Ae as WsTransport, I as aacCodecString, pt as audioMimeType, ue as avcCodecString, fe as avccToAnnexB, P as buildAudioSpecificConfig, le as buildAvcC, ve as buildHvcC, dt as buildInitSegment, mt as codecLabel, Nt as createSoftwareDecoder, D as decodeAmf0, ee as decodeAmf0All, s as detectProtocol, ne as encodeAmf0, re as encodeAmf0Values, U as fetchBytes, H as fetchText, j as hevcCodecString, pe as isAvcKeyframe, ye as isHevcKeyframe, Et as isWebCodecsSupported, J as mimeTypeFor, oe as nalsToAvcc, Se as parseAdts, G as parseAttributes, F as parseAudioSpecificConfig, M as parseAvcC, ce as parseAvcSps, _e as parseHevcSps, N as parseHvcC, Pe as parseMasterPlaylist, Le as parseMediaPlaylist, Fe as parseMediaRenditions, o as resolveOptions, W as resolveUrl, Re as selectVariant, A as splitAnnexB, ft as videoMimeType };
|
|
3962
|
+
|
|
3963
|
+
//# sourceMappingURL=fluxkit.js.map
|