@camstack/addon-post-analysis 1.2.51 → 1.2.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{dist-CnSzKo7x.js → dist-DGdY1GSf.js} +1466 -1655
- package/dist/{dist-DTeVOZBt.mjs → dist-Da97q0x6.mjs} +1479 -1657
- package/dist/embedding-encoder/index.js +1 -1
- package/dist/embedding-encoder/index.mjs +1 -1
- package/dist/node-Co0YpEFr.mjs +148 -0
- package/dist/node-CpXZkQ6V.js +151 -0
- package/dist/pipeline-analytics/_stub.js +1 -1
- package/dist/pipeline-analytics/{_virtual_mf-localSharedImportMap___mfe_internal__addon_pipeline_analytics_widgets-D-6lLgLp.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_pipeline_analytics_widgets-tCAuhv5m.mjs} +2 -2
- package/dist/pipeline-analytics/_virtual_mf___mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-DcqnDg2z.mjs +26 -0
- package/dist/pipeline-analytics/{hostInit-DPOdauxs.mjs → hostInit-CyLkuQ7j.mjs} +2 -2
- package/dist/pipeline-analytics/index.js +2509 -1093
- package/dist/pipeline-analytics/index.mjs +2499 -1091
- package/dist/pipeline-analytics/remoteEntry.js +1 -1
- package/package.json +1 -1
- package/dist/node-CnEKzHgS.js +0 -653
- package/dist/node-pIpcQ_aN.mjs +0 -649
- package/dist/pipeline-analytics/_virtual_mf___mfe_internal__addon_pipeline_analytics_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-JSo2V9IT.mjs +0 -26
|
@@ -30,7 +30,7 @@ async function d(e) {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
async function f() {
|
|
33
|
-
return l ||= d(() => import("./_virtual_mf-localSharedImportMap___mfe_internal__addon_pipeline_analytics_widgets-
|
|
33
|
+
return l ||= d(() => import("./_virtual_mf-localSharedImportMap___mfe_internal__addon_pipeline_analytics_widgets-tCAuhv5m.mjs")).catch((e) => {
|
|
34
34
|
throw l = void 0, e;
|
|
35
35
|
}), l;
|
|
36
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@camstack/addon-post-analysis",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.53",
|
|
4
4
|
"description": "Post-Analysis bundle — enrichment, embedding-encoder, pipeline-analytics. Multi-entry npm package shipping addons that consume pipeline output.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"camstack",
|
package/dist/node-CnEKzHgS.js
DELETED
|
@@ -1,653 +0,0 @@
|
|
|
1
|
-
const require_dist = require("./dist-CnSzKo7x.js");
|
|
2
|
-
let node_fs = require("node:fs");
|
|
3
|
-
node_fs = require_dist.__toESM(node_fs, 1);
|
|
4
|
-
let node_path = require("node:path");
|
|
5
|
-
node_path = require_dist.__toESM(node_path, 1);
|
|
6
|
-
let node_crypto = require("node:crypto");
|
|
7
|
-
require("node:child_process");
|
|
8
|
-
var STORAGE_LOCATION_TYPES = [
|
|
9
|
-
"data",
|
|
10
|
-
"media",
|
|
11
|
-
"recordings",
|
|
12
|
-
"recordings-high",
|
|
13
|
-
"recordings-low",
|
|
14
|
-
"recordings-clips",
|
|
15
|
-
"event-images",
|
|
16
|
-
"models",
|
|
17
|
-
"addons-data",
|
|
18
|
-
"cache",
|
|
19
|
-
"logs",
|
|
20
|
-
"backups"
|
|
21
|
-
];
|
|
22
|
-
var DEFAULT_LOCATION_SUBDIRS = {
|
|
23
|
-
data: "db",
|
|
24
|
-
media: "media",
|
|
25
|
-
recordings: "recordings",
|
|
26
|
-
"recordings-high": "recordings-high",
|
|
27
|
-
"recordings-low": "recordings-low",
|
|
28
|
-
"recordings-clips": "recordings-clips",
|
|
29
|
-
"event-images": "event-images",
|
|
30
|
-
models: "models",
|
|
31
|
-
"addons-data": "addons-data",
|
|
32
|
-
cache: "/tmp/camstack-cache",
|
|
33
|
-
logs: "logs",
|
|
34
|
-
backups: "backups"
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* Filesystem storage provider — serves all location types from a local directory tree.
|
|
38
|
-
*
|
|
39
|
-
* Default layout:
|
|
40
|
-
* {rootPath}/recordings-high/
|
|
41
|
-
* {rootPath}/recordings-low/
|
|
42
|
-
* {rootPath}/recordings-clips/
|
|
43
|
-
* {rootPath}/event-images/
|
|
44
|
-
* {rootPath}/models/
|
|
45
|
-
* {rootPath}/addons-data/
|
|
46
|
-
* {rootPath}/logs/
|
|
47
|
-
* /tmp/camstack-cache/ (cache is always local)
|
|
48
|
-
*
|
|
49
|
-
* Individual location paths can be overridden.
|
|
50
|
-
*/
|
|
51
|
-
var FilesystemStorageProvider = class {
|
|
52
|
-
id = "local";
|
|
53
|
-
name = "Local Filesystem";
|
|
54
|
-
supportedLocations = [...STORAGE_LOCATION_TYPES];
|
|
55
|
-
rootPath;
|
|
56
|
-
locationPaths;
|
|
57
|
-
constructor(rootPath, overrides) {
|
|
58
|
-
this.rootPath = node_path.resolve(rootPath);
|
|
59
|
-
this.locationPaths = /* @__PURE__ */ new Map();
|
|
60
|
-
for (const loc of STORAGE_LOCATION_TYPES) {
|
|
61
|
-
const override = overrides?.[loc];
|
|
62
|
-
if (override) this.locationPaths.set(loc, node_path.resolve(override));
|
|
63
|
-
else {
|
|
64
|
-
const subdir = DEFAULT_LOCATION_SUBDIRS[loc] ?? loc;
|
|
65
|
-
this.locationPaths.set(loc, node_path.isAbsolute(subdir) ? subdir : node_path.join(this.rootPath, subdir));
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
for (const [loc, override] of Object.entries(overrides ?? {})) if (typeof override === "string" && !this.locationPaths.has(loc)) this.locationPaths.set(loc, node_path.resolve(override));
|
|
69
|
-
}
|
|
70
|
-
async resolve({ location, relativePath }) {
|
|
71
|
-
const base = this.locationPaths.get(location) ?? node_path.join(this.rootPath, location);
|
|
72
|
-
return node_path.join(base, relativePath);
|
|
73
|
-
}
|
|
74
|
-
async write({ location, relativePath, data }) {
|
|
75
|
-
const filePath = await this.resolve({
|
|
76
|
-
location,
|
|
77
|
-
relativePath
|
|
78
|
-
});
|
|
79
|
-
await node_fs.promises.mkdir(node_path.dirname(filePath), { recursive: true });
|
|
80
|
-
if (Buffer.isBuffer(data)) await node_fs.promises.writeFile(filePath, data);
|
|
81
|
-
else {
|
|
82
|
-
const writeStream = node_fs.createWriteStream(filePath);
|
|
83
|
-
await new Promise((resolve, reject) => {
|
|
84
|
-
data.pipe(writeStream);
|
|
85
|
-
writeStream.on("finish", resolve);
|
|
86
|
-
writeStream.on("error", reject);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async read({ location, relativePath }) {
|
|
91
|
-
return node_fs.promises.readFile(await this.resolve({
|
|
92
|
-
location,
|
|
93
|
-
relativePath
|
|
94
|
-
}));
|
|
95
|
-
}
|
|
96
|
-
async exists({ location, relativePath }) {
|
|
97
|
-
try {
|
|
98
|
-
await node_fs.promises.access(await this.resolve({
|
|
99
|
-
location,
|
|
100
|
-
relativePath
|
|
101
|
-
}));
|
|
102
|
-
return true;
|
|
103
|
-
} catch {
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
async list({ location, prefix }) {
|
|
108
|
-
const base = this.locationPaths.get(location);
|
|
109
|
-
if (!base) return [];
|
|
110
|
-
const dir = prefix ? node_path.join(base, prefix) : base;
|
|
111
|
-
try {
|
|
112
|
-
return (await node_fs.promises.readdir(dir, { withFileTypes: true })).map((e) => prefix ? `${prefix}/${e.name}` : e.name);
|
|
113
|
-
} catch {
|
|
114
|
-
return [];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
async delete({ location, relativePath }) {
|
|
118
|
-
const filePath = await this.resolve({
|
|
119
|
-
location,
|
|
120
|
-
relativePath
|
|
121
|
-
});
|
|
122
|
-
await node_fs.promises.rm(filePath, { force: true });
|
|
123
|
-
}
|
|
124
|
-
async getAvailableSpace({ location }) {
|
|
125
|
-
const base = this.locationPaths.get(location);
|
|
126
|
-
if (!base) return null;
|
|
127
|
-
try {
|
|
128
|
-
let target = base;
|
|
129
|
-
while (!node_fs.existsSync(target)) {
|
|
130
|
-
const parent = node_path.dirname(target);
|
|
131
|
-
if (!parent || parent === target) return null;
|
|
132
|
-
target = parent;
|
|
133
|
-
}
|
|
134
|
-
const stats = await node_fs.promises.statfs(target);
|
|
135
|
-
return stats.bavail * stats.bsize;
|
|
136
|
-
} catch {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
/** Get the resolved path for a location type (addon-declared ids fall back
|
|
141
|
-
* to `<rootPath>/<id>`). */
|
|
142
|
-
getLocationPath(location) {
|
|
143
|
-
return this.locationPaths.get(location) ?? node_path.join(this.rootPath, location);
|
|
144
|
-
}
|
|
145
|
-
/** Get the root path */
|
|
146
|
-
getRootPath() {
|
|
147
|
-
return this.rootPath;
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
/**
|
|
151
|
-
* Fmp4FragmentPlane — a SUBSCRIBABLE fragmented-MP4 plane, fed by one
|
|
152
|
-
* {@link import('./fmp4-box-splitter.js').Fmp4BoxSplitter}.
|
|
153
|
-
*
|
|
154
|
-
* ## Why a plane and not a callback
|
|
155
|
-
*
|
|
156
|
-
* The operator's requirement for HKSV was explicit: the live fMP4 source built
|
|
157
|
-
* for it must be **dual-use**, so a HomeKit-triggered recording also lands in
|
|
158
|
-
* CamStack as an additional videoclip source alongside the recorder and the NC
|
|
159
|
-
* clip ring — *one fragmenter, two consumers; do not build an HKSV-only pipe*
|
|
160
|
-
* (`docs/roadmap.md` item 4b). A single-callback pipe makes the second consumer
|
|
161
|
-
* a second ffmpeg child of the same camera. So this is the same shape the
|
|
162
|
-
* broker's other multi-consumer surfaces already have
|
|
163
|
-
* (`AudioChunkPlane`, the push packet plane): N independent subscriptions over
|
|
164
|
-
* one producer.
|
|
165
|
-
*
|
|
166
|
-
* **Nothing consumes it yet.** Phase 4 brings the HKSV delegate and phase 4b the
|
|
167
|
-
* clip source; both are named here so the seam is not re-invented, and neither
|
|
168
|
-
* is built.
|
|
169
|
-
*
|
|
170
|
-
* ## The init segment is RETAINED
|
|
171
|
-
*
|
|
172
|
-
* A subscriber that attaches mid-stream — the clip consumer joining an already
|
|
173
|
-
* running HKSV session, which is the whole dual-use case — receives the
|
|
174
|
-
* retained `ftyp`+`moov` as its first packet and then live fragments. Without
|
|
175
|
-
* retention its fragments are undecodable and the failure looks like a codec
|
|
176
|
-
* problem.
|
|
177
|
-
*
|
|
178
|
-
* ## A slow subscriber is CLOSED, never silently gapped
|
|
179
|
-
*
|
|
180
|
-
* `AudioChunkPlane` drops its oldest chunk on overflow, which for audio costs a
|
|
181
|
-
* click. An fMP4 stream with a hole is not a shorter clip, it is a corrupt one:
|
|
182
|
-
* `moof` sequence numbers jump, the consumer's demuxer desynchronises, and HKSV
|
|
183
|
-
* shows a clip that fails to play with nothing anywhere saying why. So a
|
|
184
|
-
* subscription whose queue overflows is ENDED with a reason, loudly, and the
|
|
185
|
-
* other subscriptions are untouched.
|
|
186
|
-
*
|
|
187
|
-
* ## The PREBUFFER (phase 3)
|
|
188
|
-
*
|
|
189
|
-
* HKSV asks for context BEFORE the trigger — `CameraRecordingOptions.prebufferLength`
|
|
190
|
-
* is a HAP-mandated minimum of 4000 ms — and a subscriber that attaches at the
|
|
191
|
-
* motion edge has none. So the plane optionally retains the last few fragments
|
|
192
|
-
* and replays them to a subscriber that asks for them.
|
|
193
|
-
*
|
|
194
|
-
* Three things this ring gets right, each of which is a measured fact rather
|
|
195
|
-
* than a preference (see [D84](../../../../docs/decisions/adr-0084.md)):
|
|
196
|
-
*
|
|
197
|
-
* - **It is bounded by TIME *and* BYTES.** On the live fleet a 720p copy
|
|
198
|
-
* fragment is ~255 KB and a 4K one is ~6.35 MB — a 25× spread over the same
|
|
199
|
-
* window. A time-only bound is a per-camera RAM figure nobody can predict.
|
|
200
|
-
* - **The window is measured on ARRIVAL, not parsed from `tfdt`.** The
|
|
201
|
-
* splitter deliberately never computes a fragment's duration (a second
|
|
202
|
-
* opinion about a fact the muxer owns), and a prebuffer cares about how long
|
|
203
|
-
* ago the bytes turned up, which is exactly what arrival time answers.
|
|
204
|
-
* - **A replay is not backlog.** A subscriber taking N retained fragments gets
|
|
205
|
-
* its queue capacity raised by N for them, because closing a subscriber as a
|
|
206
|
-
* slow consumer for the prebuffer it explicitly asked for would be the
|
|
207
|
-
* stupidest possible failure — and, with `DEFAULT_QUEUE_CAPACITY` of 4 and a
|
|
208
|
-
* ring of 4, the guaranteed one.
|
|
209
|
-
*
|
|
210
|
-
* ## `isLast`
|
|
211
|
-
*
|
|
212
|
-
* hap-nodejs requires the delegate to mark exactly one `RecordingPacket` with
|
|
213
|
-
* `isLast` — a generator that finishes without it produces the twelve-second
|
|
214
|
-
* timeout loop [D50](../../../../../docs/decisions/adr-0050.md) deleted. The
|
|
215
|
-
* plane therefore computes it at DELIVERY time: a packet is last when the plane
|
|
216
|
-
* has ended and nothing remains queued behind it. A subscription that ends
|
|
217
|
-
* having delivered NOTHING says so through {@link Fmp4Subscription.delivered};
|
|
218
|
-
* the future delegate must not open an HDS stream it cannot feed.
|
|
219
|
-
*/
|
|
220
|
-
var DEFAULT_QUEUE_CAPACITY = 4;
|
|
221
|
-
var Fmp4FragmentPlane = class {
|
|
222
|
-
logger;
|
|
223
|
-
prebuffer;
|
|
224
|
-
now;
|
|
225
|
-
subscriptions = /* @__PURE__ */ new Map();
|
|
226
|
-
/** The last init unit seen, handed to every later subscriber. */
|
|
227
|
-
retainedInit = null;
|
|
228
|
-
ended = false;
|
|
229
|
-
/** Oldest first. Empty unless {@link Fmp4PrebufferOptions} was supplied. */
|
|
230
|
-
ring = [];
|
|
231
|
-
ringBytes = 0;
|
|
232
|
-
constructor(logger, prebuffer, now = Date.now) {
|
|
233
|
-
this.logger = logger;
|
|
234
|
-
this.prebuffer = prebuffer;
|
|
235
|
-
this.now = now;
|
|
236
|
-
}
|
|
237
|
-
get subscriberCount() {
|
|
238
|
-
return this.subscriptions.size;
|
|
239
|
-
}
|
|
240
|
-
/** True once {@link end} has been called — no further units are accepted. */
|
|
241
|
-
get isEnded() {
|
|
242
|
-
return this.ended;
|
|
243
|
-
}
|
|
244
|
-
/** What the prebuffer ring holds right now. All zeroes when disabled. */
|
|
245
|
-
prebufferStats() {
|
|
246
|
-
const oldest = this.ring[0];
|
|
247
|
-
return {
|
|
248
|
-
fragments: this.ring.length,
|
|
249
|
-
bytes: this.ringBytes,
|
|
250
|
-
spanMs: oldest === void 0 ? 0 : this.now() - oldest.arrivedAt
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
subscribe(input) {
|
|
254
|
-
const replay = input.withPrebuffer === true ? this.trimmedRing() : [];
|
|
255
|
-
const requested = Math.max(1, input.queueCapacity ?? DEFAULT_QUEUE_CAPACITY);
|
|
256
|
-
const sub = {
|
|
257
|
-
id: `fmp4-${(0, node_crypto.randomUUID)()}`,
|
|
258
|
-
tag: input.tag,
|
|
259
|
-
subscribedAt: this.now(),
|
|
260
|
-
capacity: requested + replay.length,
|
|
261
|
-
queue: [],
|
|
262
|
-
delivered: 0,
|
|
263
|
-
closedReason: null,
|
|
264
|
-
wake: null,
|
|
265
|
-
iterating: false
|
|
266
|
-
};
|
|
267
|
-
this.subscriptions.set(sub.id, sub);
|
|
268
|
-
if (this.retainedInit !== null) this.enqueue(sub, this.retainedInit);
|
|
269
|
-
for (const retained of replay) this.enqueue(sub, retained.unit);
|
|
270
|
-
if (this.ended) this.closeSubscription(sub, "ended");
|
|
271
|
-
this.logger?.info("fmp4 plane: subscribed", { meta: {
|
|
272
|
-
subscriptionId: sub.id,
|
|
273
|
-
tag: sub.tag,
|
|
274
|
-
hasRetainedInit: this.retainedInit !== null,
|
|
275
|
-
prebufferFragments: replay.length,
|
|
276
|
-
prebufferBytes: replay.reduce((n, r) => n + r.unit.data.length, 0)
|
|
277
|
-
} });
|
|
278
|
-
return this.facade(sub);
|
|
279
|
-
}
|
|
280
|
-
/**
|
|
281
|
-
* Fan one splitter unit out. An `init` REPLACES the retained one — ffmpeg
|
|
282
|
-
* emits exactly one per child, and a second means the child was respawned, in
|
|
283
|
-
* which case the old one describes a stream that no longer exists.
|
|
284
|
-
*/
|
|
285
|
-
publish(unit) {
|
|
286
|
-
if (this.ended) return;
|
|
287
|
-
if (unit.kind === "init") {
|
|
288
|
-
this.retainedInit = unit;
|
|
289
|
-
this.ring.length = 0;
|
|
290
|
-
this.ringBytes = 0;
|
|
291
|
-
} else this.retain(unit);
|
|
292
|
-
for (const sub of this.subscriptions.values()) {
|
|
293
|
-
if (sub.closedReason !== null) continue;
|
|
294
|
-
this.enqueue(sub, unit);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* The producer stopped. Every subscriber drains what it holds; its final
|
|
299
|
-
* packet carries `isLast`, and its generator then completes.
|
|
300
|
-
*/
|
|
301
|
-
end(reason = "producer ended") {
|
|
302
|
-
if (this.ended) return;
|
|
303
|
-
this.ended = true;
|
|
304
|
-
this.logger?.info("fmp4 plane: ended", { meta: {
|
|
305
|
-
reason,
|
|
306
|
-
subscribers: this.subscriptions.size
|
|
307
|
-
} });
|
|
308
|
-
for (const sub of this.subscriptions.values()) if (sub.closedReason === null) this.closeSubscription(sub, "ended");
|
|
309
|
-
}
|
|
310
|
-
listSubscribers() {
|
|
311
|
-
return [...this.subscriptions.values()].map((s) => ({
|
|
312
|
-
tag: s.tag,
|
|
313
|
-
subscribedAt: s.subscribedAt,
|
|
314
|
-
delivered: s.delivered,
|
|
315
|
-
closedReason: s.closedReason
|
|
316
|
-
}));
|
|
317
|
-
}
|
|
318
|
-
/** End and forget everything. Idempotent. */
|
|
319
|
-
dispose() {
|
|
320
|
-
this.end("disposed");
|
|
321
|
-
this.subscriptions.clear();
|
|
322
|
-
this.retainedInit = null;
|
|
323
|
-
this.ring.length = 0;
|
|
324
|
-
this.ringBytes = 0;
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Add one fragment to the ring and evict from the front until BOTH bounds
|
|
328
|
-
* hold. Eviction is oldest-first, which is the one place in this file where
|
|
329
|
-
* dropping is correct: the ring is context, not stream — nobody is mid-decode
|
|
330
|
-
* on it, and a subscriber only ever receives a contiguous tail of it.
|
|
331
|
-
*/
|
|
332
|
-
retain(unit) {
|
|
333
|
-
const prebuffer = this.prebuffer;
|
|
334
|
-
if (prebuffer === void 0) return;
|
|
335
|
-
const arrivedAt = this.now();
|
|
336
|
-
this.ring.push({
|
|
337
|
-
unit,
|
|
338
|
-
arrivedAt
|
|
339
|
-
});
|
|
340
|
-
this.ringBytes += unit.data.length;
|
|
341
|
-
const cutoff = arrivedAt - prebuffer.windowMs;
|
|
342
|
-
while (this.ring.length > 0) {
|
|
343
|
-
const oldest = this.ring[0];
|
|
344
|
-
if (oldest === void 0) break;
|
|
345
|
-
const tooOld = oldest.arrivedAt < cutoff;
|
|
346
|
-
const tooBig = this.ringBytes > prebuffer.maxBytes;
|
|
347
|
-
if (!tooOld && !tooBig || this.ring.length === 1) break;
|
|
348
|
-
this.ring.shift();
|
|
349
|
-
this.ringBytes -= oldest.unit.data.length;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* The ring as a subscriber should receive it — window applied AT SUBSCRIBE
|
|
354
|
-
* time, not only at publish time. A camera that went quiet keeps its last
|
|
355
|
-
* fragment in the ring indefinitely (see the never-evict-the-newest rule),
|
|
356
|
-
* and replaying a 40-second-old fragment as "prebuffer" would put stale video
|
|
357
|
-
* at the head of a clip iOS presents as the moment of the event.
|
|
358
|
-
*/
|
|
359
|
-
trimmedRing() {
|
|
360
|
-
const prebuffer = this.prebuffer;
|
|
361
|
-
if (prebuffer === void 0) return [];
|
|
362
|
-
const cutoff = this.now() - prebuffer.windowMs;
|
|
363
|
-
return this.ring.filter((r) => r.arrivedAt >= cutoff);
|
|
364
|
-
}
|
|
365
|
-
enqueue(sub, unit) {
|
|
366
|
-
if (sub.queue.length >= sub.capacity) {
|
|
367
|
-
this.logger?.warn("fmp4 plane: subscriber fell behind — CLOSING it rather than gapping it", { meta: {
|
|
368
|
-
subscriptionId: sub.id,
|
|
369
|
-
tag: sub.tag,
|
|
370
|
-
capacity: sub.capacity,
|
|
371
|
-
delivered: sub.delivered
|
|
372
|
-
} });
|
|
373
|
-
this.closeSubscription(sub, "slow-consumer");
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
sub.queue.push({
|
|
377
|
-
kind: unit.kind,
|
|
378
|
-
data: unit.data,
|
|
379
|
-
sequence: unit.sequence,
|
|
380
|
-
isLast: false
|
|
381
|
-
});
|
|
382
|
-
this.wake(sub);
|
|
383
|
-
}
|
|
384
|
-
closeSubscription(sub, reason) {
|
|
385
|
-
if (sub.closedReason !== null) return;
|
|
386
|
-
sub.closedReason = reason;
|
|
387
|
-
if (reason === "slow-consumer") sub.queue.length = 0;
|
|
388
|
-
this.wake(sub);
|
|
389
|
-
}
|
|
390
|
-
wake(sub) {
|
|
391
|
-
const resume = sub.wake;
|
|
392
|
-
sub.wake = null;
|
|
393
|
-
resume?.();
|
|
394
|
-
}
|
|
395
|
-
facade(sub) {
|
|
396
|
-
const plane = this;
|
|
397
|
-
return {
|
|
398
|
-
id: sub.id,
|
|
399
|
-
tag: sub.tag,
|
|
400
|
-
get delivered() {
|
|
401
|
-
return sub.delivered;
|
|
402
|
-
},
|
|
403
|
-
get closedReason() {
|
|
404
|
-
return sub.closedReason;
|
|
405
|
-
},
|
|
406
|
-
packets: () => plane.iterate(sub),
|
|
407
|
-
release: () => {
|
|
408
|
-
plane.closeSubscription(sub, "released");
|
|
409
|
-
plane.subscriptions.delete(sub.id);
|
|
410
|
-
}
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
async *iterate(sub) {
|
|
414
|
-
if (sub.iterating) throw new Error(`fmp4 plane: subscription ${sub.tag} is already being consumed — take a second subscription`);
|
|
415
|
-
sub.iterating = true;
|
|
416
|
-
for (;;) {
|
|
417
|
-
const next = sub.queue.shift();
|
|
418
|
-
if (next === void 0) {
|
|
419
|
-
if (sub.closedReason !== null) return;
|
|
420
|
-
await new Promise((resolve) => {
|
|
421
|
-
sub.wake = resolve;
|
|
422
|
-
});
|
|
423
|
-
continue;
|
|
424
|
-
}
|
|
425
|
-
const isLast = sub.closedReason === "ended" && sub.queue.length === 0;
|
|
426
|
-
sub.delivered += 1;
|
|
427
|
-
yield {
|
|
428
|
-
...next,
|
|
429
|
-
isLast
|
|
430
|
-
};
|
|
431
|
-
if (isLast) return;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
var DEFAULT_FIRST_UNIT_TIMEOUT_MS = 12e3;
|
|
436
|
-
/** Heartbeat cadence — ~2 minutes of 4 s fragments. */
|
|
437
|
-
var FRAGMENT_LOG_EVERY = 30;
|
|
438
|
-
var Fmp4FragmentChild = class {
|
|
439
|
-
deps;
|
|
440
|
-
args;
|
|
441
|
-
child = null;
|
|
442
|
-
splitter = new require_dist.Fmp4BoxSplitter();
|
|
443
|
-
stopped = false;
|
|
444
|
-
unitsOut = 0;
|
|
445
|
-
activeHwAccel = null;
|
|
446
|
-
constructor(deps, args) {
|
|
447
|
-
this.deps = deps;
|
|
448
|
-
this.args = args;
|
|
449
|
-
}
|
|
450
|
-
/** Spawn, and resolve once the INIT segment has been cut out of stdout. */
|
|
451
|
-
async start() {
|
|
452
|
-
const requested = this.args.invocation.decodeHwAccel;
|
|
453
|
-
this.activeHwAccel = requested;
|
|
454
|
-
try {
|
|
455
|
-
await this.spawnAttempt(requested);
|
|
456
|
-
return;
|
|
457
|
-
} catch (err) {
|
|
458
|
-
if (this.stopped) throw err;
|
|
459
|
-
if (requested === null || require_dist.isSoftwareDecode(requested)) throw err;
|
|
460
|
-
this.deps.logger.warn("fmp4 fragment child: hardware decode produced NO fragment — retrying in SOFTWARE", {
|
|
461
|
-
tags: { deviceId: this.args.deviceId },
|
|
462
|
-
meta: {
|
|
463
|
-
sourceId: this.args.sourceId,
|
|
464
|
-
decodeHwAccel: requested,
|
|
465
|
-
error: require_dist.errMsg(err)
|
|
466
|
-
}
|
|
467
|
-
});
|
|
468
|
-
this.killChild();
|
|
469
|
-
this.splitter = new require_dist.Fmp4BoxSplitter();
|
|
470
|
-
this.activeHwAccel = null;
|
|
471
|
-
await this.spawnAttempt(null);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
/** The backend the child ACTUALLY ran with — `null` for software. */
|
|
475
|
-
activeDecodeHwAccel() {
|
|
476
|
-
const value = this.activeHwAccel;
|
|
477
|
-
return value === null || value === "none" || value === "copy" ? null : value;
|
|
478
|
-
}
|
|
479
|
-
/** Kill ffmpeg and end the plane. Idempotent. */
|
|
480
|
-
async stop() {
|
|
481
|
-
if (this.stopped) return;
|
|
482
|
-
this.stopped = true;
|
|
483
|
-
this.killChild();
|
|
484
|
-
this.args.plane.end("the fragment child stopped");
|
|
485
|
-
}
|
|
486
|
-
spawnAttempt(decodeHwAccel) {
|
|
487
|
-
const args = require_dist.buildFfmpegArgs({
|
|
488
|
-
...this.args.invocation,
|
|
489
|
-
decodeHwAccel,
|
|
490
|
-
sink: {
|
|
491
|
-
kind: "stdout",
|
|
492
|
-
container: "mp4",
|
|
493
|
-
fragmentMs: this.args.fragmentMs
|
|
494
|
-
}
|
|
495
|
-
});
|
|
496
|
-
this.deps.logger.info("fmp4 fragment child: spawning ffmpeg", {
|
|
497
|
-
tags: { deviceId: this.args.deviceId },
|
|
498
|
-
meta: {
|
|
499
|
-
sourceId: this.args.sourceId,
|
|
500
|
-
fragmentMs: this.args.fragmentMs,
|
|
501
|
-
decodeHwAccel: decodeHwAccel ?? "software",
|
|
502
|
-
argv: args.join(" ")
|
|
503
|
-
}
|
|
504
|
-
});
|
|
505
|
-
return new Promise((resolve, reject) => {
|
|
506
|
-
const child = this.deps.spawnFn(this.deps.ffmpegBinaryPath, args, { stdio: [
|
|
507
|
-
"ignore",
|
|
508
|
-
"pipe",
|
|
509
|
-
"pipe"
|
|
510
|
-
] });
|
|
511
|
-
this.child = child;
|
|
512
|
-
let settled = false;
|
|
513
|
-
/**
|
|
514
|
-
* This attempt FAILED. Set before the kill, because SIGTERM makes the
|
|
515
|
-
* child exit and that exit must not be reported as a death: the retry —
|
|
516
|
-
* or the caller's rejection — already owns what happens next. Without it
|
|
517
|
-
* the timeout path ends the plane the software retry is about to fill,
|
|
518
|
-
* and the consumer sees a stream that stopped for no reason. A "which
|
|
519
|
-
* spawn is current" counter does NOT cover this: the retry has not been
|
|
520
|
-
* spawned when the kill's exit arrives.
|
|
521
|
-
*/
|
|
522
|
-
let failed = false;
|
|
523
|
-
/**
|
|
524
|
-
* This attempt is still the live producer: it has not failed (a failure
|
|
525
|
-
* hands ownership to the retry, or to the caller's rejection) and nothing
|
|
526
|
-
* has stopped the child. Those two cover every way an attempt stops being
|
|
527
|
-
* current — `start` only respawns after a rejection.
|
|
528
|
-
*/
|
|
529
|
-
const isCurrent = () => !this.stopped && !failed;
|
|
530
|
-
const timeoutMs = this.deps.firstUnitTimeoutMs ?? DEFAULT_FIRST_UNIT_TIMEOUT_MS;
|
|
531
|
-
const settle = (fail) => {
|
|
532
|
-
if (settled) return;
|
|
533
|
-
settled = true;
|
|
534
|
-
clearTimeout(timer);
|
|
535
|
-
if (fail) {
|
|
536
|
-
failed = true;
|
|
537
|
-
reject(fail);
|
|
538
|
-
} else resolve();
|
|
539
|
-
};
|
|
540
|
-
const timer = setTimeout(() => {
|
|
541
|
-
settle(/* @__PURE__ */ new Error(`fmp4 fragment child: no fragment within ${timeoutMs}ms`));
|
|
542
|
-
this.killChild();
|
|
543
|
-
}, timeoutMs);
|
|
544
|
-
timer.unref?.();
|
|
545
|
-
child.stdout?.on("data", (chunk) => {
|
|
546
|
-
for (const unit of this.splitter.push(chunk)) {
|
|
547
|
-
this.unitsOut += 1;
|
|
548
|
-
this.args.plane.publish(unit);
|
|
549
|
-
if (unit.kind === "init") {
|
|
550
|
-
this.deps.logger.info("fmp4 fragment child: INIT segment cut", {
|
|
551
|
-
tags: { deviceId: this.args.deviceId },
|
|
552
|
-
meta: {
|
|
553
|
-
sourceId: this.args.sourceId,
|
|
554
|
-
bytes: unit.data.length
|
|
555
|
-
}
|
|
556
|
-
});
|
|
557
|
-
settle();
|
|
558
|
-
} else if (this.unitsOut % FRAGMENT_LOG_EVERY === 0) this.deps.logger.info("fmp4 fragment child: fragments still flowing", {
|
|
559
|
-
tags: { deviceId: this.args.deviceId },
|
|
560
|
-
meta: {
|
|
561
|
-
sourceId: this.args.sourceId,
|
|
562
|
-
unitsOut: this.unitsOut,
|
|
563
|
-
bytes: unit.data.length,
|
|
564
|
-
subscribers: this.args.plane.subscriberCount
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
}
|
|
568
|
-
const fault = this.splitter.fault;
|
|
569
|
-
if (fault !== null) this.onFault(fault, settled, isCurrent(), settle);
|
|
570
|
-
});
|
|
571
|
-
child.stderr?.setEncoding("utf8");
|
|
572
|
-
child.stderr?.on("data", (line) => {
|
|
573
|
-
this.deps.logger.debug("fmp4 fragment child ffmpeg", {
|
|
574
|
-
tags: { deviceId: this.args.deviceId },
|
|
575
|
-
meta: {
|
|
576
|
-
sourceId: this.args.sourceId,
|
|
577
|
-
line: line.trim()
|
|
578
|
-
}
|
|
579
|
-
});
|
|
580
|
-
});
|
|
581
|
-
child.once("error", (err) => {
|
|
582
|
-
if (!settled) {
|
|
583
|
-
settle(err);
|
|
584
|
-
return;
|
|
585
|
-
}
|
|
586
|
-
if (!isCurrent()) return;
|
|
587
|
-
this.args.plane.end("the fragment child errored");
|
|
588
|
-
this.deps.onChildExit?.(err);
|
|
589
|
-
});
|
|
590
|
-
child.once("exit", (code, signal) => {
|
|
591
|
-
if (!settled) {
|
|
592
|
-
settle(/* @__PURE__ */ new Error(`fmp4 fragment child: ffmpeg exited before any fragment (code=${code} signal=${signal})`));
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
|
-
if (!isCurrent()) return;
|
|
596
|
-
const error = /* @__PURE__ */ new Error(`fmp4 fragment child: ffmpeg exited while live (code=${code} signal=${signal})`);
|
|
597
|
-
this.deps.logger.warn("fmp4 fragment child: ffmpeg exited while live", {
|
|
598
|
-
tags: { deviceId: this.args.deviceId },
|
|
599
|
-
meta: {
|
|
600
|
-
sourceId: this.args.sourceId,
|
|
601
|
-
code,
|
|
602
|
-
signal,
|
|
603
|
-
unitsOut: this.unitsOut
|
|
604
|
-
}
|
|
605
|
-
});
|
|
606
|
-
this.args.plane.end("the fragment child exited");
|
|
607
|
-
this.deps.onChildExit?.(error);
|
|
608
|
-
});
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
/**
|
|
612
|
-
* The byte stream stopped being splittable. Not recoverable — the splitter
|
|
613
|
-
* cannot resynchronise mid-box — so the child is a corpse and every consumer
|
|
614
|
-
* has to be told, loudly, with the reason.
|
|
615
|
-
*/
|
|
616
|
-
onFault(reason, wasLive, current, settle) {
|
|
617
|
-
const error = /* @__PURE__ */ new Error(`fmp4 fragment child: ${reason}`);
|
|
618
|
-
this.deps.logger.error("fmp4 fragment child: the ffmpeg output stopped parsing as fMP4", {
|
|
619
|
-
tags: { deviceId: this.args.deviceId },
|
|
620
|
-
meta: {
|
|
621
|
-
sourceId: this.args.sourceId,
|
|
622
|
-
unitsOut: this.unitsOut,
|
|
623
|
-
interstitial: this.splitter.discardedInterstitialTypes,
|
|
624
|
-
reason
|
|
625
|
-
}
|
|
626
|
-
});
|
|
627
|
-
this.killChild();
|
|
628
|
-
settle(error);
|
|
629
|
-
if (wasLive && current) {
|
|
630
|
-
this.args.plane.end("the fragment child produced unsplittable output");
|
|
631
|
-
this.deps.onChildExit?.(error);
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
killChild() {
|
|
635
|
-
const child = this.child;
|
|
636
|
-
this.child = null;
|
|
637
|
-
if (child && !child.killed) try {
|
|
638
|
-
child.kill("SIGTERM");
|
|
639
|
-
} catch (err) {
|
|
640
|
-
this.deps.logger.warn("fmp4 fragment child: kill error", {
|
|
641
|
-
tags: { deviceId: this.args.deviceId },
|
|
642
|
-
meta: {
|
|
643
|
-
sourceId: this.args.sourceId,
|
|
644
|
-
error: require_dist.errMsg(err)
|
|
645
|
-
}
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
};
|
|
650
|
-
//#endregion
|
|
651
|
-
exports.FilesystemStorageProvider = FilesystemStorageProvider;
|
|
652
|
-
exports.Fmp4FragmentChild = Fmp4FragmentChild;
|
|
653
|
-
exports.Fmp4FragmentPlane = Fmp4FragmentPlane;
|