@moq/watch 0.2.15 → 0.2.16
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/broadcast-CG8jZRfA.js +1651 -0
- package/broadcast-CG8jZRfA.js.map +1 -0
- package/element.js +1 -1
- package/index.js +1 -1
- package/package.json +3 -3
- package/support/element.js +39 -39
- package/support/element.js.map +1 -1
- package/ui/components/button.d.ts +3 -0
- package/ui/components/button.d.ts.map +1 -0
- package/ui/components/center-play.d.ts +5 -0
- package/ui/components/center-play.d.ts.map +1 -0
- package/ui/components/control-bar.d.ts +6 -0
- package/ui/components/control-bar.d.ts.map +1 -0
- package/ui/components/fullscreen-button.d.ts +2 -1
- package/ui/components/fullscreen-button.d.ts.map +1 -1
- package/ui/components/latency.d.ts +5 -0
- package/ui/components/latency.d.ts.map +1 -0
- package/ui/components/live-badge.d.ts +9 -0
- package/ui/components/live-badge.d.ts.map +1 -0
- package/ui/components/play-pause.d.ts +1 -0
- package/ui/components/play-pause.d.ts.map +1 -1
- package/ui/components/quality.d.ts +5 -0
- package/ui/components/quality.d.ts.map +1 -0
- package/ui/components/settings-button.d.ts +5 -0
- package/ui/components/settings-button.d.ts.map +1 -0
- package/ui/components/settings-panel.d.ts +6 -0
- package/ui/components/settings-panel.d.ts.map +1 -0
- package/ui/components/volume-slider.d.ts +1 -0
- package/ui/components/volume-slider.d.ts.map +1 -1
- package/ui/element.d.ts.map +1 -1
- package/ui/element.js +577 -260
- package/ui/element.js.map +1 -1
- package/ui/format.d.ts +11 -0
- package/ui/format.d.ts.map +1 -0
- package/ui/fullscreen.d.ts +16 -0
- package/ui/fullscreen.d.ts.map +1 -0
- package/ui/graph.d.ts +18 -0
- package/ui/graph.d.ts.map +1 -0
- package/ui/icons.d.ts +5 -1
- package/ui/icons.d.ts.map +1 -1
- package/ui/state.d.ts +9 -0
- package/ui/state.d.ts.map +1 -0
- package/ui/stats.d.ts +3 -2
- package/ui/stats.d.ts.map +1 -1
- package/broadcast-jtLUfuyL.js +0 -4291
- package/broadcast-jtLUfuyL.js.map +0 -1
- package/dom-Yz3UT5ap.js +0 -29
- package/dom-Yz3UT5ap.js.map +0 -1
- package/libav-opus-af-CzuaKpJb.js +0 -252
- package/libav-opus-af-CzuaKpJb.js.map +0 -1
- package/main-bFFHfuX4.js +0 -1999
- package/main-bFFHfuX4.js.map +0 -1
- package/ui/components/quality-selector.d.ts +0 -4
- package/ui/components/quality-selector.d.ts.map +0 -1
- package/ui/components/stats-button.d.ts +0 -3
- package/ui/components/stats-button.d.ts.map +0 -1
- package/ui/components/watch-status-indicator.d.ts +0 -4
- package/ui/components/watch-status-indicator.d.ts.map +0 -1
|
@@ -0,0 +1,1651 @@
|
|
|
1
|
+
import * as e from "@moq/net";
|
|
2
|
+
import { Path as t, Time as n } from "@moq/net";
|
|
3
|
+
import { Effect as r, Signal as i } from "@moq/signals";
|
|
4
|
+
import * as a from "@moq/hang/catalog";
|
|
5
|
+
import { u53 as o } from "@moq/hang/catalog";
|
|
6
|
+
import * as s from "@moq/hang/container";
|
|
7
|
+
import * as c from "@moq/hang/util";
|
|
8
|
+
import * as l from "@moq/json";
|
|
9
|
+
import * as u from "@moq/msf";
|
|
10
|
+
//#region \0rolldown/runtime.js
|
|
11
|
+
var d = Object.defineProperty, f = (e, t) => {
|
|
12
|
+
let n = {};
|
|
13
|
+
for (var r in e) d(n, r, {
|
|
14
|
+
get: e[r],
|
|
15
|
+
enumerable: !0
|
|
16
|
+
});
|
|
17
|
+
return t || d(n, Symbol.toStringTag, { value: "Module" }), n;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/base64.ts
|
|
21
|
+
function p(e) {
|
|
22
|
+
let t = atob(e), n = new Uint8Array(t.length);
|
|
23
|
+
for (let e = 0; e < t.length; e++) n[e] = t.charCodeAt(e);
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/audio/shared-ring-buffer.ts
|
|
28
|
+
var m = 0, h = 1, g = 2, _ = 3, v = 4;
|
|
29
|
+
function y(e, t, n) {
|
|
30
|
+
if (e <= 0) throw Error("invalid channels");
|
|
31
|
+
if (t <= 0) throw Error("invalid capacity");
|
|
32
|
+
if (n <= 0) throw Error("invalid sample rate");
|
|
33
|
+
let r = new SharedArrayBuffer(e * t * Float32Array.BYTES_PER_ELEMENT), i = new SharedArrayBuffer(v * Int32Array.BYTES_PER_ELEMENT), a = new Int32Array(i);
|
|
34
|
+
return Atomics.store(a, _, 1), {
|
|
35
|
+
channels: e,
|
|
36
|
+
capacity: t,
|
|
37
|
+
rate: n,
|
|
38
|
+
samples: r,
|
|
39
|
+
control: i
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function b(e, t) {
|
|
43
|
+
return (e - t | 0) > 0 ? e : t;
|
|
44
|
+
}
|
|
45
|
+
function x(e, t) {
|
|
46
|
+
return (e % t + t) % t;
|
|
47
|
+
}
|
|
48
|
+
function S(e, t, n) {
|
|
49
|
+
for (;;) {
|
|
50
|
+
let r = Atomics.load(e, t);
|
|
51
|
+
if ((n - r | 0) <= 0) return r;
|
|
52
|
+
if (Atomics.compareExchange(e, t, r, n) === r) return n;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
var C = class e {
|
|
56
|
+
channels;
|
|
57
|
+
capacity;
|
|
58
|
+
rate;
|
|
59
|
+
init;
|
|
60
|
+
#e;
|
|
61
|
+
#t;
|
|
62
|
+
constructor(e) {
|
|
63
|
+
this.channels = e.channels, this.capacity = e.capacity, this.rate = e.rate, this.init = e, this.#e = new Int32Array(e.control), this.#t = [];
|
|
64
|
+
for (let t = 0; t < this.channels; t++) this.#t.push(new Float32Array(e.samples, t * this.capacity * Float32Array.BYTES_PER_ELEMENT, this.capacity));
|
|
65
|
+
}
|
|
66
|
+
insert(e, t) {
|
|
67
|
+
if (t.length !== this.channels) throw Error("wrong number of channels");
|
|
68
|
+
let r = Math.round(n.Second.fromMicro(e) * this.rate), i = t[0].length, a = 0, o = r + i | 0, s = Atomics.load(this.#e, h), c = s - r | 0;
|
|
69
|
+
if (c > 0) {
|
|
70
|
+
if (c >= i) return;
|
|
71
|
+
a = c, r = r + c | 0;
|
|
72
|
+
}
|
|
73
|
+
let l = i - a;
|
|
74
|
+
(o - s | 0) > this.capacity && S(this.#e, h, o - this.capacity | 0);
|
|
75
|
+
let u = Atomics.load(this.#e, m), d = r - u | 0;
|
|
76
|
+
if (d > 0) {
|
|
77
|
+
let e = Math.min(d, this.capacity);
|
|
78
|
+
for (let t = 0; t < this.channels; t++) {
|
|
79
|
+
let n = this.#t[t];
|
|
80
|
+
for (let t = 0; t < e; t++) n[x(u + t | 0, this.capacity)] = 0;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (let e = 0; e < this.channels; e++) {
|
|
84
|
+
let n = t[e], i = this.#t[e];
|
|
85
|
+
for (let e = 0; e < l; e++) i[x(r + e | 0, this.capacity)] = n[a + e];
|
|
86
|
+
}
|
|
87
|
+
Atomics.store(this.#e, m, b(Atomics.load(this.#e, m), o));
|
|
88
|
+
let f = Atomics.load(this.#e, h), p = Atomics.load(this.#e, m), v = Atomics.load(this.#e, g);
|
|
89
|
+
(p - f | 0) >= v && v > 0 && Atomics.store(this.#e, _, 0);
|
|
90
|
+
}
|
|
91
|
+
read(e) {
|
|
92
|
+
if (Atomics.load(this.#e, _) === 1) return 0;
|
|
93
|
+
let t = Atomics.load(this.#e, h), n = Atomics.load(this.#e, m), r = Atomics.load(this.#e, g), i = n - t | 0;
|
|
94
|
+
if (r > 0 && i > r) {
|
|
95
|
+
let e = n - r | 0;
|
|
96
|
+
t = S(this.#e, h, e);
|
|
97
|
+
}
|
|
98
|
+
let a = n - t | 0, o = Math.min(a, e[0].length);
|
|
99
|
+
if (o <= 0) return 0;
|
|
100
|
+
for (let n = 0; n < this.channels; n++) {
|
|
101
|
+
let r = this.#t[n], i = e[n];
|
|
102
|
+
for (let e = 0; e < o; e++) i[e] = r[x(t + e | 0, this.capacity)];
|
|
103
|
+
}
|
|
104
|
+
return S(this.#e, h, t + o | 0), o;
|
|
105
|
+
}
|
|
106
|
+
setLatency(e) {
|
|
107
|
+
Atomics.store(this.#e, g, e);
|
|
108
|
+
}
|
|
109
|
+
resize(t) {
|
|
110
|
+
let n = new e(y(this.channels, t, this.rate)), r = Atomics.load(this.#e, h), i = Atomics.load(this.#e, m), a = Atomics.load(this.#e, g), o = Atomics.load(this.#e, _), s = i - r | 0, c = Math.max(0, Math.min(s, n.capacity)), l = i - c | 0;
|
|
111
|
+
for (let e = 0; e < this.channels; e++) {
|
|
112
|
+
let t = this.#t[e], r = n.#t[e];
|
|
113
|
+
for (let e = 0; e < c; e++) {
|
|
114
|
+
let i = l + e | 0;
|
|
115
|
+
r[x(i, n.capacity)] = t[x(i, this.capacity)];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return Atomics.store(n.#e, h, l), Atomics.store(n.#e, m, i), Atomics.store(n.#e, g, a), Atomics.store(n.#e, _, o), n;
|
|
119
|
+
}
|
|
120
|
+
get timestamp() {
|
|
121
|
+
let e = Atomics.load(this.#e, h);
|
|
122
|
+
return n.Micro.fromSecond(e / this.rate);
|
|
123
|
+
}
|
|
124
|
+
get stalled() {
|
|
125
|
+
return Atomics.load(this.#e, _) === 1;
|
|
126
|
+
}
|
|
127
|
+
get length() {
|
|
128
|
+
return Atomics.load(this.#e, m) - Atomics.load(this.#e, h) | 0;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/audio/buffer.ts
|
|
133
|
+
function w() {
|
|
134
|
+
return !(typeof SharedArrayBuffer > "u" || typeof crossOriginIsolated < "u" && !crossOriginIsolated);
|
|
135
|
+
}
|
|
136
|
+
function ee(e, t, n, r) {
|
|
137
|
+
return w() ? (console.log("[audio] using SharedArrayBuffer audio buffer"), new te(e, t, n, r)) : (console.log("[audio] using postMessage audio buffer (SharedArrayBuffer unavailable)"), new ne(e, t, n, r));
|
|
138
|
+
}
|
|
139
|
+
var te = class {
|
|
140
|
+
rate;
|
|
141
|
+
channels;
|
|
142
|
+
#e;
|
|
143
|
+
#t;
|
|
144
|
+
#n = new i(0);
|
|
145
|
+
timestamp = this.#n;
|
|
146
|
+
#r = new i(!0);
|
|
147
|
+
stalled = this.#r;
|
|
148
|
+
#i = new r();
|
|
149
|
+
constructor(e, t, n, r) {
|
|
150
|
+
this.#e = e, this.channels = t, this.rate = n;
|
|
151
|
+
let i = y(t, Math.max(n, r * 2), n);
|
|
152
|
+
this.#t = new C(i), this.#t.setLatency(r);
|
|
153
|
+
let a = {
|
|
154
|
+
type: "init-shared",
|
|
155
|
+
...i
|
|
156
|
+
};
|
|
157
|
+
e.port.postMessage(a), this.#i.interval(() => {
|
|
158
|
+
this.#n.set(this.#t.timestamp), this.#r.set(this.#t.stalled);
|
|
159
|
+
}, 50);
|
|
160
|
+
}
|
|
161
|
+
insert(e, t) {
|
|
162
|
+
this.#t.insert(e, t);
|
|
163
|
+
}
|
|
164
|
+
setLatency(e) {
|
|
165
|
+
if (this.#t.capacity < e * 1.5) {
|
|
166
|
+
let t = Math.max(this.rate, e * 2);
|
|
167
|
+
this.#t = this.#t.resize(t), this.#t.setLatency(e);
|
|
168
|
+
let n = {
|
|
169
|
+
type: "init-shared",
|
|
170
|
+
...this.#t.init
|
|
171
|
+
};
|
|
172
|
+
this.#e.port.postMessage(n);
|
|
173
|
+
} else this.#t.setLatency(e);
|
|
174
|
+
}
|
|
175
|
+
close() {
|
|
176
|
+
this.#i.close();
|
|
177
|
+
}
|
|
178
|
+
}, ne = class {
|
|
179
|
+
rate;
|
|
180
|
+
channels;
|
|
181
|
+
#e;
|
|
182
|
+
#t = new i(0);
|
|
183
|
+
timestamp = this.#t;
|
|
184
|
+
#n = new i(!0);
|
|
185
|
+
stalled = this.#n;
|
|
186
|
+
#r = new r();
|
|
187
|
+
constructor(e, t, r, i) {
|
|
188
|
+
this.#e = e, this.channels = t, this.rate = r;
|
|
189
|
+
let a = {
|
|
190
|
+
type: "init-post",
|
|
191
|
+
channels: t,
|
|
192
|
+
rate: r,
|
|
193
|
+
latency: n.Milli.fromSecond(i / r)
|
|
194
|
+
};
|
|
195
|
+
e.port.postMessage(a), this.#r.event(e.port, "message", (e) => {
|
|
196
|
+
let t = e.data;
|
|
197
|
+
t?.type === "state" && (this.#t.set(t.timestamp), this.#n.set(t.stalled));
|
|
198
|
+
}), e.port.start();
|
|
199
|
+
}
|
|
200
|
+
insert(e, t) {
|
|
201
|
+
let n = {
|
|
202
|
+
type: "data",
|
|
203
|
+
data: t,
|
|
204
|
+
timestamp: e
|
|
205
|
+
};
|
|
206
|
+
this.#e.port.postMessage(n, t.map((e) => e.buffer));
|
|
207
|
+
}
|
|
208
|
+
setLatency(e) {
|
|
209
|
+
let t = {
|
|
210
|
+
type: "latency",
|
|
211
|
+
latency: n.Milli.fromSecond(e / this.rate)
|
|
212
|
+
};
|
|
213
|
+
this.#e.port.postMessage(t);
|
|
214
|
+
}
|
|
215
|
+
close() {
|
|
216
|
+
this.#r.close();
|
|
217
|
+
}
|
|
218
|
+
}, re = new Blob(["var __defProp = Object.defineProperty;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __esm = (fn, res) => function __init() {\n return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;\n};\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\n\n// ../net/src/path.ts\nvar init_path = __esm({\n \"../net/src/path.ts\"() {\n \"use strict\";\n }\n});\n\n// ../net/src/varint.ts\nvar MAX_U6, MAX_U14, MAX_U30, MAX_U53, MAX_U64, MAX_U62;\nvar init_varint = __esm({\n \"../net/src/varint.ts\"() {\n \"use strict\";\n MAX_U6 = 2 ** 6 - 1;\n MAX_U14 = 2 ** 14 - 1;\n MAX_U30 = 2 ** 30 - 1;\n MAX_U53 = Number.MAX_SAFE_INTEGER;\n MAX_U64 = (1n << 64n) - 1n;\n MAX_U62 = 2n ** 62n - 1n;\n }\n});\n\n// ../net/src/index.ts\ninit_path();\n\n// ../net/src/time.ts\nvar time_exports = {};\n__export(time_exports, {\n Micro: () => Micro,\n Milli: () => Milli,\n Nano: () => Nano,\n Second: () => Second\n});\nvar Nano = {\n zero: 0,\n fromMicro: (us) => us * 1e3,\n fromMilli: (ms) => ms * 1e6,\n fromSecond: (s) => s * 1e9,\n toMicro: (ns) => ns / 1e3,\n toMilli: (ns) => ns / 1e6,\n toSecond: (ns) => ns / 1e9,\n now: () => performance.now() * 1e6,\n add: (a, b) => a + b,\n sub: (a, b) => a - b,\n mul: (a, b) => a * b,\n div: (a, b) => a / b,\n max: (a, b) => Math.max(a, b),\n min: (a, b) => Math.min(a, b)\n};\nvar Micro = {\n zero: 0,\n fromNano: (ns) => ns / 1e3,\n fromMilli: (ms) => ms * 1e3,\n fromSecond: (s) => s * 1e6,\n toNano: (us) => us * 1e3,\n toMilli: (us) => us / 1e3,\n toSecond: (us) => us / 1e6,\n now: () => performance.now() * 1e3,\n add: (a, b) => a + b,\n sub: (a, b) => a - b,\n mul: (a, b) => a * b,\n div: (a, b) => a / b,\n max: (a, b) => Math.max(a, b),\n min: (a, b) => Math.min(a, b)\n};\nvar Milli = {\n zero: 0,\n fromNano: (ns) => ns / 1e6,\n fromMicro: (us) => us / 1e3,\n fromSecond: (s) => s * 1e3,\n toNano: (ms) => ms * 1e6,\n toMicro: (ms) => ms * 1e3,\n toSecond: (ms) => ms / 1e3,\n now: () => performance.now(),\n add: (a, b) => a + b,\n sub: (a, b) => a - b,\n mul: (a, b) => a * b,\n div: (a, b) => a / b,\n max: (a, b) => Math.max(a, b),\n min: (a, b) => Math.min(a, b)\n};\nvar Second = {\n zero: 0,\n fromNano: (ns) => ns / 1e9,\n fromMicro: (us) => us / 1e6,\n fromMilli: (ms) => ms / 1e3,\n toNano: (s) => s * 1e9,\n toMicro: (s) => s * 1e6,\n toMilli: (s) => s * 1e3,\n now: () => performance.now() / 1e3,\n add: (a, b) => a + b,\n sub: (a, b) => a - b,\n mul: (a, b) => a * b,\n div: (a, b) => a / b,\n max: (a, b) => Math.max(a, b),\n min: (a, b) => Math.min(a, b)\n};\n\n// ../net/src/index.ts\ninit_varint();\n\n// src/audio/ring-buffer.ts\nvar AudioRingBuffer = class {\n #buffer;\n #writeIndex = 0;\n #readIndex = 0;\n rate;\n channels;\n #stalled = true;\n constructor(props) {\n if (props.channels <= 0) throw new Error(\"invalid channels\");\n if (props.rate <= 0) throw new Error(\"invalid sample rate\");\n if (props.latency <= 0) throw new Error(\"invalid latency\");\n const samples = Math.ceil(props.rate * time_exports.Second.fromMilli(props.latency));\n if (samples === 0) throw new Error(\"empty buffer\");\n this.rate = props.rate;\n this.channels = props.channels;\n this.#buffer = [];\n for (let i = 0; i < this.channels; i++) {\n this.#buffer[i] = new Float32Array(samples);\n }\n }\n get stalled() {\n return this.#stalled;\n }\n get timestamp() {\n return time_exports.Micro.fromSecond(this.#readIndex / this.rate);\n }\n get length() {\n return this.#writeIndex - this.#readIndex;\n }\n get capacity() {\n return this.#buffer[0]?.length;\n }\n resize(latency) {\n const newCapacity = Math.ceil(this.rate * time_exports.Second.fromMilli(latency));\n if (newCapacity === this.capacity) return;\n if (newCapacity === 0) throw new Error(\"empty buffer\");\n const newBuffer = [];\n for (let i = 0; i < this.channels; i++) {\n newBuffer[i] = new Float32Array(newCapacity);\n }\n const samplesToKeep = Math.min(this.length, newCapacity);\n if (samplesToKeep > 0) {\n const copyStart = this.#writeIndex - samplesToKeep;\n for (let channel = 0; channel < this.channels; channel++) {\n const src = this.#buffer[channel];\n const dst = newBuffer[channel];\n for (let i = 0; i < samplesToKeep; i++) {\n const srcPos = (copyStart + i) % src.length;\n const dstPos = (copyStart + i) % dst.length;\n dst[dstPos] = src[srcPos];\n }\n }\n }\n this.#buffer = newBuffer;\n this.#readIndex = this.#writeIndex - samplesToKeep;\n if (samplesToKeep === 0) this.#stalled = true;\n }\n write(timestamp, data) {\n if (data.length !== this.channels) throw new Error(\"wrong number of channels\");\n let start = Math.round(time_exports.Second.fromMicro(timestamp) * this.rate);\n let samples = data[0].length;\n let offset = this.#readIndex - start;\n if (offset > samples) {\n return;\n } else if (offset > 0) {\n samples -= offset;\n start += offset;\n } else {\n offset = 0;\n }\n const end = start + samples;\n const overflow = end - this.#readIndex - this.#buffer[0].length;\n if (overflow >= 0) {\n this.#stalled = false;\n this.#readIndex += overflow;\n }\n if (start > this.#writeIndex) {\n const gapSize = Math.min(start - this.#writeIndex, this.#buffer[0].length);\n if (gapSize === 1) {\n console.warn(\"floating point inaccuracy detected\");\n }\n for (let channel = 0; channel < this.channels; channel++) {\n const dst = this.#buffer[channel];\n for (let i = 0; i < gapSize; i++) {\n const writePos = (this.#writeIndex + i) % dst.length;\n dst[writePos] = 0;\n }\n }\n }\n for (let channel = 0; channel < this.channels; channel++) {\n let src = data[channel];\n src = src.subarray(src.length - samples);\n const dst = this.#buffer[channel];\n if (src.length !== samples) throw new Error(\"mismatching number of samples\");\n for (let i = 0; i < samples; i++) {\n const writePos = (start + i) % dst.length;\n dst[writePos] = src[i];\n }\n }\n if (end > this.#writeIndex) {\n this.#writeIndex = end;\n }\n }\n read(output) {\n if (output.length !== this.channels) throw new Error(\"wrong number of channels\");\n if (this.#stalled) return 0;\n const samples = Math.min(this.#writeIndex - this.#readIndex, output[0].length);\n if (samples === 0) return 0;\n for (let channel = 0; channel < this.channels; channel++) {\n const dst = output[channel];\n const src = this.#buffer[channel];\n if (dst.length !== output[0].length) throw new Error(\"mismatching number of samples\");\n for (let i = 0; i < samples; i++) {\n const readPos = (this.#readIndex + i) % src.length;\n dst[i] = src[readPos];\n }\n }\n this.#readIndex += samples;\n return samples;\n }\n};\n\n// src/audio/shared-ring-buffer.ts\nvar WRITE = 0;\nvar READ = 1;\nvar LATENCY = 2;\nvar STALLED = 3;\nvar CONTROL_SLOTS = 4;\nfunction allocSharedRingBuffer(channels, capacity, rate) {\n if (channels <= 0) throw new Error(\"invalid channels\");\n if (capacity <= 0) throw new Error(\"invalid capacity\");\n if (rate <= 0) throw new Error(\"invalid sample rate\");\n const samples = new SharedArrayBuffer(channels * capacity * Float32Array.BYTES_PER_ELEMENT);\n const control = new SharedArrayBuffer(CONTROL_SLOTS * Int32Array.BYTES_PER_ELEMENT);\n const ctrl = new Int32Array(control);\n Atomics.store(ctrl, STALLED, 1);\n return { channels, capacity, rate, samples, control };\n}\nfunction i32Max(a, b) {\n return (a - b | 0) > 0 ? a : b;\n}\nfunction slot(idx, capacity) {\n return (idx % capacity + capacity) % capacity;\n}\nfunction casAdvance(arr, idx, candidate) {\n for (; ; ) {\n const current = Atomics.load(arr, idx);\n if ((candidate - current | 0) <= 0) return current;\n const witnessed = Atomics.compareExchange(arr, idx, current, candidate);\n if (witnessed === current) return candidate;\n }\n}\nvar SharedRingBuffer = class _SharedRingBuffer {\n channels;\n capacity;\n rate;\n init;\n #control;\n #samples;\n constructor(init) {\n this.channels = init.channels;\n this.capacity = init.capacity;\n this.rate = init.rate;\n this.init = init;\n this.#control = new Int32Array(init.control);\n this.#samples = [];\n for (let i = 0; i < this.channels; i++) {\n this.#samples.push(\n new Float32Array(init.samples, i * this.capacity * Float32Array.BYTES_PER_ELEMENT, this.capacity)\n );\n }\n }\n /**\n * Insert audio samples at the given timestamp.\n * Main thread only. Handles out-of-order writes, gap filling, and overflow.\n */\n insert(timestamp, data) {\n if (data.length !== this.channels) throw new Error(\"wrong number of channels\");\n let start = Math.round(time_exports.Second.fromMicro(timestamp) * this.rate);\n const originalLength = data[0].length;\n let offset = 0;\n const end = start + originalLength | 0;\n const read = Atomics.load(this.#control, READ);\n const behind = read - start | 0;\n if (behind > 0) {\n if (behind >= originalLength) {\n return;\n }\n offset = behind;\n start = start + behind | 0;\n }\n const samples = originalLength - offset;\n if ((end - read | 0) > this.capacity) {\n casAdvance(this.#control, READ, end - this.capacity | 0);\n }\n const write = Atomics.load(this.#control, WRITE);\n const gap = start - write | 0;\n if (gap > 0) {\n const gapSize = Math.min(gap, this.capacity);\n for (let channel = 0; channel < this.channels; channel++) {\n const dst = this.#samples[channel];\n for (let i = 0; i < gapSize; i++) {\n dst[slot(write + i | 0, this.capacity)] = 0;\n }\n }\n }\n for (let channel = 0; channel < this.channels; channel++) {\n const src = data[channel];\n const dst = this.#samples[channel];\n for (let i = 0; i < samples; i++) {\n dst[slot(start + i | 0, this.capacity)] = src[offset + i];\n }\n }\n Atomics.store(this.#control, WRITE, i32Max(Atomics.load(this.#control, WRITE), end));\n const currentRead = Atomics.load(this.#control, READ);\n const currentWrite = Atomics.load(this.#control, WRITE);\n const latency = Atomics.load(this.#control, LATENCY);\n if ((currentWrite - currentRead | 0) >= latency && latency > 0) {\n Atomics.store(this.#control, STALLED, 0);\n }\n }\n /**\n * Read audio samples into the output buffers.\n * AudioWorklet only. Returns the number of samples read.\n */\n read(output) {\n if (Atomics.load(this.#control, STALLED) === 1) return 0;\n let read = Atomics.load(this.#control, READ);\n const write = Atomics.load(this.#control, WRITE);\n const latency = Atomics.load(this.#control, LATENCY);\n const buffered = write - read | 0;\n if (latency > 0 && buffered > latency) {\n const skipTo = write - latency | 0;\n read = casAdvance(this.#control, READ, skipTo);\n }\n const available = write - read | 0;\n const count = Math.min(available, output[0].length);\n if (count <= 0) return 0;\n for (let channel = 0; channel < this.channels; channel++) {\n const src = this.#samples[channel];\n const dst = output[channel];\n for (let i = 0; i < count; i++) {\n dst[i] = src[slot(read + i | 0, this.capacity)];\n }\n }\n casAdvance(this.#control, READ, read + count | 0);\n return count;\n }\n /** Update the target latency in samples. */\n setLatency(samples) {\n Atomics.store(this.#control, LATENCY, samples);\n }\n /**\n * Allocate a new ring with `newCapacity` samples and copy the unread window\n * [READ, WRITE) plus control state into it. Used when growing capacity so\n * we don't drop buffered audio. If `newCapacity` is smaller than the unread\n * span, the oldest samples are truncated.\n *\n * Main thread only. `resize()` reads from the source `SharedRingBuffer` and\n * writes into a freshly allocated buffer from `allocSharedRingBuffer`, so it\n * relies on the same invariant as `insert()`: no concurrent main-thread\n * writers. The AudioWorklet reader is tolerated via the CAS discipline used\n * by READ/WRITE elsewhere.\n */\n resize(newCapacity) {\n const init = allocSharedRingBuffer(this.channels, newCapacity, this.rate);\n const dst = new _SharedRingBuffer(init);\n const read = Atomics.load(this.#control, READ);\n const write = Atomics.load(this.#control, WRITE);\n const latency = Atomics.load(this.#control, LATENCY);\n const stalled = Atomics.load(this.#control, STALLED);\n const available = write - read | 0;\n const copyCount = Math.max(0, Math.min(available, dst.capacity));\n const copyStart = write - copyCount | 0;\n for (let channel = 0; channel < this.channels; channel++) {\n const src = this.#samples[channel];\n const out = dst.#samples[channel];\n for (let i = 0; i < copyCount; i++) {\n const idx = copyStart + i | 0;\n out[slot(idx, dst.capacity)] = src[slot(idx, this.capacity)];\n }\n }\n Atomics.store(dst.#control, READ, copyStart);\n Atomics.store(dst.#control, WRITE, write);\n Atomics.store(dst.#control, LATENCY, latency);\n Atomics.store(dst.#control, STALLED, stalled);\n return dst;\n }\n /** Current playback timestamp derived from READ position. */\n get timestamp() {\n const read = Atomics.load(this.#control, READ);\n return time_exports.Micro.fromSecond(read / this.rate);\n }\n /** Whether the buffer is stalled (waiting to fill). */\n get stalled() {\n return Atomics.load(this.#control, STALLED) === 1;\n }\n /**\n * Number of buffered samples (WRITE - READ).\n *\n * Non-atomic: WRITE and READ are loaded separately, so a concurrent\n * writer/reader can make the two loads inconsistent. Intended for\n * tests and diagnostics, not control-flow decisions.\n */\n get length() {\n return Atomics.load(this.#control, WRITE) - Atomics.load(this.#control, READ) | 0;\n }\n};\n\n// src/audio/render-worklet.ts\nvar Render = class extends AudioWorkletProcessor {\n // Set after init, depending on which path the main thread chose.\n #backend;\n #underflow = 0;\n #stateCounter = 0;\n constructor() {\n super();\n this.port.onmessage = (event) => {\n const msg = event.data;\n if (msg.type === \"init-shared\") {\n console.log(\"[audio-worklet] init-shared: using SharedArrayBuffer path\");\n this.#backend = new SharedRingBuffer(msg);\n this.#underflow = 0;\n } else if (msg.type === \"init-post\") {\n console.log(\"[audio-worklet] init-post: using postMessage path\");\n this.#backend = new AudioRingBuffer(msg);\n this.#underflow = 0;\n } else if (msg.type === \"data\") {\n if (this.#backend instanceof AudioRingBuffer) this.#backend.write(msg.timestamp, msg.data);\n } else if (msg.type === \"latency\") {\n if (this.#backend instanceof AudioRingBuffer) this.#backend.resize(msg.latency);\n }\n };\n }\n process(_inputs, outputs, _parameters) {\n const output = outputs[0];\n const backend = this.#backend;\n const samplesRead = backend?.read(output) ?? 0;\n if (samplesRead < output[0].length) {\n this.#underflow += output[0].length - samplesRead;\n } else if (this.#underflow > 0 && backend) {\n console.debug(`audio underflow: ${Math.round(1e3 * this.#underflow / backend.rate)}ms`);\n this.#underflow = 0;\n }\n if (backend instanceof AudioRingBuffer) {\n this.#stateCounter++;\n if (this.#stateCounter >= 5) {\n this.#stateCounter = 0;\n const state = {\n type: \"state\",\n timestamp: backend.timestamp,\n stalled: backend.stalled\n };\n this.port.postMessage(state);\n }\n }\n return true;\n }\n};\nregisterProcessor(\"render\", Render);\n"], { type: "application/javascript" }), T = URL.createObjectURL(re), E = class {
|
|
219
|
+
source;
|
|
220
|
+
enabled;
|
|
221
|
+
#e = new i(void 0);
|
|
222
|
+
context = this.#e;
|
|
223
|
+
#t = new i(void 0);
|
|
224
|
+
root = this.#t;
|
|
225
|
+
#n = new i(void 0);
|
|
226
|
+
sampleRate = this.#n;
|
|
227
|
+
#r = new i(void 0);
|
|
228
|
+
stats = this.#r;
|
|
229
|
+
#i = new i(void 0);
|
|
230
|
+
timestamp = this.#i;
|
|
231
|
+
#a = new i(!0);
|
|
232
|
+
stalled = this.#a;
|
|
233
|
+
#o = new i([]);
|
|
234
|
+
#s = new i([]);
|
|
235
|
+
buffered = this.#s;
|
|
236
|
+
#c;
|
|
237
|
+
#l = new r();
|
|
238
|
+
constructor(e, t) {
|
|
239
|
+
this.source = e, this.source.supported.set(D), this.enabled = i.from(t?.enabled ?? !1), this.#l.run(this.#u.bind(this)), this.#l.run(this.#d.bind(this)), this.#l.run(this.#f.bind(this)), this.#l.run(this.#p.bind(this));
|
|
240
|
+
}
|
|
241
|
+
#u(e) {
|
|
242
|
+
let t = e.get(this.source.config);
|
|
243
|
+
if (!t) return;
|
|
244
|
+
let r = t.sampleRate, i = t.numberOfChannels, a = new AudioContext({
|
|
245
|
+
latencyHint: "interactive",
|
|
246
|
+
sampleRate: r
|
|
247
|
+
});
|
|
248
|
+
e.set(this.#e, a), e.cleanup(() => a.close()), e.spawn(async () => {
|
|
249
|
+
if (await a.audioWorklet.addModule(T), a.state === "closed") return;
|
|
250
|
+
let t = new AudioWorkletNode(a, "render", {
|
|
251
|
+
channelCount: i,
|
|
252
|
+
channelCountMode: "explicit",
|
|
253
|
+
outputChannelCount: [i]
|
|
254
|
+
});
|
|
255
|
+
e.cleanup(() => t.disconnect());
|
|
256
|
+
let o = this.source.sync.buffer.peek(), s = ee(t, i, r, Math.ceil(r * n.Second.fromMilli(o)));
|
|
257
|
+
this.#c = s, e.cleanup(() => {
|
|
258
|
+
s.close(), this.#c = void 0;
|
|
259
|
+
}), e.run((e) => {
|
|
260
|
+
let t = n.Milli.fromMicro(e.get(s.timestamp));
|
|
261
|
+
this.#i.set(t), this.#v(t);
|
|
262
|
+
}), e.run((e) => {
|
|
263
|
+
this.#a.set(e.get(s.stalled));
|
|
264
|
+
}), e.set(this.#t, t);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
#d(e) {
|
|
268
|
+
let t = e.getAll([this.enabled, this.#e]);
|
|
269
|
+
if (!t) return;
|
|
270
|
+
let [n, r] = t;
|
|
271
|
+
r.resume();
|
|
272
|
+
}
|
|
273
|
+
#f(e) {
|
|
274
|
+
if (!e.get(this.#t)) return;
|
|
275
|
+
let t = this.#c;
|
|
276
|
+
if (!t) return;
|
|
277
|
+
let r = e.get(this.source.sync.buffer), i = Math.ceil(t.rate * n.Second.fromMilli(r));
|
|
278
|
+
t.setLatency(i);
|
|
279
|
+
}
|
|
280
|
+
#p(e) {
|
|
281
|
+
if (!e.get(this.enabled)) return;
|
|
282
|
+
let t = e.get(this.source.broadcast);
|
|
283
|
+
if (!t) return;
|
|
284
|
+
let n = e.get(this.source.track);
|
|
285
|
+
if (!n) return;
|
|
286
|
+
let r = e.get(this.source.config);
|
|
287
|
+
if (!r) return;
|
|
288
|
+
let i = e.get(t.active);
|
|
289
|
+
if (!i) return;
|
|
290
|
+
let o = i.subscribe(n, a.PRIORITY.audio);
|
|
291
|
+
e.cleanup(() => o.close()), r.container.kind === "cmaf" ? this.#h(e, o, r) : this.#m(e, o, r);
|
|
292
|
+
}
|
|
293
|
+
#m(e, t, r) {
|
|
294
|
+
let i = r.container.kind === "loc" ? new s.Loc.Format() : new s.Legacy.Format(), a = new s.Consumer(t, {
|
|
295
|
+
format: i,
|
|
296
|
+
latency: this.source.sync.buffer
|
|
297
|
+
});
|
|
298
|
+
e.cleanup(() => a.close()), e.run((e) => {
|
|
299
|
+
let t = e.get(a.buffered), n = e.get(this.#o);
|
|
300
|
+
this.#s.update(() => s.mergeBufferedRanges(t, n));
|
|
301
|
+
}), e.spawn(async () => {
|
|
302
|
+
if (!await c.Libav.polyfill()) return;
|
|
303
|
+
let t = 0, i = new AudioDecoder({
|
|
304
|
+
output: (e) => {
|
|
305
|
+
if (t++, t <= 3) {
|
|
306
|
+
e.close();
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
this.#g(e);
|
|
310
|
+
},
|
|
311
|
+
error: (e) => console.error(e)
|
|
312
|
+
});
|
|
313
|
+
e.cleanup(() => {
|
|
314
|
+
i.state !== "closed" && i.close();
|
|
315
|
+
});
|
|
316
|
+
let o = r.codec === "opus" ? void 0 : r.description ? c.Hex.toBytes(r.description) : void 0;
|
|
317
|
+
for (i.configure({
|
|
318
|
+
...r,
|
|
319
|
+
description: o
|
|
320
|
+
});;) {
|
|
321
|
+
let e = await a.next();
|
|
322
|
+
if (!e) break;
|
|
323
|
+
let { frame: t } = e;
|
|
324
|
+
if (!t) continue;
|
|
325
|
+
let r = n.Milli.fromMicro(t.timestamp);
|
|
326
|
+
this.source.sync.received(r, "audio"), this.#r.update((e) => ({ bytesReceived: (e?.bytesReceived ?? 0) + t.data.byteLength }));
|
|
327
|
+
let o = new EncodedAudioChunk({
|
|
328
|
+
type: t.keyframe ? "key" : "delta",
|
|
329
|
+
data: t.data,
|
|
330
|
+
timestamp: t.timestamp
|
|
331
|
+
});
|
|
332
|
+
i.decode(o);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
#h(e, t, r) {
|
|
337
|
+
if (r.container.kind !== "cmaf") return;
|
|
338
|
+
let i = p(r.container.init), a = s.Cmaf.decodeInitSegment(i), o = r.codec === "opus" ? void 0 : r.description ? c.Hex.toBytes(r.description) : a.description, l = new s.Consumer(t, {
|
|
339
|
+
format: new s.Cmaf.Format(a),
|
|
340
|
+
latency: this.source.sync.buffer
|
|
341
|
+
});
|
|
342
|
+
e.cleanup(() => l.close()), e.run((e) => {
|
|
343
|
+
let t = e.get(l.buffered), n = e.get(this.#o);
|
|
344
|
+
this.#s.update(() => s.mergeBufferedRanges(t, n));
|
|
345
|
+
}), e.spawn(async () => {
|
|
346
|
+
if (!await c.Libav.polyfill()) return;
|
|
347
|
+
let t = new AudioDecoder({
|
|
348
|
+
output: (e) => this.#g(e),
|
|
349
|
+
error: (e) => console.error(e)
|
|
350
|
+
});
|
|
351
|
+
for (e.cleanup(() => {
|
|
352
|
+
t.state !== "closed" && t.close();
|
|
353
|
+
}), t.configure({
|
|
354
|
+
codec: r.codec,
|
|
355
|
+
sampleRate: r.sampleRate,
|
|
356
|
+
numberOfChannels: r.numberOfChannels,
|
|
357
|
+
description: o
|
|
358
|
+
});;) {
|
|
359
|
+
let e = await l.next();
|
|
360
|
+
if (!e) break;
|
|
361
|
+
let { frame: r } = e;
|
|
362
|
+
if (!r) continue;
|
|
363
|
+
let i = n.Milli.fromMicro(r.timestamp);
|
|
364
|
+
if (this.source.sync.received(i, "audio"), this.#r.update((e) => ({ bytesReceived: (e?.bytesReceived ?? 0) + r.data.byteLength })), t.state === "closed") break;
|
|
365
|
+
t.decode(new EncodedAudioChunk({
|
|
366
|
+
type: r.keyframe ? "key" : "delta",
|
|
367
|
+
data: r.data,
|
|
368
|
+
timestamp: r.timestamp
|
|
369
|
+
}));
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
#g(e) {
|
|
374
|
+
let t = e.timestamp, r = n.Milli.fromMicro(t), i = this.#c;
|
|
375
|
+
if (!i) {
|
|
376
|
+
e.close();
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
let a = e.numberOfFrames / e.sampleRate * 1e6, o = n.Milli.fromMicro(a), s = n.Milli.add(r, o);
|
|
380
|
+
this.#_(r, s);
|
|
381
|
+
let c = Math.min(e.numberOfChannels, i.channels), l = [];
|
|
382
|
+
for (let t = 0; t < c; t++) {
|
|
383
|
+
let n = new Float32Array(e.numberOfFrames);
|
|
384
|
+
e.copyTo(n, {
|
|
385
|
+
format: "f32-planar",
|
|
386
|
+
planeIndex: t
|
|
387
|
+
}), l.push(n);
|
|
388
|
+
}
|
|
389
|
+
i.insert(t, l), e.close();
|
|
390
|
+
}
|
|
391
|
+
#_(e, t) {
|
|
392
|
+
e > t || this.#o.mutate((r) => {
|
|
393
|
+
for (let i of r) if (e <= i.end + 1 && t >= i.start) {
|
|
394
|
+
i.start = n.Milli.min(i.start, e), i.end = n.Milli.max(i.end, t);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
r.push({
|
|
398
|
+
start: e,
|
|
399
|
+
end: t
|
|
400
|
+
}), r.sort((e, t) => e.start - t.start);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
#v(e) {
|
|
404
|
+
this.#o.mutate((t) => {
|
|
405
|
+
for (; t.length > 0;) {
|
|
406
|
+
if (t[0].end >= e) {
|
|
407
|
+
t[0].start = n.Milli.max(t[0].start, e);
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
t.shift();
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
close() {
|
|
415
|
+
this.#l.close();
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
async function D(e) {
|
|
419
|
+
let t;
|
|
420
|
+
if (e.codec !== "opus") {
|
|
421
|
+
if (e.description) t = c.Hex.toBytes(e.description);
|
|
422
|
+
else if (e.container.kind === "cmaf") try {
|
|
423
|
+
t = s.Cmaf.decodeInitSegment(p(e.container.init)).description;
|
|
424
|
+
} catch (t) {
|
|
425
|
+
return console.warn(`audio: malformed CMAF init segment for codec ${e.codec}`, t), !1;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return (await AudioDecoder.isConfigSupported({
|
|
429
|
+
...e,
|
|
430
|
+
description: t
|
|
431
|
+
})).supported ?? !1;
|
|
432
|
+
}
|
|
433
|
+
//#endregion
|
|
434
|
+
//#region src/audio/emitter.ts
|
|
435
|
+
var O = .001, k = .2, A = class {
|
|
436
|
+
source;
|
|
437
|
+
volume;
|
|
438
|
+
muted;
|
|
439
|
+
paused;
|
|
440
|
+
#e = new r();
|
|
441
|
+
#t = .5;
|
|
442
|
+
#n = new i(void 0);
|
|
443
|
+
constructor(e, t) {
|
|
444
|
+
this.source = e, this.volume = i.from(t?.volume ?? .5), this.muted = i.from(t?.muted ?? !1), this.paused = i.from(t?.paused ?? t?.muted ?? !1), this.#e.run((e) => {
|
|
445
|
+
e.get(this.muted) ? (this.#t = this.volume.peek() || .5, this.volume.set(0)) : this.volume.set(this.#t);
|
|
446
|
+
}), this.#e.run((e) => {
|
|
447
|
+
let t = !e.get(this.paused) && !e.get(this.muted);
|
|
448
|
+
this.source.enabled.set(t);
|
|
449
|
+
}), this.#e.run((e) => {
|
|
450
|
+
let t = e.get(this.volume);
|
|
451
|
+
this.muted.set(t === 0);
|
|
452
|
+
}), this.#e.run((e) => {
|
|
453
|
+
let t = e.get(this.source.root);
|
|
454
|
+
if (!t) return;
|
|
455
|
+
let n = new GainNode(t.context, { gain: e.get(this.volume) });
|
|
456
|
+
t.connect(n), e.set(this.#n, n), e.run((e) => {
|
|
457
|
+
e.get(this.source.enabled) && (n.connect(t.context.destination), e.cleanup(() => n.disconnect()));
|
|
458
|
+
});
|
|
459
|
+
}), this.#e.run((e) => {
|
|
460
|
+
let t = e.get(this.#n);
|
|
461
|
+
if (!t) return;
|
|
462
|
+
e.cleanup(() => t.gain.cancelScheduledValues(t.context.currentTime));
|
|
463
|
+
let n = e.get(this.volume);
|
|
464
|
+
n < O ? (t.gain.exponentialRampToValueAtTime(O, t.context.currentTime + k), t.gain.setValueAtTime(0, t.context.currentTime + k + .01)) : t.gain.exponentialRampToValueAtTime(n, t.context.currentTime + k);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
close() {
|
|
468
|
+
this.#e.close();
|
|
469
|
+
}
|
|
470
|
+
}, ie = /* @__PURE__ */ f({ Muxer: () => j }), j = class {
|
|
471
|
+
element;
|
|
472
|
+
paused;
|
|
473
|
+
#e;
|
|
474
|
+
#t = new i(void 0);
|
|
475
|
+
mediaSource = this.#t;
|
|
476
|
+
#n = new r();
|
|
477
|
+
constructor(e, t) {
|
|
478
|
+
this.element = i.from(t?.element), this.paused = i.from(t?.paused ?? !1), this.#e = e, this.#n.run(this.#r.bind(this)), this.#n.run(this.#i.bind(this)), this.#n.run(this.#a.bind(this)), this.#n.run(this.#o.bind(this)), this.#n.run(this.#s.bind(this));
|
|
479
|
+
}
|
|
480
|
+
#r(e) {
|
|
481
|
+
let t = e.get(this.element);
|
|
482
|
+
if (!t) return;
|
|
483
|
+
let n = new MediaSource();
|
|
484
|
+
t.src = URL.createObjectURL(n), e.cleanup(() => URL.revokeObjectURL(t.src)), e.event(n, "sourceopen", () => {
|
|
485
|
+
e.set(this.#t, n);
|
|
486
|
+
}, { once: !0 }), e.event(n, "error", (e) => {
|
|
487
|
+
console.error("[MSE] MediaSource error event:", e);
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
#i(e) {
|
|
491
|
+
let t = e.get(this.element);
|
|
492
|
+
if (!t || e.get(this.paused)) return;
|
|
493
|
+
let r = n.Milli.toSecond(e.get(this.#e.buffer));
|
|
494
|
+
e.interval(() => {
|
|
495
|
+
let e = t.buffered;
|
|
496
|
+
if (e.length === 0) return;
|
|
497
|
+
let n = e.end(e.length - 1) - r, i = n - t.currentTime;
|
|
498
|
+
(i > .1 || i < -.1) && (console.warn("seeking", i > 0 ? "forward" : "backward", Math.abs(i).toFixed(3), "seconds"), t.currentTime = n);
|
|
499
|
+
}, 100);
|
|
500
|
+
}
|
|
501
|
+
#a(e) {
|
|
502
|
+
let t = e.get(this.element);
|
|
503
|
+
if (!t) return;
|
|
504
|
+
let n = e.get(this.mediaSource);
|
|
505
|
+
n && e.interval(async () => {
|
|
506
|
+
for (let e of n.sourceBuffers) {
|
|
507
|
+
for (; e.updating;) await new Promise((t) => e.addEventListener("updateend", t, { once: !0 }));
|
|
508
|
+
t.currentTime > 10 && e.remove(0, t.currentTime - 10);
|
|
509
|
+
}
|
|
510
|
+
}, 1e3);
|
|
511
|
+
}
|
|
512
|
+
#o(e) {
|
|
513
|
+
let t = e.get(this.element);
|
|
514
|
+
if (!t) return;
|
|
515
|
+
let n = e.get(this.paused);
|
|
516
|
+
n && !t.paused ? t.pause() : !n && t.paused && t.play().catch((e) => {
|
|
517
|
+
console.error("[MSE] MediaElement play error:", e);
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
#s(e) {
|
|
521
|
+
let t = e.get(this.element);
|
|
522
|
+
if (!t || e.get(this.paused)) return;
|
|
523
|
+
let r = e.get(this.#e.reference);
|
|
524
|
+
if (r === void 0) return;
|
|
525
|
+
let i = e.get(this.#e.buffer), a = n.Milli.sub(n.Milli.sub(n.Milli.now(), r), i);
|
|
526
|
+
t.currentTime = n.Milli.toSecond(a);
|
|
527
|
+
}
|
|
528
|
+
close() {
|
|
529
|
+
this.#n.close();
|
|
530
|
+
}
|
|
531
|
+
}, ae = 20, M = 100, N = class e {
|
|
532
|
+
#e = new i(void 0);
|
|
533
|
+
reference = this.#e;
|
|
534
|
+
latency;
|
|
535
|
+
jitter;
|
|
536
|
+
audio;
|
|
537
|
+
video;
|
|
538
|
+
#t = new i(n.Milli.zero);
|
|
539
|
+
buffer = this.#t;
|
|
540
|
+
#n;
|
|
541
|
+
timestamp = new i(void 0);
|
|
542
|
+
#r = /* @__PURE__ */ new Map();
|
|
543
|
+
#i;
|
|
544
|
+
#a;
|
|
545
|
+
signals = new r();
|
|
546
|
+
constructor(e) {
|
|
547
|
+
this.latency = i.from(e?.latency ?? "real-time"), this.jitter = new i(M), this.#i = e?.connection, this.audio = i.from(e?.audio), this.video = i.from(e?.video), this.#n = Promise.withResolvers(), this.signals.run(this.#o.bind(this)), this.signals.run(this.#s.bind(this));
|
|
548
|
+
}
|
|
549
|
+
#o(e) {
|
|
550
|
+
let t = e.get(this.latency);
|
|
551
|
+
if (typeof t == "number") {
|
|
552
|
+
this.#a = void 0, this.jitter.set(t);
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
let n = (this.#i ? e.get(this.#i) : void 0)?.rtt, r = n ? e.get(n) : void 0;
|
|
556
|
+
if (r !== void 0) {
|
|
557
|
+
this.#a = this.#a === void 0 ? r : Math.min(this.#a, r);
|
|
558
|
+
let e = Math.max(ae, this.#a * 1.25);
|
|
559
|
+
this.jitter.set(e);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
this.#a = void 0, this.jitter.set(M);
|
|
563
|
+
}
|
|
564
|
+
#s(e) {
|
|
565
|
+
let t = e.get(this.jitter), r = e.get(this.video) ?? n.Milli.zero, i = e.get(this.audio) ?? n.Milli.zero, a = n.Milli.add(n.Milli.max(r, i), t);
|
|
566
|
+
this.#t.set(a), this.#n.resolve(), this.#n = Promise.withResolvers();
|
|
567
|
+
}
|
|
568
|
+
received(t, r = "") {
|
|
569
|
+
this.timestamp.update((e) => e === void 0 || t > e ? t : e);
|
|
570
|
+
let i = n.Milli.now(), a = n.Milli.sub(i, t), o = this.#e.peek();
|
|
571
|
+
if (o !== void 0) {
|
|
572
|
+
let t = n.Milli.add(n.Milli.sub(o, a), this.#t.peek());
|
|
573
|
+
if (t < 0) {
|
|
574
|
+
let e = this.#r.get(r);
|
|
575
|
+
e ? (e.count++, e.maxMs = Math.max(e.maxMs, -t)) : this.#r.set(r, {
|
|
576
|
+
count: 1,
|
|
577
|
+
maxMs: -t
|
|
578
|
+
});
|
|
579
|
+
} else {
|
|
580
|
+
let t = this.#r.get(r);
|
|
581
|
+
if (t) {
|
|
582
|
+
let n = r ? `sync[${r}]` : "sync", i = e.#c(t.maxMs);
|
|
583
|
+
console.debug(`${n}: ${t.count} late frame(s), max ${i} behind`), this.#r.delete(r);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
if (a >= o) return;
|
|
587
|
+
}
|
|
588
|
+
this.#e.set(a), this.#n.resolve(), this.#n = Promise.withResolvers();
|
|
589
|
+
}
|
|
590
|
+
now() {
|
|
591
|
+
let e = this.#e.peek();
|
|
592
|
+
if (e !== void 0) return n.Milli.sub(n.Milli.sub(n.Milli.now(), e), this.#t.peek());
|
|
593
|
+
}
|
|
594
|
+
async wait(e) {
|
|
595
|
+
if (this.#e.peek() === void 0) throw Error("reference not set; call update() first");
|
|
596
|
+
for (;;) {
|
|
597
|
+
let t = n.Milli.now(), r = n.Milli.sub(t, e), i = this.#e.peek();
|
|
598
|
+
if (i === void 0) return;
|
|
599
|
+
let a = n.Milli.add(n.Milli.sub(i, r), this.#t.peek());
|
|
600
|
+
if (a <= 0 || a < 5) return;
|
|
601
|
+
let o = new Promise((e) => setTimeout(e, a)).then(() => !0);
|
|
602
|
+
if (await Promise.race([this.#n.promise, o])) return;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
static #c(e) {
|
|
606
|
+
if (e = Math.round(e), e < 1e3) return `${e}ms`;
|
|
607
|
+
let t = e / 1e3;
|
|
608
|
+
if (t < 60) return `${Math.round(t * 10) / 10}s`;
|
|
609
|
+
let n = t / 60;
|
|
610
|
+
return `${Math.round(n * 10) / 10}m`;
|
|
611
|
+
}
|
|
612
|
+
close() {
|
|
613
|
+
this.signals.close();
|
|
614
|
+
}
|
|
615
|
+
}, oe = 500, P = 100, F = class {
|
|
616
|
+
enabled;
|
|
617
|
+
source;
|
|
618
|
+
#e = new i(void 0);
|
|
619
|
+
#t = new i(void 0);
|
|
620
|
+
frame = this.#t;
|
|
621
|
+
#n = new i(void 0);
|
|
622
|
+
timestamp = this.#n;
|
|
623
|
+
#r = new i(void 0);
|
|
624
|
+
display = this.#r;
|
|
625
|
+
#i = new i(!1);
|
|
626
|
+
stalled = this.#i;
|
|
627
|
+
#a = new i(void 0);
|
|
628
|
+
stats = this.#a;
|
|
629
|
+
#o = new i([]);
|
|
630
|
+
buffered = this.#o;
|
|
631
|
+
#s = new r();
|
|
632
|
+
#c() {
|
|
633
|
+
this.#t.update((e) => {
|
|
634
|
+
e?.close();
|
|
635
|
+
}), this.#n.set(void 0);
|
|
636
|
+
}
|
|
637
|
+
constructor(e, t) {
|
|
638
|
+
this.enabled = i.from(t?.enabled ?? !1), this.source = e, this.source.supported.set(L), this.#s.run(this.#l.bind(this)), this.#s.run(this.#u.bind(this)), this.#s.run(this.#d.bind(this)), this.#s.run(this.#f.bind(this));
|
|
639
|
+
}
|
|
640
|
+
#l(e) {
|
|
641
|
+
let t = e.getAll([
|
|
642
|
+
this.enabled,
|
|
643
|
+
this.source.broadcast,
|
|
644
|
+
this.source.track,
|
|
645
|
+
this.source.config
|
|
646
|
+
]);
|
|
647
|
+
if (!t) {
|
|
648
|
+
this.#e.set(void 0);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
let [n, r, i, a] = t, o = e.get(r.active);
|
|
652
|
+
if (!o) {
|
|
653
|
+
this.#e.set(void 0), this.#c(), this.#o.set([]);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
let s = new I({
|
|
657
|
+
source: this.source,
|
|
658
|
+
broadcast: o,
|
|
659
|
+
track: i,
|
|
660
|
+
config: a,
|
|
661
|
+
stats: this.#a
|
|
662
|
+
});
|
|
663
|
+
e.cleanup(() => s?.close()), e.run((e) => {
|
|
664
|
+
if (!s) return;
|
|
665
|
+
let t = e.get(this.#e);
|
|
666
|
+
if (t) {
|
|
667
|
+
let n = e.get(s.timestamp), r = e.get(t.timestamp);
|
|
668
|
+
if (!n || r && r > n + P) return;
|
|
669
|
+
}
|
|
670
|
+
this.#e.set(s), s = void 0, e.close();
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
#u(e) {
|
|
674
|
+
let t = e.get(this.#e);
|
|
675
|
+
if (!t) {
|
|
676
|
+
this.#o.set([]);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
e.cleanup(() => t.close()), e.run((e) => {
|
|
680
|
+
let n = e.get(t.frame);
|
|
681
|
+
this.#t.update((e) => (e?.close(), n?.clone()));
|
|
682
|
+
}), e.proxy(this.#n, t.timestamp), e.proxy(this.#o, t.buffered);
|
|
683
|
+
}
|
|
684
|
+
#d(e) {
|
|
685
|
+
let t = e.get(this.source.catalog);
|
|
686
|
+
if (!t) return;
|
|
687
|
+
let n = t.display;
|
|
688
|
+
if (n) {
|
|
689
|
+
e.set(this.#r, {
|
|
690
|
+
width: n.width,
|
|
691
|
+
height: n.height
|
|
692
|
+
});
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
let r = e.get(this.frame);
|
|
696
|
+
r && e.set(this.#r, {
|
|
697
|
+
width: r.displayWidth,
|
|
698
|
+
height: r.displayHeight
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
#f(e) {
|
|
702
|
+
if (e.get(this.enabled)) {
|
|
703
|
+
if (!e.get(this.frame)) {
|
|
704
|
+
this.#i.set(!0);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
this.#i.set(!1), e.timer(() => {
|
|
708
|
+
this.#i.set(!0);
|
|
709
|
+
}, oe);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
close() {
|
|
713
|
+
this.#c(), this.#s.close();
|
|
714
|
+
}
|
|
715
|
+
}, I = class {
|
|
716
|
+
source;
|
|
717
|
+
broadcast;
|
|
718
|
+
track;
|
|
719
|
+
config;
|
|
720
|
+
stats;
|
|
721
|
+
timestamp = new i(void 0);
|
|
722
|
+
frame = new i(void 0);
|
|
723
|
+
buffered = new i([]);
|
|
724
|
+
#e = new i([]);
|
|
725
|
+
signals = new r();
|
|
726
|
+
constructor(e) {
|
|
727
|
+
let { codedWidth: t, codedHeight: n, ...r } = e.config;
|
|
728
|
+
this.source = e.source, this.broadcast = e.broadcast, this.track = e.track, this.config = r, this.stats = e.stats, this.signals.run(this.#t.bind(this));
|
|
729
|
+
}
|
|
730
|
+
#t(e) {
|
|
731
|
+
let t = this.broadcast.subscribe(this.track, a.PRIORITY.video);
|
|
732
|
+
e.cleanup(() => t.close());
|
|
733
|
+
let r = new VideoDecoder({
|
|
734
|
+
output: async (t) => {
|
|
735
|
+
try {
|
|
736
|
+
let r = n.Milli.fromMicro(t.timestamp);
|
|
737
|
+
if (r < (this.timestamp.peek() ?? 0)) return;
|
|
738
|
+
this.frame.peek() === void 0 && this.frame.set(t.clone());
|
|
739
|
+
let i = this.source.sync.wait(r).then(() => !0);
|
|
740
|
+
if (!await Promise.race([i, e.cancel]) || r < (this.timestamp.peek() ?? 0)) return;
|
|
741
|
+
this.timestamp.set(r), this.#a(r), this.frame.update((e) => (e?.close(), t.clone()));
|
|
742
|
+
} finally {
|
|
743
|
+
t.close();
|
|
744
|
+
}
|
|
745
|
+
},
|
|
746
|
+
error: (t) => {
|
|
747
|
+
console.error(t), e.close();
|
|
748
|
+
}
|
|
749
|
+
});
|
|
750
|
+
e.cleanup(() => {
|
|
751
|
+
r.state !== "closed" && r.close();
|
|
752
|
+
}), this.config.container.kind === "cmaf" ? this.#r(e, t, r) : this.#n(e, t, r);
|
|
753
|
+
}
|
|
754
|
+
#n(e, t, r) {
|
|
755
|
+
let i = this.config.container.kind === "loc" ? new s.Loc.Format() : new s.Legacy.Format(), a = new s.Consumer(t, {
|
|
756
|
+
format: i,
|
|
757
|
+
latency: this.source.sync.buffer
|
|
758
|
+
});
|
|
759
|
+
e.cleanup(() => a.close()), e.run((e) => {
|
|
760
|
+
let t = e.get(a.buffered), n = e.get(this.#e);
|
|
761
|
+
this.buffered.update(() => s.mergeBufferedRanges(t, n));
|
|
762
|
+
}), r.configure({
|
|
763
|
+
...this.config,
|
|
764
|
+
description: this.config.description ? c.Hex.toBytes(this.config.description) : void 0,
|
|
765
|
+
optimizeForLatency: this.config.optimizeForLatency ?? !0,
|
|
766
|
+
flip: !1
|
|
767
|
+
});
|
|
768
|
+
let o;
|
|
769
|
+
e.spawn(async () => {
|
|
770
|
+
for (;;) {
|
|
771
|
+
let e = await a.next();
|
|
772
|
+
if (!e) break;
|
|
773
|
+
let { frame: t, group: i } = e;
|
|
774
|
+
if (!t) {
|
|
775
|
+
o && (o.final = !0);
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
let s = n.Milli.fromMicro(t.timestamp);
|
|
779
|
+
this.source.sync.received(s, "video");
|
|
780
|
+
let c = new EncodedVideoChunk({
|
|
781
|
+
type: t.keyframe ? "key" : "delta",
|
|
782
|
+
data: t.data,
|
|
783
|
+
timestamp: t.timestamp
|
|
784
|
+
});
|
|
785
|
+
this.stats.update((e) => ({
|
|
786
|
+
frameCount: (e?.frameCount ?? 0) + 1,
|
|
787
|
+
bytesReceived: (e?.bytesReceived ?? 0) + t.data.byteLength
|
|
788
|
+
}));
|
|
789
|
+
let l = o;
|
|
790
|
+
if (l && (l.group === i || l.final && l.group + 1 === i)) {
|
|
791
|
+
let e = n.Milli.fromMicro(l.timestamp), r = n.Milli.fromMicro(t.timestamp);
|
|
792
|
+
this.#i(e, r);
|
|
793
|
+
}
|
|
794
|
+
o = {
|
|
795
|
+
timestamp: t.timestamp,
|
|
796
|
+
group: i,
|
|
797
|
+
final: !1
|
|
798
|
+
}, r.decode(c);
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
#r(e, t, r) {
|
|
803
|
+
if (this.config.container.kind !== "cmaf") return;
|
|
804
|
+
let i = p(this.config.container.init), a = s.Cmaf.decodeInitSegment(i), o = this.config.description ? c.Hex.toBytes(this.config.description) : a.description, l = new s.Consumer(t, {
|
|
805
|
+
format: new s.Cmaf.Format(a),
|
|
806
|
+
latency: this.source.sync.buffer
|
|
807
|
+
});
|
|
808
|
+
e.cleanup(() => l.close()), e.run((e) => {
|
|
809
|
+
let t = e.get(l.buffered), n = e.get(this.#e);
|
|
810
|
+
this.buffered.update(() => s.mergeBufferedRanges(t, n));
|
|
811
|
+
}), r.configure({
|
|
812
|
+
codec: this.config.codec,
|
|
813
|
+
description: o,
|
|
814
|
+
optimizeForLatency: this.config.optimizeForLatency ?? !0,
|
|
815
|
+
flip: !1
|
|
816
|
+
});
|
|
817
|
+
let u;
|
|
818
|
+
e.spawn(async () => {
|
|
819
|
+
for (;;) {
|
|
820
|
+
let e = await l.next();
|
|
821
|
+
if (!e) break;
|
|
822
|
+
let { frame: t, group: i } = e;
|
|
823
|
+
if (!t) {
|
|
824
|
+
u && (u.final = !0);
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
let a = n.Milli.fromMicro(t.timestamp);
|
|
828
|
+
this.source.sync.received(a, "video"), this.stats.update((e) => ({
|
|
829
|
+
frameCount: (e?.frameCount ?? 0) + 1,
|
|
830
|
+
bytesReceived: (e?.bytesReceived ?? 0) + t.data.byteLength
|
|
831
|
+
}));
|
|
832
|
+
let o = u;
|
|
833
|
+
if (o && (o.group === i || o.final && o.group + 1 === i)) {
|
|
834
|
+
let e = n.Milli.fromMicro(o.timestamp), r = n.Milli.fromMicro(t.timestamp);
|
|
835
|
+
this.#i(e, r);
|
|
836
|
+
}
|
|
837
|
+
if (u = {
|
|
838
|
+
timestamp: t.timestamp,
|
|
839
|
+
group: i,
|
|
840
|
+
final: !1
|
|
841
|
+
}, r.state === "closed") break;
|
|
842
|
+
r.decode(new EncodedVideoChunk({
|
|
843
|
+
type: t.keyframe ? "key" : "delta",
|
|
844
|
+
data: t.data,
|
|
845
|
+
timestamp: t.timestamp
|
|
846
|
+
}));
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
#i(e, t) {
|
|
851
|
+
e > t || this.#e.mutate((r) => {
|
|
852
|
+
for (let i of r) if (i.start <= t && i.end >= e) {
|
|
853
|
+
i.start = n.Milli.min(i.start, e), i.end = n.Milli.max(i.end, t);
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
r.push({
|
|
857
|
+
start: e,
|
|
858
|
+
end: t
|
|
859
|
+
}), r.sort((e, t) => e.start - t.start);
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
#a(e) {
|
|
863
|
+
this.#e.mutate((t) => {
|
|
864
|
+
for (; t.length > 0;) {
|
|
865
|
+
if (t[0].end >= e) {
|
|
866
|
+
t[0].start = n.Milli.max(t[0].start, e);
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
t.shift();
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
close() {
|
|
874
|
+
this.signals.close(), this.frame.update((e) => {
|
|
875
|
+
e?.close();
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
async function L(e) {
|
|
880
|
+
let t;
|
|
881
|
+
if (e.description) t = c.Hex.toBytes(e.description);
|
|
882
|
+
else if (e.container.kind === "cmaf") try {
|
|
883
|
+
t = s.Cmaf.decodeInitSegment(p(e.container.init)).description;
|
|
884
|
+
} catch (t) {
|
|
885
|
+
return console.warn(`video: malformed CMAF init segment for codec ${e.codec}`, t), !1;
|
|
886
|
+
}
|
|
887
|
+
let { supported: n } = await VideoDecoder.isConfigSupported({
|
|
888
|
+
codec: e.codec,
|
|
889
|
+
description: t,
|
|
890
|
+
optimizeForLatency: e.optimizeForLatency ?? !0
|
|
891
|
+
});
|
|
892
|
+
if (n) return !0;
|
|
893
|
+
if (e.codec.startsWith("avc3.")) {
|
|
894
|
+
let n = `avc1.${e.codec.slice(5)}`;
|
|
895
|
+
if ((await VideoDecoder.isConfigSupported({
|
|
896
|
+
codec: n,
|
|
897
|
+
description: t,
|
|
898
|
+
optimizeForLatency: e.optimizeForLatency ?? !0
|
|
899
|
+
})).supported) return e.codec = n, !0;
|
|
900
|
+
}
|
|
901
|
+
return !1;
|
|
902
|
+
}
|
|
903
|
+
//#endregion
|
|
904
|
+
//#region src/video/mse.ts
|
|
905
|
+
var R = class {
|
|
906
|
+
muxer;
|
|
907
|
+
source;
|
|
908
|
+
#e = new i(void 0);
|
|
909
|
+
stats = this.#e;
|
|
910
|
+
#t = new i([]);
|
|
911
|
+
buffered = this.#t;
|
|
912
|
+
#n = new i(!1);
|
|
913
|
+
stalled = this.#n;
|
|
914
|
+
#r = new i(e.Time.Milli.zero);
|
|
915
|
+
timestamp = this.#r;
|
|
916
|
+
signals = new r();
|
|
917
|
+
constructor(e, t) {
|
|
918
|
+
this.muxer = e, this.source = t, this.source.supported.set(z), this.signals.run(this.#i.bind(this)), this.signals.run(this.#c.bind(this)), this.signals.run(this.#l.bind(this));
|
|
919
|
+
}
|
|
920
|
+
#i(e) {
|
|
921
|
+
let t = e.get(this.muxer.element);
|
|
922
|
+
if (!t) return;
|
|
923
|
+
let n = e.get(this.muxer.mediaSource);
|
|
924
|
+
if (!n) return;
|
|
925
|
+
let r = e.get(this.source.broadcast);
|
|
926
|
+
if (!r) return;
|
|
927
|
+
let i = e.get(r.active);
|
|
928
|
+
if (!i) return;
|
|
929
|
+
let a = e.get(this.source.track);
|
|
930
|
+
if (!a) return;
|
|
931
|
+
let o = e.get(this.source.config);
|
|
932
|
+
if (!o) return;
|
|
933
|
+
let s = `video/mp4; codecs="${o.codec}"`, c = n.addSourceBuffer(s);
|
|
934
|
+
e.cleanup(() => {
|
|
935
|
+
n.removeSourceBuffer(c), c.abort();
|
|
936
|
+
}), e.event(c, "error", (e) => {
|
|
937
|
+
console.error("[MSE] SourceBuffer error:", e);
|
|
938
|
+
}), e.event(c, "updateend", () => {
|
|
939
|
+
this.#t.set(q(c.buffered));
|
|
940
|
+
}), o.container.kind === "cmaf" ? this.#o(e, i, a, o, c, t) : this.#s(e, i, a, o, c, t);
|
|
941
|
+
}
|
|
942
|
+
async #a(e, t) {
|
|
943
|
+
for (; e.updating;) await new Promise((t) => e.addEventListener("updateend", t, { once: !0 }));
|
|
944
|
+
for (e.appendBuffer(t); e.updating;) await new Promise((t) => e.addEventListener("updateend", t, { once: !0 }));
|
|
945
|
+
}
|
|
946
|
+
#o(t, n, r, i, o, c) {
|
|
947
|
+
if (i.container.kind !== "cmaf") throw Error("unreachable");
|
|
948
|
+
let l = n.subscribe(r, a.PRIORITY.video);
|
|
949
|
+
t.cleanup(() => l.close());
|
|
950
|
+
let u = p(i.container.init), d = s.Cmaf.decodeInitSegment(u);
|
|
951
|
+
t.spawn(async () => {
|
|
952
|
+
for (await this.#a(o, u);;) {
|
|
953
|
+
let t = await l.readFrame();
|
|
954
|
+
if (!t) return;
|
|
955
|
+
let n = s.Cmaf.decodeTimestamp(t, d);
|
|
956
|
+
this.source.sync.received(e.Time.Milli.fromMicro(n), "video"), await this.#a(o, t), c.buffered.length > 0 && c.currentTime < c.buffered.start(0) && (c.currentTime = c.buffered.start(0));
|
|
957
|
+
}
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
#s(t, n, r, i, o, c) {
|
|
961
|
+
let l = n.subscribe(r, a.PRIORITY.video);
|
|
962
|
+
t.cleanup(() => l.close());
|
|
963
|
+
let u = i.container.kind === "loc" ? new s.Loc.Format() : new s.Legacy.Format(), d = new s.Consumer(l, {
|
|
964
|
+
format: u,
|
|
965
|
+
latency: this.source.sync.buffer
|
|
966
|
+
});
|
|
967
|
+
t.cleanup(() => d.close()), t.spawn(async () => {
|
|
968
|
+
let t = s.Cmaf.createVideoInitSegment(i);
|
|
969
|
+
await this.#a(o, t);
|
|
970
|
+
let n = 1, r, a;
|
|
971
|
+
for (;;) {
|
|
972
|
+
let t = await d.next();
|
|
973
|
+
if (!t) return;
|
|
974
|
+
if (!t.frame) continue;
|
|
975
|
+
a = t.frame;
|
|
976
|
+
let n = e.Time.Milli.fromMicro(a.timestamp);
|
|
977
|
+
this.source.sync.received(n, "video");
|
|
978
|
+
break;
|
|
979
|
+
}
|
|
980
|
+
for (;;) {
|
|
981
|
+
let t = await d.next();
|
|
982
|
+
if (t && !t.frame) continue;
|
|
983
|
+
let i = t?.frame;
|
|
984
|
+
if (i) {
|
|
985
|
+
r = e.Time.Micro.sub(i.timestamp, a.timestamp);
|
|
986
|
+
let t = e.Time.Milli.fromMicro(i.timestamp);
|
|
987
|
+
this.source.sync.received(t, "video");
|
|
988
|
+
}
|
|
989
|
+
let l = s.Cmaf.encodeDataSegment({
|
|
990
|
+
data: a.data,
|
|
991
|
+
timestamp: a.timestamp,
|
|
992
|
+
duration: r ?? 0,
|
|
993
|
+
keyframe: a.keyframe,
|
|
994
|
+
sequence: n++
|
|
995
|
+
});
|
|
996
|
+
if (await this.#a(o, l), c.buffered.length > 0 && c.currentTime < c.buffered.start(0) && (c.currentTime = c.buffered.start(0)), !i) return;
|
|
997
|
+
a = i;
|
|
998
|
+
}
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
#c(e) {
|
|
1002
|
+
let t = e.get(this.muxer.element);
|
|
1003
|
+
if (!t) return;
|
|
1004
|
+
let n = () => {
|
|
1005
|
+
this.#n.set(t.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA);
|
|
1006
|
+
};
|
|
1007
|
+
n(), e.event(t, "waiting", n), e.event(t, "playing", n), e.event(t, "seeking", n);
|
|
1008
|
+
}
|
|
1009
|
+
#l(t) {
|
|
1010
|
+
let n = t.get(this.muxer.element);
|
|
1011
|
+
if (n) if ("requestVideoFrameCallback" in n) {
|
|
1012
|
+
let r = n, i, a = () => {
|
|
1013
|
+
let t = e.Time.Milli.fromSecond(r.currentTime);
|
|
1014
|
+
this.#r.set(t), i = r.requestVideoFrameCallback(a);
|
|
1015
|
+
};
|
|
1016
|
+
i = r.requestVideoFrameCallback(a), t.cleanup(() => r.cancelVideoFrameCallback(i));
|
|
1017
|
+
} else t.event(n, "timeupdate", () => {
|
|
1018
|
+
let t = e.Time.Milli.fromSecond(n.currentTime);
|
|
1019
|
+
this.#r.set(t);
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
close() {
|
|
1023
|
+
this.source.close(), this.signals.close();
|
|
1024
|
+
}
|
|
1025
|
+
};
|
|
1026
|
+
async function z(e) {
|
|
1027
|
+
return MediaSource.isTypeSupported(`video/mp4; codecs="${e.codec}"`);
|
|
1028
|
+
}
|
|
1029
|
+
//#endregion
|
|
1030
|
+
//#region src/video/renderer.ts
|
|
1031
|
+
var B = class {
|
|
1032
|
+
decoder;
|
|
1033
|
+
canvas;
|
|
1034
|
+
paused;
|
|
1035
|
+
frame = new i(void 0);
|
|
1036
|
+
timestamp = new i(void 0);
|
|
1037
|
+
#e = new i(void 0);
|
|
1038
|
+
#t = new i(!1);
|
|
1039
|
+
#n = new r();
|
|
1040
|
+
constructor(e, t) {
|
|
1041
|
+
this.decoder = e, this.canvas = i.from(t?.canvas), this.paused = i.from(t?.paused ?? !1), this.#n.run((e) => {
|
|
1042
|
+
let t = e.get(this.canvas);
|
|
1043
|
+
this.#e.set(t?.getContext("2d") ?? void 0);
|
|
1044
|
+
}), this.#n.run(this.#i.bind(this)), this.#n.run(this.#a.bind(this)), this.#n.run(this.#o.bind(this)), this.#n.run(this.#r.bind(this));
|
|
1045
|
+
}
|
|
1046
|
+
#r(e) {
|
|
1047
|
+
let t = e.getAll([this.canvas, this.decoder.display]);
|
|
1048
|
+
if (!t) return;
|
|
1049
|
+
let [n, r] = t;
|
|
1050
|
+
(n.width !== r.width || n.height !== r.height) && (n.width = r.width, n.height = r.height);
|
|
1051
|
+
}
|
|
1052
|
+
#i(e) {
|
|
1053
|
+
let t = e.get(this.canvas);
|
|
1054
|
+
if (!t) {
|
|
1055
|
+
this.#t.set(!1);
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
let n = !1, r = () => {
|
|
1059
|
+
this.#t.set(n && !document.hidden);
|
|
1060
|
+
}, i = new IntersectionObserver((e) => {
|
|
1061
|
+
for (let t of e) n = t.isIntersecting, r();
|
|
1062
|
+
}, { threshold: .01 });
|
|
1063
|
+
e.event(document, "visibilitychange", r), i.observe(t), e.cleanup(() => i.disconnect()), e.cleanup(() => this.#t.set(!1));
|
|
1064
|
+
}
|
|
1065
|
+
#a(e) {
|
|
1066
|
+
let t = e.get(this.paused), n = e.get(this.#t);
|
|
1067
|
+
if (e.cleanup(() => this.decoder.enabled.set(!1)), !t) {
|
|
1068
|
+
this.decoder.enabled.set(n);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
let r = e.get(this.decoder.frame);
|
|
1072
|
+
this.decoder.enabled.set(!r);
|
|
1073
|
+
}
|
|
1074
|
+
#o(e) {
|
|
1075
|
+
let t = e.get(this.#e);
|
|
1076
|
+
if (!t) return;
|
|
1077
|
+
let r = e.get(this.decoder.frame), i = requestAnimationFrame(() => {
|
|
1078
|
+
this.#s(t, r), r ? (this.frame.update((e) => (e?.close(), r.clone())), this.timestamp.set(n.Milli.fromMicro(r.timestamp))) : (this.frame.update((e) => {
|
|
1079
|
+
e?.close();
|
|
1080
|
+
}), this.timestamp.set(void 0)), i = void 0;
|
|
1081
|
+
});
|
|
1082
|
+
e.cleanup(() => {
|
|
1083
|
+
i && cancelAnimationFrame(i);
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
#s(e, t) {
|
|
1087
|
+
if (!t) {
|
|
1088
|
+
e.fillStyle = "#000", e.fillRect(0, 0, e.canvas.width, e.canvas.height);
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
e.save(), e.fillStyle = "#000", e.fillRect(0, 0, e.canvas.width, e.canvas.height), this.decoder.source.catalog.peek()?.flip && (e.scale(-1, 1), e.translate(-e.canvas.width, 0)), e.drawImage(t, 0, 0, e.canvas.width, e.canvas.height), e.restore();
|
|
1092
|
+
}
|
|
1093
|
+
close() {
|
|
1094
|
+
this.frame.update((e) => {
|
|
1095
|
+
e?.close();
|
|
1096
|
+
}), this.timestamp.set(void 0), this.#n.close();
|
|
1097
|
+
}
|
|
1098
|
+
};
|
|
1099
|
+
//#endregion
|
|
1100
|
+
//#region src/video/source.ts
|
|
1101
|
+
function V(e) {
|
|
1102
|
+
return (t) => {
|
|
1103
|
+
let n = [], r = [];
|
|
1104
|
+
for (let [i, a] of t) if (a.codedWidth && a.codedHeight) {
|
|
1105
|
+
let t = a.codedWidth * a.codedHeight;
|
|
1106
|
+
t <= e ? n.push({
|
|
1107
|
+
name: i,
|
|
1108
|
+
size: t
|
|
1109
|
+
}) : r.push({
|
|
1110
|
+
name: i,
|
|
1111
|
+
size: t
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
return n.sort((e, t) => t.size - e.size), n.length > 0 ? n.map((e) => e.name) : r.length > 0 ? (r.sort((e, t) => e.size - t.size), [r[0].name]) : t.map(([e]) => e);
|
|
1115
|
+
};
|
|
1116
|
+
}
|
|
1117
|
+
function H(e, t) {
|
|
1118
|
+
return (n) => {
|
|
1119
|
+
let r = [], i = [];
|
|
1120
|
+
for (let [a, o] of n) {
|
|
1121
|
+
if (!o.codedWidth || !o.codedHeight) continue;
|
|
1122
|
+
let n = o.codedWidth * o.codedHeight, s = e == null || o.codedWidth <= e, c = t == null || o.codedHeight <= t;
|
|
1123
|
+
s && c ? r.push({
|
|
1124
|
+
name: a,
|
|
1125
|
+
size: n
|
|
1126
|
+
}) : i.push({
|
|
1127
|
+
name: a,
|
|
1128
|
+
size: n
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
return r.sort((e, t) => t.size - e.size), r.length > 0 ? r.map((e) => e.name) : i.length > 0 ? (i.sort((e, t) => e.size - t.size), [i[0].name]) : n.map(([e]) => e);
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
function U(e) {
|
|
1135
|
+
return (t) => {
|
|
1136
|
+
let n = [], r = [];
|
|
1137
|
+
for (let [i, a] of t) a.bitrate != null && a.bitrate <= e ? n.push({
|
|
1138
|
+
name: i,
|
|
1139
|
+
bitrate: a.bitrate
|
|
1140
|
+
}) : a.bitrate != null && r.push({
|
|
1141
|
+
name: i,
|
|
1142
|
+
bitrate: a.bitrate
|
|
1143
|
+
});
|
|
1144
|
+
return n.sort((e, t) => t.bitrate - e.bitrate), n.length > 0 ? n.map((e) => e.name) : r.length > 0 ? (r.sort((e, t) => e.bitrate - t.bitrate), [r[0].name]) : t.map(([e]) => e);
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
function W(e) {
|
|
1148
|
+
let t = e[0];
|
|
1149
|
+
for (let n of e) {
|
|
1150
|
+
let [, e] = n, [, r] = t, i = (e.codedWidth ?? 0) * (e.codedHeight ?? 0), a = (r.codedWidth ?? 0) * (r.codedHeight ?? 0);
|
|
1151
|
+
if (i !== a) {
|
|
1152
|
+
i > a && (t = n);
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
(e.bitrate ?? 0) > (r.bitrate ?? 0) && (t = n);
|
|
1156
|
+
}
|
|
1157
|
+
return t[0];
|
|
1158
|
+
}
|
|
1159
|
+
var G = class {
|
|
1160
|
+
broadcast;
|
|
1161
|
+
target;
|
|
1162
|
+
catalog;
|
|
1163
|
+
#e = new i({});
|
|
1164
|
+
available = this.#e;
|
|
1165
|
+
#t = new i(void 0);
|
|
1166
|
+
track = this.#t;
|
|
1167
|
+
#n = new i(void 0);
|
|
1168
|
+
config = this.#n;
|
|
1169
|
+
sync;
|
|
1170
|
+
supported;
|
|
1171
|
+
#r = new r();
|
|
1172
|
+
constructor(e, t) {
|
|
1173
|
+
this.broadcast = i.from(t?.broadcast), this.target = i.from(t?.target), this.sync = e, this.supported = i.from(t?.supported), this.catalog = this.#r.computed((e) => {
|
|
1174
|
+
let t = e.get(this.broadcast);
|
|
1175
|
+
return t ? e.get(t.catalog)?.video : void 0;
|
|
1176
|
+
}), this.#r.run(this.#i.bind(this)), this.#r.run(this.#a.bind(this));
|
|
1177
|
+
}
|
|
1178
|
+
#i(e) {
|
|
1179
|
+
let t = e.get(this.supported);
|
|
1180
|
+
if (!t) return;
|
|
1181
|
+
let n = e.get(this.catalog)?.renditions ?? {};
|
|
1182
|
+
e.spawn(async () => {
|
|
1183
|
+
let e = {};
|
|
1184
|
+
for (let [r, i] of Object.entries(n)) await t(i) && (e[r] = i);
|
|
1185
|
+
Object.keys(e).length === 0 && Object.keys(n).length > 0 && console.warn("[Source] No supported video renditions found:", n), this.#e.set(e);
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
#a(e) {
|
|
1189
|
+
let t = e.get(this.#e);
|
|
1190
|
+
if (Object.keys(t).length === 0) return;
|
|
1191
|
+
let n = e.get(this.target);
|
|
1192
|
+
if (n?.name && n.name in t) {
|
|
1193
|
+
let r = t[n.name];
|
|
1194
|
+
e.set(this.#t, n.name), e.set(this.#n, r), e.set(this.sync.video, r.jitter);
|
|
1195
|
+
return;
|
|
1196
|
+
}
|
|
1197
|
+
let r = n;
|
|
1198
|
+
if (!n?.bitrate) {
|
|
1199
|
+
let t = e.get(this.broadcast), i = (t ? e.get(t.connection) : void 0)?.recvBandwidth;
|
|
1200
|
+
if (i) {
|
|
1201
|
+
let t = e.get(i);
|
|
1202
|
+
if (t != null) {
|
|
1203
|
+
let e = Math.round(t * .8);
|
|
1204
|
+
r = {
|
|
1205
|
+
...n,
|
|
1206
|
+
bitrate: e
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
let i = this.#o(t, r);
|
|
1212
|
+
if (!i) return;
|
|
1213
|
+
let a = t[i];
|
|
1214
|
+
e.set(this.#t, i), e.set(this.#n, a);
|
|
1215
|
+
let o = a.jitter ?? (a.framerate ? Math.ceil(1e3 / a.framerate) : void 0);
|
|
1216
|
+
e.set(this.sync.video, o);
|
|
1217
|
+
}
|
|
1218
|
+
#o(e, t) {
|
|
1219
|
+
let n = Object.entries(e);
|
|
1220
|
+
if (n.length === 0) return;
|
|
1221
|
+
if (n.length === 1) return n[0][0];
|
|
1222
|
+
let r = [];
|
|
1223
|
+
if (t?.pixels != null && r.push(V(t.pixels)), (t?.width != null || t?.height != null) && r.push(H(t.width, t.height)), t?.bitrate != null && r.push(U(t.bitrate)), r.length === 0) return W(n);
|
|
1224
|
+
let i = r.map((e) => e(n)), a = i.map((e) => new Set(e));
|
|
1225
|
+
for (let e of i[0]) if (a.every((t) => t.has(e))) return e;
|
|
1226
|
+
console.warn("conflicting rendition filters, no rendition satisfies all criteria");
|
|
1227
|
+
}
|
|
1228
|
+
close() {
|
|
1229
|
+
this.#r.close();
|
|
1230
|
+
}
|
|
1231
|
+
}, K = /* @__PURE__ */ f({
|
|
1232
|
+
Decoder: () => F,
|
|
1233
|
+
Mse: () => R,
|
|
1234
|
+
Renderer: () => B,
|
|
1235
|
+
Source: () => G
|
|
1236
|
+
});
|
|
1237
|
+
//#endregion
|
|
1238
|
+
//#region src/backend.ts
|
|
1239
|
+
function q(t) {
|
|
1240
|
+
let n = [];
|
|
1241
|
+
for (let r = 0; r < t.length; r++) {
|
|
1242
|
+
let i = e.Time.Milli.fromSecond(t.start(r)), a = e.Time.Milli.fromSecond(t.end(r));
|
|
1243
|
+
n.push({
|
|
1244
|
+
start: i,
|
|
1245
|
+
end: a
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1248
|
+
return n;
|
|
1249
|
+
}
|
|
1250
|
+
var se = class {
|
|
1251
|
+
source;
|
|
1252
|
+
stats = new i(void 0);
|
|
1253
|
+
stalled = new i(!1);
|
|
1254
|
+
buffered = new i([]);
|
|
1255
|
+
timestamp = new i(e.Time.Milli.zero);
|
|
1256
|
+
constructor(e) {
|
|
1257
|
+
this.source = e;
|
|
1258
|
+
}
|
|
1259
|
+
}, ce = class {
|
|
1260
|
+
source;
|
|
1261
|
+
volume = new i(.5);
|
|
1262
|
+
muted = new i(!1);
|
|
1263
|
+
stats = new i(void 0);
|
|
1264
|
+
buffered = new i([]);
|
|
1265
|
+
context = new i(void 0);
|
|
1266
|
+
constructor(e) {
|
|
1267
|
+
this.source = e;
|
|
1268
|
+
}
|
|
1269
|
+
}, le = class {
|
|
1270
|
+
element = new i(void 0);
|
|
1271
|
+
broadcast;
|
|
1272
|
+
latency;
|
|
1273
|
+
jitter;
|
|
1274
|
+
paused;
|
|
1275
|
+
video;
|
|
1276
|
+
#e;
|
|
1277
|
+
audio;
|
|
1278
|
+
#t;
|
|
1279
|
+
sync;
|
|
1280
|
+
signals = new r();
|
|
1281
|
+
constructor(e) {
|
|
1282
|
+
this.element = i.from(e?.element), this.broadcast = i.from(e?.broadcast), this.sync = new N({
|
|
1283
|
+
latency: e?.latency,
|
|
1284
|
+
connection: e?.connection
|
|
1285
|
+
}), this.latency = this.sync.latency, this.jitter = this.sync.jitter, this.#e = new G(this.sync, { broadcast: this.broadcast }), this.#t = new Y(this.sync, { broadcast: this.broadcast }), this.video = new se(this.#e), this.audio = new ce(this.#t), this.paused = i.from(e?.paused ?? !1), this.signals.run(this.#n.bind(this));
|
|
1286
|
+
}
|
|
1287
|
+
#n(e) {
|
|
1288
|
+
let t = e.get(this.element);
|
|
1289
|
+
t && (t instanceof HTMLCanvasElement ? this.#r(e, t) : t instanceof HTMLVideoElement && this.#i(e, t));
|
|
1290
|
+
}
|
|
1291
|
+
#r(e, t) {
|
|
1292
|
+
let n = new F(this.#e), r = new E(this.#t), i = new A(r, {
|
|
1293
|
+
volume: this.audio.volume,
|
|
1294
|
+
muted: this.audio.muted,
|
|
1295
|
+
paused: this.paused
|
|
1296
|
+
}), a = new B(n, {
|
|
1297
|
+
canvas: t,
|
|
1298
|
+
paused: this.paused
|
|
1299
|
+
});
|
|
1300
|
+
e.cleanup(() => {
|
|
1301
|
+
n.close(), r.close(), i.close(), a.close();
|
|
1302
|
+
}), e.proxy(this.video.stats, n.stats), e.proxy(this.video.buffered, n.buffered), e.proxy(this.video.stalled, n.stalled), e.proxy(this.video.timestamp, n.timestamp), e.proxy(this.audio.stats, r.stats), e.proxy(this.audio.buffered, r.buffered), e.proxy(this.audio.context, r.context);
|
|
1303
|
+
}
|
|
1304
|
+
#i(e, t) {
|
|
1305
|
+
let n = new j(this.sync, {
|
|
1306
|
+
paused: this.paused,
|
|
1307
|
+
element: t
|
|
1308
|
+
}), r = new R(n, this.#e), i = new J(n, this.#t, {
|
|
1309
|
+
volume: this.audio.volume,
|
|
1310
|
+
muted: this.audio.muted
|
|
1311
|
+
});
|
|
1312
|
+
e.cleanup(() => {
|
|
1313
|
+
r.close(), i.close(), n.close();
|
|
1314
|
+
}), e.proxy(this.video.stats, r.stats), e.proxy(this.video.buffered, r.buffered), e.proxy(this.video.stalled, r.stalled), e.proxy(this.video.timestamp, r.timestamp), e.proxy(this.audio.stats, i.stats), e.proxy(this.audio.buffered, i.buffered), e.proxy(this.audio.context, i.context);
|
|
1315
|
+
}
|
|
1316
|
+
close() {
|
|
1317
|
+
this.signals.close(), this.#e.close(), this.#t.close(), this.sync.close();
|
|
1318
|
+
}
|
|
1319
|
+
}, J = class {
|
|
1320
|
+
muxer;
|
|
1321
|
+
source;
|
|
1322
|
+
volume;
|
|
1323
|
+
muted;
|
|
1324
|
+
#e = new i(void 0);
|
|
1325
|
+
stats = this.#e;
|
|
1326
|
+
#t = new i([]);
|
|
1327
|
+
buffered = this.#t;
|
|
1328
|
+
context = new i(void 0);
|
|
1329
|
+
#n = new r();
|
|
1330
|
+
constructor(e, t, n) {
|
|
1331
|
+
this.muxer = e, this.source = t, this.source.supported.set(ue), this.volume = i.from(n?.volume ?? .5), this.muted = i.from(n?.muted ?? !1), this.#n.run(this.#r.bind(this)), this.#n.run(this.#s.bind(this));
|
|
1332
|
+
}
|
|
1333
|
+
#r(e) {
|
|
1334
|
+
let t = e.get(this.muxer.element);
|
|
1335
|
+
if (!t) return;
|
|
1336
|
+
let n = e.get(this.muxer.mediaSource);
|
|
1337
|
+
if (!n) return;
|
|
1338
|
+
let r = e.get(this.source.broadcast);
|
|
1339
|
+
if (!r) return;
|
|
1340
|
+
let i = e.get(r.active);
|
|
1341
|
+
if (!i) return;
|
|
1342
|
+
let o = e.get(this.source.track);
|
|
1343
|
+
if (!o) return;
|
|
1344
|
+
let s = e.get(this.source.config);
|
|
1345
|
+
if (!s) return;
|
|
1346
|
+
let c = `audio/mp4; codecs="${s.codec}"`, l = n.addSourceBuffer(c);
|
|
1347
|
+
e.cleanup(() => {
|
|
1348
|
+
n.removeSourceBuffer(l), l.abort();
|
|
1349
|
+
}), e.event(l, "error", (e) => {
|
|
1350
|
+
console.error("[MSE] SourceBuffer error:", e);
|
|
1351
|
+
}), e.event(l, "updateend", () => {
|
|
1352
|
+
this.#t.set(q(l.buffered));
|
|
1353
|
+
});
|
|
1354
|
+
let u = i.subscribe(o, a.PRIORITY.audio);
|
|
1355
|
+
e.cleanup(() => u.close()), s.container.kind === "cmaf" ? this.#a(e, u, s, l, t) : this.#o(e, u, s, l, t);
|
|
1356
|
+
}
|
|
1357
|
+
async #i(e, t) {
|
|
1358
|
+
for (; e.updating;) await new Promise((t) => e.addEventListener("updateend", t, { once: !0 }));
|
|
1359
|
+
for (e.appendBuffer(t); e.updating;) await new Promise((t) => e.addEventListener("updateend", t, { once: !0 }));
|
|
1360
|
+
}
|
|
1361
|
+
#a(t, n, r, i, a) {
|
|
1362
|
+
if (r.container.kind !== "cmaf") throw Error("unreachable");
|
|
1363
|
+
let o = p(r.container.init), c = s.Cmaf.decodeInitSegment(o);
|
|
1364
|
+
t.spawn(async () => {
|
|
1365
|
+
for (await this.#i(i, o);;) {
|
|
1366
|
+
let t = await n.readFrame();
|
|
1367
|
+
if (!t) return;
|
|
1368
|
+
let r = s.Cmaf.decodeTimestamp(t, c);
|
|
1369
|
+
this.source.sync.received(e.Time.Milli.fromMicro(r), "audio"), await this.#i(i, t), a.buffered.length > 0 && a.currentTime < a.buffered.start(0) && (a.currentTime = a.buffered.start(0));
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
#o(t, n, r, i, a) {
|
|
1374
|
+
let o = r.container.kind === "loc" ? new s.Loc.Format() : new s.Legacy.Format(), c = new s.Consumer(n, {
|
|
1375
|
+
format: o,
|
|
1376
|
+
latency: this.source.sync.buffer
|
|
1377
|
+
});
|
|
1378
|
+
t.cleanup(() => c.close()), t.spawn(async () => {
|
|
1379
|
+
let t = s.Cmaf.createAudioInitSegment(r);
|
|
1380
|
+
await this.#i(i, t);
|
|
1381
|
+
let n = 1, o, l;
|
|
1382
|
+
for (;;) {
|
|
1383
|
+
let t = await c.next();
|
|
1384
|
+
if (!t) return;
|
|
1385
|
+
if (!t.frame) continue;
|
|
1386
|
+
l = t.frame;
|
|
1387
|
+
let n = e.Time.Milli.fromMicro(l.timestamp);
|
|
1388
|
+
this.source.sync.received(n, "audio");
|
|
1389
|
+
break;
|
|
1390
|
+
}
|
|
1391
|
+
for (;;) {
|
|
1392
|
+
let t = await c.next();
|
|
1393
|
+
if (t && !t.frame) continue;
|
|
1394
|
+
let r = t?.frame;
|
|
1395
|
+
if (r) {
|
|
1396
|
+
o = e.Time.Micro.sub(r.timestamp, l.timestamp);
|
|
1397
|
+
let t = e.Time.Milli.fromMicro(r.timestamp);
|
|
1398
|
+
this.source.sync.received(t, "audio");
|
|
1399
|
+
}
|
|
1400
|
+
let u = s.Cmaf.encodeDataSegment({
|
|
1401
|
+
data: l.data,
|
|
1402
|
+
timestamp: l.timestamp,
|
|
1403
|
+
duration: o ?? 0,
|
|
1404
|
+
keyframe: l.keyframe,
|
|
1405
|
+
sequence: n++
|
|
1406
|
+
});
|
|
1407
|
+
if (await this.#i(i, u), a.buffered.length > 0 && a.currentTime < a.buffered.start(0) && (a.currentTime = a.buffered.start(0)), !r) return;
|
|
1408
|
+
l = r;
|
|
1409
|
+
}
|
|
1410
|
+
});
|
|
1411
|
+
}
|
|
1412
|
+
#s(e) {
|
|
1413
|
+
let t = e.get(this.muxer.element);
|
|
1414
|
+
if (!t) return;
|
|
1415
|
+
let n = e.get(this.volume), r = e.get(this.muted);
|
|
1416
|
+
r && !t.muted ? t.muted = !0 : !r && t.muted && (t.muted = !1), n !== t.volume && (t.volume = n), e.event(t, "volumechange", () => {
|
|
1417
|
+
this.volume.set(t.volume);
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
close() {
|
|
1421
|
+
this.#n.close();
|
|
1422
|
+
}
|
|
1423
|
+
};
|
|
1424
|
+
async function ue(e) {
|
|
1425
|
+
return MediaSource.isTypeSupported(`audio/mp4; codecs="${e.codec}"`);
|
|
1426
|
+
}
|
|
1427
|
+
//#endregion
|
|
1428
|
+
//#region src/audio/source.ts
|
|
1429
|
+
var de = 128, Y = class {
|
|
1430
|
+
broadcast;
|
|
1431
|
+
target;
|
|
1432
|
+
catalog;
|
|
1433
|
+
#e = new i({});
|
|
1434
|
+
available = this.#e;
|
|
1435
|
+
#t = new i(void 0);
|
|
1436
|
+
track = this.#t;
|
|
1437
|
+
#n = new i(void 0);
|
|
1438
|
+
config = this.#n;
|
|
1439
|
+
supported;
|
|
1440
|
+
sync;
|
|
1441
|
+
#r = new r();
|
|
1442
|
+
constructor(e, t) {
|
|
1443
|
+
this.sync = e, this.broadcast = i.from(t?.broadcast), this.target = i.from(t?.target), this.supported = i.from(t?.supported), this.catalog = this.#r.computed((e) => {
|
|
1444
|
+
let t = e.get(this.broadcast);
|
|
1445
|
+
return t ? e.get(t.catalog)?.audio : void 0;
|
|
1446
|
+
}), this.#r.run(this.#i.bind(this)), this.#r.run(this.#a.bind(this));
|
|
1447
|
+
}
|
|
1448
|
+
#i(e) {
|
|
1449
|
+
let t = e.get(this.catalog)?.renditions ?? {}, n = e.get(this.supported);
|
|
1450
|
+
n && e.spawn(async () => {
|
|
1451
|
+
let e = {};
|
|
1452
|
+
for (let [r, i] of Object.entries(t)) await n(i) && (e[r] = i);
|
|
1453
|
+
Object.keys(e).length === 0 && Object.keys(t).length > 0 && console.warn("no supported audio renditions found:", t), this.#e.set(e);
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
#a(e) {
|
|
1457
|
+
let t = e.get(this.#e);
|
|
1458
|
+
if (Object.keys(t).length === 0) return;
|
|
1459
|
+
let n = e.get(this.target), r;
|
|
1460
|
+
if (n?.name && n.name in t) r = {
|
|
1461
|
+
track: n.name,
|
|
1462
|
+
config: t[n.name]
|
|
1463
|
+
};
|
|
1464
|
+
else if (r = this.#o(t), !r) return;
|
|
1465
|
+
e.set(this.#t, r.track), e.set(this.#n, r.config);
|
|
1466
|
+
let i = (r.config.jitter ?? fe(r.config) ?? 0) + Math.ceil(de / r.config.sampleRate * 1e3);
|
|
1467
|
+
e.set(this.sync.audio, i);
|
|
1468
|
+
}
|
|
1469
|
+
#o(e) {
|
|
1470
|
+
let t = Object.entries(e);
|
|
1471
|
+
if (t.length !== 0) {
|
|
1472
|
+
for (let [e, n] of t) if (n.container.kind === "legacy") return {
|
|
1473
|
+
track: e,
|
|
1474
|
+
config: n
|
|
1475
|
+
};
|
|
1476
|
+
for (let [e, n] of t) if (n.container.kind === "loc") return {
|
|
1477
|
+
track: e,
|
|
1478
|
+
config: n
|
|
1479
|
+
};
|
|
1480
|
+
for (let [e, n] of t) if (n.container.kind === "cmaf") return {
|
|
1481
|
+
track: e,
|
|
1482
|
+
config: n
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
close() {
|
|
1487
|
+
this.#r.close();
|
|
1488
|
+
}
|
|
1489
|
+
};
|
|
1490
|
+
function fe(e) {
|
|
1491
|
+
if (e.codec.startsWith("opus")) return 20;
|
|
1492
|
+
if (e.codec.startsWith("mp4a")) return Math.ceil(1024 / e.sampleRate * 1e3);
|
|
1493
|
+
}
|
|
1494
|
+
//#endregion
|
|
1495
|
+
//#region src/audio/index.ts
|
|
1496
|
+
var pe = /* @__PURE__ */ f({
|
|
1497
|
+
Decoder: () => E,
|
|
1498
|
+
Emitter: () => A,
|
|
1499
|
+
Mse: () => J,
|
|
1500
|
+
Source: () => Y
|
|
1501
|
+
}), me = 48e3, X = 2;
|
|
1502
|
+
function he(e) {
|
|
1503
|
+
let t = "";
|
|
1504
|
+
for (let n = 0; n < e.length; n++) t += e[n].toString(16).padStart(2, "0");
|
|
1505
|
+
return t;
|
|
1506
|
+
}
|
|
1507
|
+
function Z(e) {
|
|
1508
|
+
let t;
|
|
1509
|
+
try {
|
|
1510
|
+
t = e.initData ? p(e.initData) : void 0;
|
|
1511
|
+
} catch {
|
|
1512
|
+
t = void 0;
|
|
1513
|
+
}
|
|
1514
|
+
return e.packaging === "cmaf" && e.initData && t ? {
|
|
1515
|
+
container: {
|
|
1516
|
+
kind: "cmaf",
|
|
1517
|
+
init: e.initData
|
|
1518
|
+
},
|
|
1519
|
+
description: void 0
|
|
1520
|
+
} : {
|
|
1521
|
+
container: { kind: "legacy" },
|
|
1522
|
+
description: t ? he(t) : void 0
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
function ge(e) {
|
|
1526
|
+
if (!e.codec) return;
|
|
1527
|
+
let { container: t, description: n } = Z(e);
|
|
1528
|
+
return {
|
|
1529
|
+
codec: e.codec,
|
|
1530
|
+
container: t,
|
|
1531
|
+
description: n,
|
|
1532
|
+
codedWidth: e.width == null ? void 0 : o(e.width),
|
|
1533
|
+
codedHeight: e.height == null ? void 0 : o(e.height),
|
|
1534
|
+
framerate: e.framerate,
|
|
1535
|
+
bitrate: e.bitrate == null ? void 0 : o(e.bitrate),
|
|
1536
|
+
jitter: e.jitter == null ? void 0 : o(e.jitter)
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1539
|
+
function _e(e) {
|
|
1540
|
+
if (!e.codec) return;
|
|
1541
|
+
let t = (() => {
|
|
1542
|
+
if (!e.channelConfig) return X;
|
|
1543
|
+
let t = Number.parseInt(e.channelConfig, 10);
|
|
1544
|
+
return Number.isFinite(t) ? t : X;
|
|
1545
|
+
})(), { container: n, description: r } = Z(e);
|
|
1546
|
+
return {
|
|
1547
|
+
codec: e.codec,
|
|
1548
|
+
container: n,
|
|
1549
|
+
description: r,
|
|
1550
|
+
sampleRate: o(e.samplerate ?? me),
|
|
1551
|
+
numberOfChannels: o(t),
|
|
1552
|
+
bitrate: e.bitrate == null ? void 0 : o(e.bitrate),
|
|
1553
|
+
jitter: e.jitter == null ? void 0 : o(e.jitter)
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
function Q(e) {
|
|
1557
|
+
let t = {}, n = {};
|
|
1558
|
+
for (let r of e.tracks) if (r.role === "video") {
|
|
1559
|
+
let e = ge(r);
|
|
1560
|
+
e && (t[r.name] = e);
|
|
1561
|
+
} else if (r.role === "audio") {
|
|
1562
|
+
let e = _e(r);
|
|
1563
|
+
e && (n[r.name] = e);
|
|
1564
|
+
}
|
|
1565
|
+
let r = {};
|
|
1566
|
+
return Object.keys(t).length > 0 && (r.video = { renditions: t }), Object.keys(n).length > 0 && (r.audio = { renditions: n }), r;
|
|
1567
|
+
}
|
|
1568
|
+
//#endregion
|
|
1569
|
+
//#region src/broadcast.ts
|
|
1570
|
+
var $ = [...a.FORMATS, "manual"];
|
|
1571
|
+
function ve(e) {
|
|
1572
|
+
if (e !== null) return $.find((t) => t === e);
|
|
1573
|
+
}
|
|
1574
|
+
var ye = class {
|
|
1575
|
+
connection;
|
|
1576
|
+
enabled;
|
|
1577
|
+
name;
|
|
1578
|
+
status = new i("offline");
|
|
1579
|
+
reload;
|
|
1580
|
+
catalogFormat;
|
|
1581
|
+
#e = new i(void 0);
|
|
1582
|
+
active = this.#e;
|
|
1583
|
+
catalog;
|
|
1584
|
+
#t;
|
|
1585
|
+
#n = new i(!1);
|
|
1586
|
+
signals = new r();
|
|
1587
|
+
constructor(e) {
|
|
1588
|
+
this.connection = i.from(e?.connection), this.name = i.from(e?.name ?? t.empty()), this.enabled = i.from(e?.enabled ?? !1), this.reload = i.from(e?.reload ?? !1), this.catalogFormat = i.from(e?.catalogFormat), this.catalog = i.from(e?.catalog), this.#t = e?.announced ?? new i(/* @__PURE__ */ new Set()), this.signals.run(this.#r.bind(this)), this.signals.run(this.#i.bind(this)), this.signals.run(this.#a.bind(this));
|
|
1589
|
+
}
|
|
1590
|
+
#r(e) {
|
|
1591
|
+
if (!e.get(this.reload)) {
|
|
1592
|
+
this.#n.set(!0);
|
|
1593
|
+
return;
|
|
1594
|
+
}
|
|
1595
|
+
if (e.get(this.connection)?.url.hostname.endsWith("mediaoverquic.com")) {
|
|
1596
|
+
console.warn("Cloudflare relay does not support broadcast discovery yet; ignoring reload signal."), this.#n.set(!0);
|
|
1597
|
+
return;
|
|
1598
|
+
}
|
|
1599
|
+
let t = e.get(this.name), n = e.get(this.#t);
|
|
1600
|
+
this.#n.set(n.has(t));
|
|
1601
|
+
}
|
|
1602
|
+
#i(e) {
|
|
1603
|
+
if (!e.get(this.enabled) || !e.get(this.#n)) return;
|
|
1604
|
+
let t = e.get(this.connection);
|
|
1605
|
+
if (!t) return;
|
|
1606
|
+
let n = e.get(this.name), r = t.consume(n);
|
|
1607
|
+
e.cleanup(() => r.close()), e.set(this.#e, r, void 0);
|
|
1608
|
+
}
|
|
1609
|
+
#a(e) {
|
|
1610
|
+
if (!e.get(this.enabled)) return;
|
|
1611
|
+
let t = e.get(this.catalogFormat), n = e.get(this.name), r = t ?? a.detectFormat(n) ?? a.DEFAULT_FORMAT;
|
|
1612
|
+
if (r === "manual") {
|
|
1613
|
+
let t = e.get(this.catalog);
|
|
1614
|
+
this.status.set(t ? "live" : "loading");
|
|
1615
|
+
return;
|
|
1616
|
+
}
|
|
1617
|
+
let i = e.get(this.active);
|
|
1618
|
+
if (!i) return;
|
|
1619
|
+
this.status.set("loading");
|
|
1620
|
+
let o = r === "hang" ? "catalog.json" : "catalog", s = i.subscribe(o, a.PRIORITY.catalog);
|
|
1621
|
+
e.cleanup(() => s.close());
|
|
1622
|
+
let c;
|
|
1623
|
+
if (r === "hang") {
|
|
1624
|
+
let e = new l.Consumer(s, { schema: a.RootSchema });
|
|
1625
|
+
c = () => e.next();
|
|
1626
|
+
} else c = async () => {
|
|
1627
|
+
let e = await u.fetch(s);
|
|
1628
|
+
return e ? Q(e) : void 0;
|
|
1629
|
+
};
|
|
1630
|
+
e.spawn(async () => {
|
|
1631
|
+
try {
|
|
1632
|
+
for (;;) {
|
|
1633
|
+
let t = await Promise.race([e.cancel, c()]);
|
|
1634
|
+
if (!t) break;
|
|
1635
|
+
console.debug("received catalog", r, this.name.peek(), t), this.catalog.set(t), this.status.set("live");
|
|
1636
|
+
}
|
|
1637
|
+
} catch (e) {
|
|
1638
|
+
console.warn("error fetching catalog", this.name.peek(), e);
|
|
1639
|
+
} finally {
|
|
1640
|
+
this.catalog.set(void 0), this.status.set("offline");
|
|
1641
|
+
}
|
|
1642
|
+
});
|
|
1643
|
+
}
|
|
1644
|
+
close() {
|
|
1645
|
+
this.signals.close();
|
|
1646
|
+
}
|
|
1647
|
+
};
|
|
1648
|
+
//#endregion
|
|
1649
|
+
export { le as a, N as c, pe as i, ie as l, $ as n, q as o, ve as r, K as s, ye as t };
|
|
1650
|
+
|
|
1651
|
+
//# sourceMappingURL=broadcast-CG8jZRfA.js.map
|