@norskvideo/moq-watch 0.1.2 → 0.1.4

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