@camstack/addon-pipeline 0.1.20 → 0.2.1
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/audio-analyzer/index.js +736 -719
- package/dist/audio-analyzer/index.mjs +726 -679
- package/dist/audio-codec-nodeav/index.js +304 -461
- package/dist/audio-codec-nodeav/index.mjs +300 -462
- package/dist/chunk-BdkLduGY.mjs +5 -0
- package/dist/chunk-D6vf50IK.js +28 -0
- package/dist/codec-runtime-BOk-13PN.js +202 -0
- package/dist/codec-runtime-BsqlEjPi.mjs +197 -0
- package/dist/constants-B_b0a-6h.mjs +3119 -0
- package/dist/{index-CMcx_k6Y.js → constants-D65v6yp6.js} +3107 -2935
- package/dist/decoder-nodeav/index.js +1374 -1444
- package/dist/decoder-nodeav/index.mjs +1369 -1425
- package/dist/detection-pipeline/index.js +6462 -5613
- package/dist/detection-pipeline/index.mjs +6451 -5574
- package/dist/dist-7ewQjTle.js +22454 -0
- package/dist/dist-C5jnNl0n.mjs +22089 -0
- package/dist/motion-wasm/index.js +469 -467
- package/dist/motion-wasm/index.mjs +464 -446
- package/dist/pipeline-runner/index.js +2029 -1827
- package/dist/pipeline-runner/index.mjs +2025 -1811
- package/dist/recorder/index.js +2045 -2157
- package/dist/recorder/index.mjs +2042 -2156
- package/dist/stream-broker/_stub.js +1806 -1352
- package/dist/stream-broker/_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-D4-DHanK.mjs +156 -0
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-Tf-HACFd.mjs +26 -0
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-C9WX5HNw.mjs +26 -0
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_tanstack_mf_1_react_mf_2_query__loadShare__.js-BO7TIbJV.mjs +26 -0
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare__react__loadShare__.js-C9j-2lBe.mjs +26 -0
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare__react_mf_1_jsx_mf_2_runtime__loadShare__.js-XO0-Pyu6.mjs +26 -0
- package/dist/stream-broker/dist-CYZr2fwk.mjs +2726 -0
- package/dist/stream-broker/hostInit-Di6vceAU.mjs +129 -0
- package/dist/stream-broker/index.js +17778 -15470
- package/dist/stream-broker/index.mjs +17769 -15465
- package/dist/stream-broker/remoteEntry.js +134 -2973
- package/dist/stream-broker/remoteEntry.ssr.js +33 -0
- package/dist/stream-broker/virtualExposes-dYNvIwoR.mjs +27 -0
- package/dist/stream-broker/virtual_mf-exposes-ssr___mfe_internal__addon_stream_broker_widgets__remoteEntry_js-Cmqfp4i_.mjs +10 -0
- package/embed-dist/assets/index-B8VlSD0-.js +150 -0
- package/embed-dist/assets/index-ZhDdp1Nd.css +2 -0
- package/embed-dist/index.html +13 -0
- package/package.json +25 -7
- package/wasm/assembly/index.ts +41 -16
- package/dist/audio-analyzer/index.js.map +0 -1
- package/dist/audio-analyzer/index.mjs.map +0 -1
- package/dist/audio-codec-nodeav/index.js.map +0 -1
- package/dist/audio-codec-nodeav/index.mjs.map +0 -1
- package/dist/decoder-nodeav/index.js.map +0 -1
- package/dist/decoder-nodeav/index.mjs.map +0 -1
- package/dist/detection-pipeline/index.js.map +0 -1
- package/dist/detection-pipeline/index.mjs.map +0 -1
- package/dist/index-5aYef068.mjs +0 -17514
- package/dist/index-5aYef068.mjs.map +0 -1
- package/dist/index-B36NMAdu.js +0 -17513
- package/dist/index-B36NMAdu.js.map +0 -1
- package/dist/index-CMcx_k6Y.js.map +0 -1
- package/dist/index-CYb7cFrv.mjs +0 -5790
- package/dist/index-CYb7cFrv.mjs.map +0 -1
- package/dist/motion-wasm/index.js.map +0 -1
- package/dist/motion-wasm/index.mjs.map +0 -1
- package/dist/pipeline-runner/index.js.map +0 -1
- package/dist/pipeline-runner/index.mjs.map +0 -1
- package/dist/recorder/index.js.map +0 -1
- package/dist/recorder/index.mjs.map +0 -1
- package/dist/stream-broker/@mf-types/compiled-types/stream-broker/widgets/FfmpegParamsField.d.ts +0 -41
- package/dist/stream-broker/@mf-types/compiled-types/stream-broker/widgets/GeometryBuilder.d.ts +0 -54
- package/dist/stream-broker/@mf-types/compiled-types/stream-broker/widgets/StreamBrokerPanel.d.ts +0 -21
- package/dist/stream-broker/@mf-types/compiled-types/stream-broker/widgets/format-ua.d.ts +0 -13
- package/dist/stream-broker/@mf-types/compiled-types/stream-broker/widgets/index.d.ts +0 -15
- package/dist/stream-broker/@mf-types/widgets.d.ts +0 -2
- package/dist/stream-broker/@mf-types.d.ts +0 -3
- package/dist/stream-broker/@mf-types.zip +0 -0
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_sdk__loadShare__.mjs-lantnv8e.mjs +0 -12
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.mjs-DJ3UNg7O.mjs +0 -30
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.mjs-CYXy_bhS.mjs +0 -21
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_tanstack_mf_1_react_mf_2_query__loadShare__.mjs-U1EUeEPs.mjs +0 -104
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_trpc_mf_1_client__loadShare__.mjs-DeouEaSs.mjs +0 -85
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_trpc_mf_1_react_mf_2_query__loadShare__.mjs-DHUwjbb9.mjs +0 -62
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare__react__loadShare__.mjs-CaDEYBIU.mjs +0 -89
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare__react__loadShare__.mjs_commonjs-proxy-D6EROtlA.mjs +0 -29
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare__react_mf_1_jsx_mf_2_runtime__loadShare__.mjs-x6pP3Ghk.mjs +0 -36
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare__react_mf_2_dom__loadShare__.mjs-DYEKzzY-.mjs +0 -45
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare__react_mf_2_dom__loadShare__.mjs_commonjs-proxy-CcnN6sbA.mjs +0 -6
- package/dist/stream-broker/__mfe_internal__addon_stream_broker_widgets__loadShare__react_mf_2_dom_mf_1_client__loadShare__.mjs-DICOtMTl.mjs +0 -34
- package/dist/stream-broker/_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-CL9DR49k.mjs +0 -156
- package/dist/stream-broker/client-BvTmMOQu.mjs +0 -9836
- package/dist/stream-broker/getErrorShape-BPSzUA7W-TlK8ipWe.mjs +0 -211
- package/dist/stream-broker/hostInit-ChmiMPS0.mjs +0 -168
- package/dist/stream-broker/index-BxsFuFmE.mjs +0 -2603
- package/dist/stream-broker/index-C-248uOU.mjs +0 -725
- package/dist/stream-broker/index-C05B6jqp.mjs +0 -185
- package/dist/stream-broker/index-CWkKuNLr.mjs +0 -232
- package/dist/stream-broker/index-DOJoSShD.mjs +0 -67784
- package/dist/stream-broker/index-DtOI1aTU.mjs +0 -18504
- package/dist/stream-broker/index-oMq6ilgR.mjs +0 -1641
- package/dist/stream-broker/index-vIWZQBIL.mjs +0 -435
- package/dist/stream-broker/index-xncRG7-x.mjs +0 -2713
- package/dist/stream-broker/index.js.map +0 -1
- package/dist/stream-broker/index.mjs.map +0 -1
- package/dist/stream-broker/jsx-runtime-BRT_HL0A.mjs +0 -55
- package/dist/stream-broker/schemas-B7L0qZtq.mjs +0 -3599
- package/dist/stream-broker/virtualExposes-pCd777Rp.mjs +0 -42
|
@@ -1,1463 +1,1393 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
const require_dist = require("../dist-7ewQjTle.js");
|
|
6
|
+
let _camstack_shm_ring = require("@camstack/shm-ring");
|
|
7
|
+
let node_crypto = require("node:crypto");
|
|
8
|
+
//#region src/decoder-nodeav/frame-ring-sink.ts
|
|
9
|
+
/**
|
|
10
|
+
* `DecoderFrameRingSink` — the decoder's shared-memory write side (Phase 5 / D9).
|
|
11
|
+
*
|
|
12
|
+
* When a decoder session is configured with `frameSink: 'shm'`, the decoder
|
|
13
|
+
* **owns** the shared-memory ring segment for that stream: it creates the
|
|
14
|
+
* segment on the first decoded frame (when the output geometry is known),
|
|
15
|
+
* writes every subsequent decoded frame into the ring via a `FrameRingWriter`,
|
|
16
|
+
* and closes + unlinks the segment when the session is destroyed.
|
|
17
|
+
*
|
|
18
|
+
* What leaves the decoder is no longer the pixel `Buffer` — it is a tiny,
|
|
19
|
+
* serialisable `FrameHandle` (`FrameRingWriter.writeFrame`'s return value).
|
|
20
|
+
* Same-host consumers (motion, detection, the WebRTC encoder) open the same
|
|
21
|
+
* segment with a `FrameRingReader` and read the pixels zero-copy.
|
|
22
|
+
*
|
|
23
|
+
* ## Lazy segment creation
|
|
24
|
+
*
|
|
25
|
+
* The segment cannot be sized until the first frame: `slotByteLength` is
|
|
26
|
+
* `width × height × bytesPerPixel`, and the output dimensions are only known
|
|
27
|
+
* once the scaler has produced its first `dstFrame`. So `writeFrame` is a
|
|
28
|
+
* no-op-until-armed: the first call sizes + creates the segment, every later
|
|
29
|
+
* call writes into it.
|
|
30
|
+
*
|
|
31
|
+
* ## Resolution-change decision
|
|
32
|
+
*
|
|
33
|
+
* A live camera stream can change resolution mid-stream (the decoder's scaler
|
|
34
|
+
* is rebuilt on a config toggle, or the source renegotiates). The slot is
|
|
35
|
+
* sized for the **first** frame's geometry. A later frame that no longer fits
|
|
36
|
+
* the slot triggers a **segment re-create**: the old segment is closed +
|
|
37
|
+
* unlinked and a fresh, larger segment is created under a new generation-tagged
|
|
38
|
+
* name. This is simpler and leak-free versus over-allocating slots for a
|
|
39
|
+
* worst-case 4K frame on every stream; resolution changes on a live camera are
|
|
40
|
+
* rare, and a brief gap while consumers re-open the segment is acceptable
|
|
41
|
+
* (latest-wins — a missed frame is correct behaviour).
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* Per-ring shared-memory budget (MB) — the slot count is derived per-resolution
|
|
45
|
+
* from this budget via {@link deriveSlotCount}, so a 360p stream gets many
|
|
46
|
+
* slots and a 4K stream a few, both inside the same memory footprint.
|
|
47
|
+
*
|
|
48
|
+
* Read ONCE at module load from `CAMSTACK_SHM_RING_BUDGET_MB`; a non-finite or
|
|
49
|
+
* non-positive value falls back to the 128 MB default.
|
|
50
|
+
*/
|
|
51
|
+
var RING_BUDGET_MB = (() => {
|
|
52
|
+
const raw = Number(process.env["CAMSTACK_SHM_RING_BUDGET_MB"]);
|
|
53
|
+
return Number.isFinite(raw) && raw > 0 ? Math.floor(raw) : 128;
|
|
31
54
|
})();
|
|
32
|
-
|
|
55
|
+
/** {@link RING_BUDGET_MB} in bytes — the budget passed to `deriveSlotCount`. */
|
|
56
|
+
var RING_BUDGET_BYTES = RING_BUDGET_MB * 1024 * 1024;
|
|
57
|
+
/** A unique, stable shared-memory segment name for a decoder stream.
|
|
58
|
+
*
|
|
59
|
+
* macOS POSIX shm names are capped at ~31 characters (`PSHMNAMLEN`). A
|
|
60
|
+
* `camstack.frames.<deviceId>.<streamId>` scheme overflows that for realistic
|
|
61
|
+
* ids, so the sink uses a short, collision-resistant scheme instead:
|
|
62
|
+
* `csf.<base36 hash>.<gen>`. The hash folds the device id, the session tag
|
|
63
|
+
* and a per-process random salt; the generation suffix makes a re-created
|
|
64
|
+
* segment (resolution change) a distinct name so a stale consumer mapping is
|
|
65
|
+
* never silently reused.
|
|
66
|
+
*/
|
|
33
67
|
function makeSegmentName(seed, generation) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
const tag = (hash >>> 0).toString(36);
|
|
39
|
-
return `csf.${tag}.${generation}`;
|
|
40
|
-
}
|
|
41
|
-
class DecoderFrameRingSink {
|
|
42
|
-
seed;
|
|
43
|
-
logger;
|
|
44
|
-
nodeId;
|
|
45
|
-
segment = null;
|
|
46
|
-
writer = null;
|
|
47
|
-
segmentName = null;
|
|
48
|
-
slotByteLength = 0;
|
|
49
|
-
generation = 0;
|
|
50
|
-
destroyed = false;
|
|
51
|
-
/** Frames committed into the ring across this sink's lifetime (all generations). */
|
|
52
|
-
framesWritten = 0;
|
|
53
|
-
constructor(options) {
|
|
54
|
-
const salt = Math.random().toString(36).slice(2, 8);
|
|
55
|
-
this.seed = `${options.seed}.${salt}`;
|
|
56
|
-
this.logger = options.logger;
|
|
57
|
-
this.nodeId = options.nodeId;
|
|
58
|
-
}
|
|
59
|
-
/** Whether a segment has been created (i.e. at least one frame written). */
|
|
60
|
-
get isArmed() {
|
|
61
|
-
return this.writer !== null;
|
|
62
|
-
}
|
|
63
|
-
/** The current segment name, or `null` before the first frame. */
|
|
64
|
-
get currentSegmentName() {
|
|
65
|
-
return this.segmentName;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Write one decoded frame into the ring and return its `FrameHandle`.
|
|
69
|
-
*
|
|
70
|
-
* On the first call (or after a geometry change that overflows the current
|
|
71
|
-
* slot) the segment is created / re-created sized for this frame. Returns
|
|
72
|
-
* `null` only when the sink has been destroyed.
|
|
73
|
-
*
|
|
74
|
-
* This is the copy-in convenience form (it copies `pixels` into the slot).
|
|
75
|
-
* The decoder's hot path uses the zero-copy {@link beginFrame} /
|
|
76
|
-
* {@link commitFrame} scatter-write pair instead — the scaler produces its
|
|
77
|
-
* packed output directly into the slot, eliminating the write-side memcpy.
|
|
78
|
-
*/
|
|
79
|
-
writeFrame(pixels, meta) {
|
|
80
|
-
if (this.destroyed) return null;
|
|
81
|
-
if (this.writer === null || shmRing.computeSlotByteLength(meta.width, meta.height, meta.format) > this.slotByteLength) {
|
|
82
|
-
this.recreateSegment(
|
|
83
|
-
shmRing.computeSlotByteLength(meta.width, meta.height, meta.format)
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
const writer = this.writer;
|
|
87
|
-
if (writer === null) {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
const handle = writer.writeFrame(pixels, meta);
|
|
91
|
-
this.framesWritten += 1;
|
|
92
|
-
return handle;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Reserve a ring slot for a frame of the given geometry — the **zero-copy**
|
|
96
|
-
* scatter-write entry point (Phase 5 / D9 Task 7c).
|
|
97
|
-
*
|
|
98
|
-
* The segment is created / re-created here if this is the first frame or the
|
|
99
|
-
* geometry overflows the current slot capacity, so the slot is correctly
|
|
100
|
-
* sized before the caller fills it. The returned `buffer` is a writable view
|
|
101
|
-
* **directly over the mapped segment** — the node-av scaler scatters its
|
|
102
|
-
* packed output straight into it, with no intermediate copy. The caller MUST
|
|
103
|
-
* call {@link commitFrame} with the returned `slot` once the slot is filled.
|
|
104
|
-
*
|
|
105
|
-
* Returns `null` when the sink is destroyed or the segment cannot be created.
|
|
106
|
-
*/
|
|
107
|
-
beginFrame(width, height, format) {
|
|
108
|
-
if (this.destroyed) return null;
|
|
109
|
-
const requiredSlotBytes = shmRing.computeSlotByteLength(width, height, format);
|
|
110
|
-
if (this.writer === null || requiredSlotBytes > this.slotByteLength) {
|
|
111
|
-
this.recreateSegment(requiredSlotBytes);
|
|
112
|
-
}
|
|
113
|
-
const writer = this.writer;
|
|
114
|
-
if (writer === null) {
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
const { slot, buffer } = writer.beginFrame();
|
|
118
|
-
return { slot, buffer };
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Publish the frame whose slot was reserved by {@link beginFrame} and filled
|
|
122
|
-
* in place by the caller. `slot` MUST be the value from the matching
|
|
123
|
-
* `beginFrame`. Returns the published `FrameHandle`, or `null` if the sink
|
|
124
|
-
* was destroyed (or the segment lost) between begin and commit.
|
|
125
|
-
*/
|
|
126
|
-
commitFrame(slot, meta) {
|
|
127
|
-
if (this.destroyed) return null;
|
|
128
|
-
const writer = this.writer;
|
|
129
|
-
if (writer === null) return null;
|
|
130
|
-
const handle = writer.commitFrame(slot, meta);
|
|
131
|
-
this.framesWritten += 1;
|
|
132
|
-
return handle;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Current shm ring usage — `null` until the first frame arms the segment.
|
|
136
|
-
* Surfaced through `decoder.getShmStats` so a downstream consumer can
|
|
137
|
-
* observe ring pressure (slot depth, byte budget, frames written).
|
|
138
|
-
*/
|
|
139
|
-
getShmStats() {
|
|
140
|
-
if (this.writer === null) return null;
|
|
141
|
-
return {
|
|
142
|
-
slotCount: this.writer.slotCount,
|
|
143
|
-
slotByteLength: this.slotByteLength,
|
|
144
|
-
segmentBytes: shmRing.computeSegmentSize(this.writer.slotCount, this.slotByteLength),
|
|
145
|
-
framesWritten: this.framesWritten
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Abandon a slot reserved by {@link beginFrame} **without publishing it** —
|
|
150
|
-
* the degenerate-path counterpart of {@link commitFrame}.
|
|
151
|
-
*
|
|
152
|
-
* A caller that reserved a slot but then could not produce valid pixels (no
|
|
153
|
-
* decoded source planes, or the scaler threw) MUST call this instead of
|
|
154
|
-
* `commitFrame`: it closes the open seqlock without advancing `writeIndex`,
|
|
155
|
-
* so no reader ever sees the slot's uninitialised bytes as a real frame, and
|
|
156
|
-
* no `FrameHandle` is handed downstream. `slot` MUST be the value from the
|
|
157
|
-
* matching `beginFrame`. A no-op if the sink was destroyed (or the segment
|
|
158
|
-
* lost) between begin and abort.
|
|
159
|
-
*/
|
|
160
|
-
abortFrame(slot) {
|
|
161
|
-
if (this.destroyed) return;
|
|
162
|
-
const writer = this.writer;
|
|
163
|
-
if (writer === null) return;
|
|
164
|
-
writer.abortFrame(slot);
|
|
165
|
-
}
|
|
166
|
-
/** Close + unlink the segment. Idempotent. */
|
|
167
|
-
destroy() {
|
|
168
|
-
if (this.destroyed) return;
|
|
169
|
-
this.destroyed = true;
|
|
170
|
-
this.releaseSegment();
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Create a fresh segment sized for at least `slotByteLength` bytes per slot,
|
|
174
|
-
* replacing any prior one. A re-create bumps the generation so the new
|
|
175
|
-
* segment has a distinct name — a consumer holding the old mapping is never
|
|
176
|
-
* silently handed a resized segment.
|
|
177
|
-
*/
|
|
178
|
-
recreateSegment(slotByteLength) {
|
|
179
|
-
this.releaseSegment();
|
|
180
|
-
this.generation += 1;
|
|
181
|
-
const name = makeSegmentName(this.seed, this.generation);
|
|
182
|
-
const slotCount = shmRing.deriveSlotCount(RING_BUDGET_BYTES, slotByteLength);
|
|
183
|
-
if (slotCount === shmRing.MIN_RING_SLOTS && shmRing.MIN_RING_SLOTS * slotByteLength > RING_BUDGET_BYTES) {
|
|
184
|
-
this.logger.warn(
|
|
185
|
-
"decoder shm ring: budget too small for resolution — using MIN slots",
|
|
186
|
-
{ meta: { slotByteLength, budgetMb: RING_BUDGET_MB } }
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
const totalBytes = shmRing.computeSegmentSize(slotCount, slotByteLength);
|
|
190
|
-
try {
|
|
191
|
-
const segment = shmRing.createSegment(name, totalBytes);
|
|
192
|
-
this.segment = segment;
|
|
193
|
-
this.segmentName = name;
|
|
194
|
-
this.slotByteLength = slotByteLength;
|
|
195
|
-
this.writer = new shmRing.FrameRingWriter(
|
|
196
|
-
segment.buffer,
|
|
197
|
-
name,
|
|
198
|
-
slotCount,
|
|
199
|
-
slotByteLength,
|
|
200
|
-
this.nodeId
|
|
201
|
-
);
|
|
202
|
-
this.logger.info("decoder shm ring: segment created", {
|
|
203
|
-
meta: {
|
|
204
|
-
segment: name,
|
|
205
|
-
slotCount,
|
|
206
|
-
slotByteLength,
|
|
207
|
-
totalBytes,
|
|
208
|
-
generation: this.generation
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
} catch (err) {
|
|
212
|
-
this.segment = null;
|
|
213
|
-
this.writer = null;
|
|
214
|
-
this.segmentName = null;
|
|
215
|
-
this.slotByteLength = 0;
|
|
216
|
-
this.logger.error("decoder shm ring: segment create failed", {
|
|
217
|
-
meta: {
|
|
218
|
-
segment: name,
|
|
219
|
-
slotByteLength,
|
|
220
|
-
error: err instanceof Error ? err.message : String(err)
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
/** Unmap + unlink the current segment, if any. */
|
|
226
|
-
releaseSegment() {
|
|
227
|
-
const segment = this.segment;
|
|
228
|
-
if (segment === null) return;
|
|
229
|
-
this.segment = null;
|
|
230
|
-
this.writer = null;
|
|
231
|
-
const name = this.segmentName;
|
|
232
|
-
this.segmentName = null;
|
|
233
|
-
try {
|
|
234
|
-
segment.close();
|
|
235
|
-
segment.unlink();
|
|
236
|
-
this.logger.info("decoder shm ring: segment released", {
|
|
237
|
-
meta: { segment: name }
|
|
238
|
-
});
|
|
239
|
-
} catch (err) {
|
|
240
|
-
this.logger.warn("decoder shm ring: segment release failed", {
|
|
241
|
-
meta: {
|
|
242
|
-
segment: name,
|
|
243
|
-
error: err instanceof Error ? err.message : String(err)
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}
|
|
68
|
+
let hash = 5381;
|
|
69
|
+
for (let i = 0; i < seed.length; i += 1) hash = (hash << 5) + hash + seed.charCodeAt(i) | 0;
|
|
70
|
+
return `csf.${(hash >>> 0).toString(36)}.${generation}`;
|
|
248
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* The decoder-side owner of one stream's shared-memory frame ring.
|
|
74
|
+
*
|
|
75
|
+
* Not constructed until a session actually uses the shm sink; the segment
|
|
76
|
+
* itself is created lazily on the first `writeFrame`.
|
|
77
|
+
*/
|
|
78
|
+
var DecoderFrameRingSink = class {
|
|
79
|
+
seed;
|
|
80
|
+
logger;
|
|
81
|
+
nodeId;
|
|
82
|
+
segment = null;
|
|
83
|
+
writer = null;
|
|
84
|
+
segmentName = null;
|
|
85
|
+
slotByteLength = 0;
|
|
86
|
+
generation = 0;
|
|
87
|
+
destroyed = false;
|
|
88
|
+
/** Frames committed into the ring across this sink's lifetime (all generations). */
|
|
89
|
+
framesWritten = 0;
|
|
90
|
+
constructor(options) {
|
|
91
|
+
const salt = Math.random().toString(36).slice(2, 8);
|
|
92
|
+
this.seed = `${options.seed}.${salt}`;
|
|
93
|
+
this.logger = options.logger;
|
|
94
|
+
this.nodeId = options.nodeId;
|
|
95
|
+
}
|
|
96
|
+
/** Whether a segment has been created (i.e. at least one frame written). */
|
|
97
|
+
get isArmed() {
|
|
98
|
+
return this.writer !== null;
|
|
99
|
+
}
|
|
100
|
+
/** The current segment name, or `null` before the first frame. */
|
|
101
|
+
get currentSegmentName() {
|
|
102
|
+
return this.segmentName;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Write one decoded frame into the ring and return its `FrameHandle`.
|
|
106
|
+
*
|
|
107
|
+
* On the first call (or after a geometry change that overflows the current
|
|
108
|
+
* slot) the segment is created / re-created sized for this frame. Returns
|
|
109
|
+
* `null` only when the sink has been destroyed.
|
|
110
|
+
*
|
|
111
|
+
* This is the copy-in convenience form (it copies `pixels` into the slot).
|
|
112
|
+
* The decoder's hot path uses the zero-copy {@link beginFrame} /
|
|
113
|
+
* {@link commitFrame} scatter-write pair instead — the scaler produces its
|
|
114
|
+
* packed output directly into the slot, eliminating the write-side memcpy.
|
|
115
|
+
*/
|
|
116
|
+
writeFrame(pixels, meta) {
|
|
117
|
+
if (this.destroyed) return null;
|
|
118
|
+
if (this.writer === null || (0, _camstack_shm_ring.computeSlotByteLength)(meta.width, meta.height, meta.format) > this.slotByteLength) this.recreateSegment((0, _camstack_shm_ring.computeSlotByteLength)(meta.width, meta.height, meta.format));
|
|
119
|
+
const writer = this.writer;
|
|
120
|
+
if (writer === null) return null;
|
|
121
|
+
const handle = writer.writeFrame(pixels, meta);
|
|
122
|
+
this.framesWritten += 1;
|
|
123
|
+
return handle;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Reserve a ring slot for a frame of the given geometry — the **zero-copy**
|
|
127
|
+
* scatter-write entry point (Phase 5 / D9 Task 7c).
|
|
128
|
+
*
|
|
129
|
+
* The segment is created / re-created here if this is the first frame or the
|
|
130
|
+
* geometry overflows the current slot capacity, so the slot is correctly
|
|
131
|
+
* sized before the caller fills it. The returned `buffer` is a writable view
|
|
132
|
+
* **directly over the mapped segment** — the node-av scaler scatters its
|
|
133
|
+
* packed output straight into it, with no intermediate copy. The caller MUST
|
|
134
|
+
* call {@link commitFrame} with the returned `slot` once the slot is filled.
|
|
135
|
+
*
|
|
136
|
+
* Returns `null` when the sink is destroyed or the segment cannot be created.
|
|
137
|
+
*/
|
|
138
|
+
beginFrame(width, height, format) {
|
|
139
|
+
if (this.destroyed) return null;
|
|
140
|
+
const requiredSlotBytes = (0, _camstack_shm_ring.computeSlotByteLength)(width, height, format);
|
|
141
|
+
if (this.writer === null || requiredSlotBytes > this.slotByteLength) this.recreateSegment(requiredSlotBytes);
|
|
142
|
+
const writer = this.writer;
|
|
143
|
+
if (writer === null) return null;
|
|
144
|
+
const { slot, buffer } = writer.beginFrame();
|
|
145
|
+
return {
|
|
146
|
+
slot,
|
|
147
|
+
buffer
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Publish the frame whose slot was reserved by {@link beginFrame} and filled
|
|
152
|
+
* in place by the caller. `slot` MUST be the value from the matching
|
|
153
|
+
* `beginFrame`. Returns the published `FrameHandle`, or `null` if the sink
|
|
154
|
+
* was destroyed (or the segment lost) between begin and commit.
|
|
155
|
+
*/
|
|
156
|
+
commitFrame(slot, meta) {
|
|
157
|
+
if (this.destroyed) return null;
|
|
158
|
+
const writer = this.writer;
|
|
159
|
+
if (writer === null) return null;
|
|
160
|
+
const handle = writer.commitFrame(slot, meta);
|
|
161
|
+
this.framesWritten += 1;
|
|
162
|
+
return handle;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Current shm ring usage — `null` until the first frame arms the segment.
|
|
166
|
+
* Surfaced through `decoder.getShmStats` so a downstream consumer can
|
|
167
|
+
* observe ring pressure (slot depth, byte budget, frames written).
|
|
168
|
+
*/
|
|
169
|
+
getShmStats() {
|
|
170
|
+
if (this.writer === null) return null;
|
|
171
|
+
return {
|
|
172
|
+
slotCount: this.writer.slotCount,
|
|
173
|
+
slotByteLength: this.slotByteLength,
|
|
174
|
+
segmentBytes: (0, _camstack_shm_ring.computeSegmentSize)(this.writer.slotCount, this.slotByteLength),
|
|
175
|
+
framesWritten: this.framesWritten
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Abandon a slot reserved by {@link beginFrame} **without publishing it** —
|
|
180
|
+
* the degenerate-path counterpart of {@link commitFrame}.
|
|
181
|
+
*
|
|
182
|
+
* A caller that reserved a slot but then could not produce valid pixels (no
|
|
183
|
+
* decoded source planes, or the scaler threw) MUST call this instead of
|
|
184
|
+
* `commitFrame`: it closes the open seqlock without advancing `writeIndex`,
|
|
185
|
+
* so no reader ever sees the slot's uninitialised bytes as a real frame, and
|
|
186
|
+
* no `FrameHandle` is handed downstream. `slot` MUST be the value from the
|
|
187
|
+
* matching `beginFrame`. A no-op if the sink was destroyed (or the segment
|
|
188
|
+
* lost) between begin and abort.
|
|
189
|
+
*/
|
|
190
|
+
abortFrame(slot) {
|
|
191
|
+
if (this.destroyed) return;
|
|
192
|
+
const writer = this.writer;
|
|
193
|
+
if (writer === null) return;
|
|
194
|
+
writer.abortFrame(slot);
|
|
195
|
+
}
|
|
196
|
+
/** Close + unlink the segment. Idempotent. */
|
|
197
|
+
destroy() {
|
|
198
|
+
if (this.destroyed) return;
|
|
199
|
+
this.destroyed = true;
|
|
200
|
+
this.releaseSegment();
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Create a fresh segment sized for at least `slotByteLength` bytes per slot,
|
|
204
|
+
* replacing any prior one. A re-create bumps the generation so the new
|
|
205
|
+
* segment has a distinct name — a consumer holding the old mapping is never
|
|
206
|
+
* silently handed a resized segment.
|
|
207
|
+
*/
|
|
208
|
+
recreateSegment(slotByteLength) {
|
|
209
|
+
this.releaseSegment();
|
|
210
|
+
this.generation += 1;
|
|
211
|
+
const name = makeSegmentName(this.seed, this.generation);
|
|
212
|
+
const slotCount = (0, _camstack_shm_ring.deriveSlotCount)(RING_BUDGET_BYTES, slotByteLength);
|
|
213
|
+
if (slotCount === _camstack_shm_ring.MIN_RING_SLOTS && _camstack_shm_ring.MIN_RING_SLOTS * slotByteLength > RING_BUDGET_BYTES) this.logger.warn("decoder shm ring: budget too small for resolution — using MIN slots", { meta: {
|
|
214
|
+
slotByteLength,
|
|
215
|
+
budgetMb: RING_BUDGET_MB
|
|
216
|
+
} });
|
|
217
|
+
const totalBytes = (0, _camstack_shm_ring.computeSegmentSize)(slotCount, slotByteLength);
|
|
218
|
+
try {
|
|
219
|
+
const segment = (0, _camstack_shm_ring.createSegment)(name, totalBytes);
|
|
220
|
+
this.segment = segment;
|
|
221
|
+
this.segmentName = name;
|
|
222
|
+
this.slotByteLength = slotByteLength;
|
|
223
|
+
this.writer = new _camstack_shm_ring.FrameRingWriter(segment.buffer, name, slotCount, slotByteLength, this.nodeId);
|
|
224
|
+
this.logger.info("decoder shm ring: segment created", { meta: {
|
|
225
|
+
segment: name,
|
|
226
|
+
slotCount,
|
|
227
|
+
slotByteLength,
|
|
228
|
+
totalBytes,
|
|
229
|
+
generation: this.generation
|
|
230
|
+
} });
|
|
231
|
+
} catch (err) {
|
|
232
|
+
this.segment = null;
|
|
233
|
+
this.writer = null;
|
|
234
|
+
this.segmentName = null;
|
|
235
|
+
this.slotByteLength = 0;
|
|
236
|
+
this.logger.error("decoder shm ring: segment create failed", { meta: {
|
|
237
|
+
segment: name,
|
|
238
|
+
slotByteLength,
|
|
239
|
+
error: err instanceof Error ? err.message : String(err)
|
|
240
|
+
} });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/** Unmap + unlink the current segment, if any. */
|
|
244
|
+
releaseSegment() {
|
|
245
|
+
const segment = this.segment;
|
|
246
|
+
if (segment === null) return;
|
|
247
|
+
this.segment = null;
|
|
248
|
+
this.writer = null;
|
|
249
|
+
const name = this.segmentName;
|
|
250
|
+
this.segmentName = null;
|
|
251
|
+
try {
|
|
252
|
+
segment.close();
|
|
253
|
+
segment.unlink();
|
|
254
|
+
this.logger.info("decoder shm ring: segment released", { meta: { segment: name } });
|
|
255
|
+
} catch (err) {
|
|
256
|
+
this.logger.warn("decoder shm ring: segment release failed", { meta: {
|
|
257
|
+
segment: name,
|
|
258
|
+
error: err instanceof Error ? err.message : String(err)
|
|
259
|
+
} });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
//#endregion
|
|
264
|
+
//#region src/decoder-nodeav/nodeav-decoder-session.ts
|
|
265
|
+
/** Map our canonical backend name to the node-av `AV_HWDEVICE_TYPE_*` constant. */
|
|
249
266
|
function backendToHwDeviceConst(backend, consts) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return consts.AV_HWDEVICE_TYPE_D3D11VA;
|
|
264
|
-
case "dxva2":
|
|
265
|
-
return consts.AV_HWDEVICE_TYPE_DXVA2;
|
|
266
|
-
case "amf":
|
|
267
|
-
return consts.AV_HWDEVICE_TYPE_AMF;
|
|
268
|
-
case "vdpau":
|
|
269
|
-
return consts.AV_HWDEVICE_TYPE_VDPAU;
|
|
270
|
-
case "drm":
|
|
271
|
-
return consts.AV_HWDEVICE_TYPE_DRM;
|
|
272
|
-
default:
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
267
|
+
switch (backend) {
|
|
268
|
+
case "videotoolbox": return consts.AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
|
|
269
|
+
case "cuda": return consts.AV_HWDEVICE_TYPE_CUDA;
|
|
270
|
+
case "nvdec": return consts.AV_HWDEVICE_TYPE_CUDA;
|
|
271
|
+
case "vaapi": return consts.AV_HWDEVICE_TYPE_VAAPI;
|
|
272
|
+
case "qsv": return consts.AV_HWDEVICE_TYPE_QSV;
|
|
273
|
+
case "d3d11va": return consts.AV_HWDEVICE_TYPE_D3D11VA;
|
|
274
|
+
case "dxva2": return consts.AV_HWDEVICE_TYPE_DXVA2;
|
|
275
|
+
case "amf": return consts.AV_HWDEVICE_TYPE_AMF;
|
|
276
|
+
case "vdpau": return consts.AV_HWDEVICE_TYPE_VDPAU;
|
|
277
|
+
case "drm": return consts.AV_HWDEVICE_TYPE_DRM;
|
|
278
|
+
default: return null;
|
|
279
|
+
}
|
|
275
280
|
}
|
|
281
|
+
/** Pick the hw pixel format for a given backend — used in get_format callback. */
|
|
276
282
|
function backendToHwPixFmt(backend, consts) {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
case "dxva2":
|
|
291
|
-
return consts.AV_PIX_FMT_DXVA2_VLD;
|
|
292
|
-
case "amf":
|
|
293
|
-
return consts.AV_PIX_FMT_D3D11;
|
|
294
|
-
case "vdpau":
|
|
295
|
-
return consts.AV_PIX_FMT_VDPAU;
|
|
296
|
-
case "drm":
|
|
297
|
-
return consts.AV_PIX_FMT_DRM_PRIME;
|
|
298
|
-
default:
|
|
299
|
-
return null;
|
|
300
|
-
}
|
|
283
|
+
switch (backend) {
|
|
284
|
+
case "videotoolbox": return consts.AV_PIX_FMT_VIDEOTOOLBOX;
|
|
285
|
+
case "cuda": return consts.AV_PIX_FMT_CUDA;
|
|
286
|
+
case "nvdec": return consts.AV_PIX_FMT_CUDA;
|
|
287
|
+
case "vaapi": return consts.AV_PIX_FMT_VAAPI;
|
|
288
|
+
case "qsv": return consts.AV_PIX_FMT_QSV;
|
|
289
|
+
case "d3d11va": return consts.AV_PIX_FMT_D3D11;
|
|
290
|
+
case "dxva2": return consts.AV_PIX_FMT_DXVA2_VLD;
|
|
291
|
+
case "amf": return consts.AV_PIX_FMT_D3D11;
|
|
292
|
+
case "vdpau": return consts.AV_PIX_FMT_VDPAU;
|
|
293
|
+
case "drm": return consts.AV_PIX_FMT_DRM_PRIME;
|
|
294
|
+
default: return null;
|
|
295
|
+
}
|
|
301
296
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
297
|
+
var _nav = null;
|
|
298
|
+
var _consts = null;
|
|
299
|
+
var _sharp = null;
|
|
305
300
|
async function getNodeAv() {
|
|
306
|
-
|
|
307
|
-
|
|
301
|
+
if (!_nav) _nav = await import("node-av");
|
|
302
|
+
return _nav;
|
|
308
303
|
}
|
|
309
304
|
async function getConstants() {
|
|
310
|
-
|
|
311
|
-
|
|
305
|
+
if (!_consts) _consts = await Promise.resolve().then(() => require("../constants-D65v6yp6.js"));
|
|
306
|
+
return _consts;
|
|
312
307
|
}
|
|
313
308
|
async function getSharp() {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
_sharp = mod.default;
|
|
317
|
-
}
|
|
318
|
-
return _sharp;
|
|
309
|
+
if (!_sharp) _sharp = (await import("sharp")).default;
|
|
310
|
+
return _sharp;
|
|
319
311
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
},
|
|
332
|
-
withTags() {
|
|
333
|
-
return noopLogger;
|
|
334
|
-
}
|
|
312
|
+
var noopLogger = {
|
|
313
|
+
debug() {},
|
|
314
|
+
info() {},
|
|
315
|
+
warn() {},
|
|
316
|
+
error() {},
|
|
317
|
+
child() {
|
|
318
|
+
return noopLogger;
|
|
319
|
+
},
|
|
320
|
+
withTags() {
|
|
321
|
+
return noopLogger;
|
|
322
|
+
}
|
|
335
323
|
};
|
|
336
|
-
class NodeAvDecoderSession {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
* cap caller may keep the frame across reads, so the pixels are copied
|
|
1402
|
-
* into a fresh detached `ArrayBuffer` here. This matches the existing
|
|
1403
|
-
* callback-path copy in `wireCallbackSink`.
|
|
1404
|
-
*/
|
|
1405
|
-
async getFrame(input) {
|
|
1406
|
-
const frame = this.frameReaders?.read(input.handle) ?? null;
|
|
1407
|
-
if (!frame) {
|
|
1408
|
-
this.getFrameMisses += 1;
|
|
1409
|
-
return null;
|
|
1410
|
-
}
|
|
1411
|
-
this.getFrameHits += 1;
|
|
1412
|
-
const arrayBuf = new ArrayBuffer(frame.data.byteLength);
|
|
1413
|
-
new Uint8Array(arrayBuf).set(frame.data);
|
|
1414
|
-
return {
|
|
1415
|
-
data: new Uint8Array(arrayBuf),
|
|
1416
|
-
width: frame.width,
|
|
1417
|
-
height: frame.height,
|
|
1418
|
-
format: frame.format,
|
|
1419
|
-
timestamp: frame.timestamp
|
|
1420
|
-
};
|
|
1421
|
-
}
|
|
1422
|
-
/**
|
|
1423
|
-
* shm ring usage stats for a `frameSink: 'shm'` session — slot geometry,
|
|
1424
|
-
* frames written, byte budget, plus this addon's running `getFrame`
|
|
1425
|
-
* hit/miss counters. Returns `null` for an unknown session or one whose
|
|
1426
|
-
* shm ring has not yet been armed by a first decoded frame.
|
|
1427
|
-
*/
|
|
1428
|
-
async getShmStats(input) {
|
|
1429
|
-
const session = this.sessions.get(input.sessionId);
|
|
1430
|
-
const stats = session?.frameRingSinkOrNull?.getShmStats() ?? null;
|
|
1431
|
-
if (!stats) return null;
|
|
1432
|
-
return {
|
|
1433
|
-
sessionId: input.sessionId,
|
|
1434
|
-
...stats,
|
|
1435
|
-
budgetMb: RING_BUDGET_MB,
|
|
1436
|
-
getFrameHits: this.getFrameHits,
|
|
1437
|
-
getFrameMisses: this.getFrameMisses
|
|
1438
|
-
};
|
|
1439
|
-
}
|
|
1440
|
-
async onShutdown() {
|
|
1441
|
-
this.ctx.logger.info("node-av decoder addon shutdown — destroying all sessions");
|
|
1442
|
-
const destroyPromises = [];
|
|
1443
|
-
for (const [sessionId, session] of this.sessions) {
|
|
1444
|
-
const unsub = this.unsubscribers.get(sessionId);
|
|
1445
|
-
if (unsub) unsub();
|
|
1446
|
-
destroyPromises.push(session.destroy());
|
|
1447
|
-
}
|
|
1448
|
-
await Promise.all(destroyPromises);
|
|
1449
|
-
this.sessions.clear();
|
|
1450
|
-
this.frameBuffers.clear();
|
|
1451
|
-
this.handleBuffers.clear();
|
|
1452
|
-
this.unsubscribers.clear();
|
|
1453
|
-
this.sessionMeta.clear();
|
|
1454
|
-
this.frameReaders?.close();
|
|
1455
|
-
this.frameReaders = null;
|
|
1456
|
-
}
|
|
1457
|
-
}
|
|
324
|
+
var NodeAvDecoderSession = class NodeAvDecoderSession {
|
|
325
|
+
config;
|
|
326
|
+
logger;
|
|
327
|
+
frameCallbacks = /* @__PURE__ */ new Set();
|
|
328
|
+
handleCallbacks = /* @__PURE__ */ new Set();
|
|
329
|
+
destroyed = false;
|
|
330
|
+
/**
|
|
331
|
+
* Frame delivery mode (see {@link DecoderFrameSink}). `'shm'` lazily
|
|
332
|
+
* constructs `frameRingSink` and routes every decoded frame through it.
|
|
333
|
+
*/
|
|
334
|
+
frameSink;
|
|
335
|
+
/**
|
|
336
|
+
* The shared-memory ring writer for this stream. Created lazily on the
|
|
337
|
+
* first decoded frame in `'shm'` mode (the segment cannot be sized until
|
|
338
|
+
* the output geometry is known) and torn down in `destroy`.
|
|
339
|
+
*/
|
|
340
|
+
frameRingSink = null;
|
|
341
|
+
parser = null;
|
|
342
|
+
codecCtx = null;
|
|
343
|
+
scaler = null;
|
|
344
|
+
avPacket = null;
|
|
345
|
+
avFrame = null;
|
|
346
|
+
dstFrame = null;
|
|
347
|
+
EAGAIN = -11;
|
|
348
|
+
PIX_FMT_GRAY8 = 8;
|
|
349
|
+
PIX_FMT_RGB24 = 2;
|
|
350
|
+
SWS_FAST_BILINEAR = 1;
|
|
351
|
+
/**
|
|
352
|
+
* Decoder output mode. Drives both the scaler's destination pixel
|
|
353
|
+
* format and whether sharp runs the JPEG encode at the end:
|
|
354
|
+
*
|
|
355
|
+
* - `'jpeg'` — scaler→RGB24 → sharp encode → emit JPEG bytes
|
|
356
|
+
* - `'rgb'` — scaler→RGB24 → emit raw RGB24 (no sharp)
|
|
357
|
+
* - `'gray'` — scaler→GRAY8 → emit raw GRAY8 (no sharp)
|
|
358
|
+
*
|
|
359
|
+
* The broker holds the policy decision on which mode to request based
|
|
360
|
+
* on its active subscribers; on-the-fly conversion (e.g. RGB→JPEG for
|
|
361
|
+
* a WebRTC consumer that joined while detection holds the decoder in
|
|
362
|
+
* RGB mode) happens broker-side via the per-frame conversion cache.
|
|
363
|
+
*/
|
|
364
|
+
outputMode;
|
|
365
|
+
sharpFn = null;
|
|
366
|
+
/**
|
|
367
|
+
* Backpressure for the sharp JPEG encode pipeline. The broker
|
|
368
|
+
* currently creates sessions with `maxFps: 0` (unlimited) and relies
|
|
369
|
+
* on per-subscriber throttling, so without a bound the
|
|
370
|
+
* fire-and-forget `sharp(...).toBuffer()` chain would accumulate
|
|
371
|
+
* unboundedly whenever sharp falls behind the decoder. Cap at
|
|
372
|
+
* `MAX_JPEG_INFLIGHT` pending encodes per session — any frame that
|
|
373
|
+
* arrives while the cap is saturated is dropped and counted.
|
|
374
|
+
*/
|
|
375
|
+
static MAX_JPEG_INFLIGHT = 2;
|
|
376
|
+
jpegEncodeInFlight = 0;
|
|
377
|
+
/**
|
|
378
|
+
* Map a `DecoderSessionConfig.outputFormat` value to one of the three
|
|
379
|
+
* native scaler/encoder modes the session understands. The cap-level
|
|
380
|
+
* format vocabulary is broader (it accepts `bgr`, `yuv420`) than what
|
|
381
|
+
* libav's scaler is wired for here — anything else degrades to RGB
|
|
382
|
+
* (the canonical raw mode) and the broker is expected to convert
|
|
383
|
+
* downstream if a subscriber needs a different shape.
|
|
384
|
+
*/
|
|
385
|
+
static resolveOutputMode(format) {
|
|
386
|
+
if (format === "jpeg" || format === void 0) return "jpeg";
|
|
387
|
+
if (format === "gray") return "gray";
|
|
388
|
+
return "rgb";
|
|
389
|
+
}
|
|
390
|
+
initialized = false;
|
|
391
|
+
initializing = false;
|
|
392
|
+
scalerInitializing = false;
|
|
393
|
+
/**
|
|
394
|
+
* Monotonic counter incremented by `updateConfig` whenever the
|
|
395
|
+
* scaler + dstFrame get invalidated (e.g. output format toggle).
|
|
396
|
+
* `initScaler` captures the current value at entry and aborts — or
|
|
397
|
+
* disposes the locally-built scaler — if the epoch moved while
|
|
398
|
+
* its async init was in flight. Without this, a toggle racing an
|
|
399
|
+
* in-flight init could leave two scalers allocated natively while
|
|
400
|
+
* `this.scaler` only holds a reference to one → libav leak.
|
|
401
|
+
*/
|
|
402
|
+
scalerEpoch = 0;
|
|
403
|
+
/**
|
|
404
|
+
* One-shot guard for the "first frame" diagnostic log + raw frame
|
|
405
|
+
* dump. Setting this synchronously inside `emitDecodedFrame`
|
|
406
|
+
* prevents re-entry — without it we were using `outputFrames === 0`
|
|
407
|
+
* which stays true until the async sharp encode callback runs, so
|
|
408
|
+
* several decoded frames could trigger the dump before the first
|
|
409
|
+
* JPEG landed.
|
|
410
|
+
*/
|
|
411
|
+
firstFrameLogged = false;
|
|
412
|
+
outWidth = 0;
|
|
413
|
+
outHeight = 0;
|
|
414
|
+
lastEmitTime = 0;
|
|
415
|
+
minIntervalMs;
|
|
416
|
+
inputPackets = 0;
|
|
417
|
+
outputFrames = 0;
|
|
418
|
+
droppedFrames = 0;
|
|
419
|
+
totalDecodeTimeMs = 0;
|
|
420
|
+
startTime = Date.now();
|
|
421
|
+
hwaccelPref;
|
|
422
|
+
hwaccelResolver;
|
|
423
|
+
/** Cluster node id stamped into every `FrameHandle` the `'shm'` sink emits. */
|
|
424
|
+
nodeId;
|
|
425
|
+
/** The backend that actually initialised successfully — `'none'` = software fallback. */
|
|
426
|
+
activeHwAccel = "none";
|
|
427
|
+
hwDevice = null;
|
|
428
|
+
swTransferFrame = null;
|
|
429
|
+
constructor(config, logger = noopLogger, options) {
|
|
430
|
+
this.config = { ...config };
|
|
431
|
+
const sessionTags = {};
|
|
432
|
+
if (typeof config.deviceId === "number") sessionTags["deviceId"] = config.deviceId;
|
|
433
|
+
if (typeof config.tag === "string" && config.tag.length > 0) sessionTags["tag"] = config.tag;
|
|
434
|
+
this.logger = Object.keys(sessionTags).length > 0 ? logger.withTags(sessionTags) : logger;
|
|
435
|
+
this.minIntervalMs = config.maxFps > 0 ? 1e3 / config.maxFps : 0;
|
|
436
|
+
this.outputMode = NodeAvDecoderSession.resolveOutputMode(config.outputFormat);
|
|
437
|
+
this.hwaccelPref = options?.hwaccel ?? "auto";
|
|
438
|
+
this.hwaccelResolver = options?.hwaccelResolver ?? null;
|
|
439
|
+
this.frameSink = options?.frameSink ?? "callback";
|
|
440
|
+
this.nodeId = options?.nodeId ?? "local";
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* The shared-memory ring sink for this stream, or `null` before the first
|
|
444
|
+
* `'shm'`-mode frame lazily creates it. Exposed so the owning addon can
|
|
445
|
+
* surface ring stats via `decoder.getShmStats`.
|
|
446
|
+
*/
|
|
447
|
+
get frameRingSinkOrNull() {
|
|
448
|
+
return this.frameRingSink;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Lazily build the shared-memory ring sink for this stream. The segment
|
|
452
|
+
* itself is still created lazily by the sink on its first `writeFrame`,
|
|
453
|
+
* once the decoded geometry is known.
|
|
454
|
+
*/
|
|
455
|
+
ensureFrameRingSink() {
|
|
456
|
+
if (this.frameRingSink === null) {
|
|
457
|
+
const seedParts = [];
|
|
458
|
+
if (typeof this.config.deviceId === "number") seedParts.push(String(this.config.deviceId));
|
|
459
|
+
if (typeof this.config.tag === "string" && this.config.tag.length > 0) seedParts.push(this.config.tag);
|
|
460
|
+
const seed = seedParts.length > 0 ? seedParts.join(":") : "anon";
|
|
461
|
+
this.frameRingSink = new DecoderFrameRingSink({
|
|
462
|
+
seed,
|
|
463
|
+
logger: this.logger,
|
|
464
|
+
nodeId: this.nodeId
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
return this.frameRingSink;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Resolve the backend preference list and try each one against
|
|
471
|
+
* node-av's HW context APIs. The first backend whose
|
|
472
|
+
* `HardwareDeviceContext.create()` succeeds gets attached to
|
|
473
|
+
* `codecCtx.hwDeviceCtx` + its hw pixel format registered via
|
|
474
|
+
* `setHardwarePixelFormat`. On any failure, falls through to the
|
|
475
|
+
* next backend; if all fail, returns with `activeHwAccel='none'`
|
|
476
|
+
* and the decoder runs in software on the same context.
|
|
477
|
+
*/
|
|
478
|
+
async tryAttachHwAccel(nav, C) {
|
|
479
|
+
if (!this.codecCtx) return;
|
|
480
|
+
if (this.hwaccelPref === "none") {
|
|
481
|
+
this.activeHwAccel = "none";
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const explicit = this.hwaccelPref === "auto" ? null : this.hwaccelPref;
|
|
485
|
+
const resolution = this.hwaccelResolver ? await this.hwaccelResolver.resolve(explicit) : explicit ? {
|
|
486
|
+
preferred: [explicit],
|
|
487
|
+
rationale: "explicit (no resolver)"
|
|
488
|
+
} : {
|
|
489
|
+
preferred: [],
|
|
490
|
+
rationale: "auto + no resolver → software"
|
|
491
|
+
};
|
|
492
|
+
if (resolution.preferred.length === 0) {
|
|
493
|
+
this.activeHwAccel = "none";
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
for (const backend of resolution.preferred) {
|
|
497
|
+
const deviceType = backendToHwDeviceConst(backend, C);
|
|
498
|
+
const hwPixFmt = backendToHwPixFmt(backend, C);
|
|
499
|
+
if (!deviceType || !hwPixFmt) continue;
|
|
500
|
+
const device = new nav.HardwareDeviceContext();
|
|
501
|
+
const rc = device.create(deviceType);
|
|
502
|
+
if (rc < 0) {
|
|
503
|
+
this.logger.warn("node-av: hwaccel device create failed — trying next", { meta: {
|
|
504
|
+
backend,
|
|
505
|
+
rc
|
|
506
|
+
} });
|
|
507
|
+
device.free();
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
try {
|
|
511
|
+
this.codecCtx.hwDeviceCtx = device;
|
|
512
|
+
this.codecCtx.setHardwarePixelFormat(hwPixFmt);
|
|
513
|
+
} catch (err) {
|
|
514
|
+
this.logger.warn("node-av: hwaccel context attach failed — trying next", { meta: {
|
|
515
|
+
backend,
|
|
516
|
+
error: require_dist.errMsg(err)
|
|
517
|
+
} });
|
|
518
|
+
device.free();
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
this.hwDevice = device;
|
|
522
|
+
this.activeHwAccel = backend;
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
this.logger.warn("node-av: no hwaccel backend initialised — using software", { meta: {
|
|
526
|
+
attempted: resolution.preferred.join(","),
|
|
527
|
+
rationale: resolution.rationale
|
|
528
|
+
} });
|
|
529
|
+
this.activeHwAccel = "none";
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Download a HW frame (format == hw pix fmt) into a SW frame so the
|
|
533
|
+
* rest of the pipeline (scaler, JPEG encoder, grayscale passthrough)
|
|
534
|
+
* handles it identically to the pure-software path. Uses the sync
|
|
535
|
+
* variant so the synchronous receive loop below doesn't need to be
|
|
536
|
+
* async-ified. Returns `null` on transfer failure, meaning the
|
|
537
|
+
* caller should drop the frame.
|
|
538
|
+
*/
|
|
539
|
+
transferHwFrame(hwFrame) {
|
|
540
|
+
if (this.activeHwAccel === "none" || !this.swTransferFrame) return hwFrame;
|
|
541
|
+
const rc = hwFrame.hwframeTransferDataSync(this.swTransferFrame, 0);
|
|
542
|
+
if (rc < 0) {
|
|
543
|
+
this.logger.warn("node-av: hwframeTransferData failed", { meta: { rc } });
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
return this.swTransferFrame;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Initialize the decoder pipeline on the first keyframe.
|
|
550
|
+
* After this returns, all hot-path methods are fully synchronous (except JPEG encode).
|
|
551
|
+
*/
|
|
552
|
+
async initDecoder() {
|
|
553
|
+
if (this.initialized || this.initializing || this.destroyed) return;
|
|
554
|
+
this.initializing = true;
|
|
555
|
+
try {
|
|
556
|
+
const nav = await getNodeAv();
|
|
557
|
+
const C = await getConstants();
|
|
558
|
+
this.EAGAIN = C.AVERROR_EAGAIN;
|
|
559
|
+
this.PIX_FMT_GRAY8 = C.AV_PIX_FMT_GRAY8;
|
|
560
|
+
this.PIX_FMT_RGB24 = C.AV_PIX_FMT_RGB24;
|
|
561
|
+
this.SWS_FAST_BILINEAR = C.SWS_FAST_BILINEAR;
|
|
562
|
+
if (this.outputMode === "jpeg") this.sharpFn = await getSharp();
|
|
563
|
+
nav.Log.setLevel(C.AV_LOG_FATAL);
|
|
564
|
+
const codecId = this.config.codec === "h265" || this.config.codec === "hevc" ? C.AV_CODEC_ID_HEVC : C.AV_CODEC_ID_H264;
|
|
565
|
+
this.parser = new nav.CodecParser();
|
|
566
|
+
this.parser.init(codecId);
|
|
567
|
+
const codec = nav.Codec.findDecoder(codecId);
|
|
568
|
+
if (!codec) {
|
|
569
|
+
this.logger.error("node-av: no decoder found", { meta: { codec: this.config.codec } });
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
this.codecCtx = new nav.CodecContext();
|
|
573
|
+
this.codecCtx.allocContext3(codec);
|
|
574
|
+
if (this.config.width && this.config.height) {
|
|
575
|
+
this.codecCtx.width = this.config.width;
|
|
576
|
+
this.codecCtx.height = this.config.height;
|
|
577
|
+
}
|
|
578
|
+
this.codecCtx.threadCount = 1;
|
|
579
|
+
await this.tryAttachHwAccel(nav, C);
|
|
580
|
+
const ret = await this.codecCtx.open2(codec);
|
|
581
|
+
if (ret < 0) if (this.activeHwAccel !== "none") {
|
|
582
|
+
this.logger.warn("node-av: open2 failed with hwaccel — retrying in software", { meta: {
|
|
583
|
+
ret,
|
|
584
|
+
hwAccel: this.activeHwAccel
|
|
585
|
+
} });
|
|
586
|
+
this.hwDevice?.free();
|
|
587
|
+
this.hwDevice = null;
|
|
588
|
+
this.activeHwAccel = "none";
|
|
589
|
+
this.codecCtx = new nav.CodecContext();
|
|
590
|
+
this.codecCtx.allocContext3(codec);
|
|
591
|
+
if (this.config.width && this.config.height) {
|
|
592
|
+
this.codecCtx.width = this.config.width;
|
|
593
|
+
this.codecCtx.height = this.config.height;
|
|
594
|
+
}
|
|
595
|
+
this.codecCtx.threadCount = 1;
|
|
596
|
+
const retry = await this.codecCtx.open2(codec);
|
|
597
|
+
if (retry < 0) {
|
|
598
|
+
this.logger.error("node-av: failed to open decoder (sw fallback)", { meta: { ret: retry } });
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
} else {
|
|
602
|
+
this.logger.error("node-av: failed to open decoder", { meta: { ret } });
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (this.activeHwAccel !== "none") {
|
|
606
|
+
this.swTransferFrame = new nav.Frame();
|
|
607
|
+
this.swTransferFrame.alloc();
|
|
608
|
+
}
|
|
609
|
+
this.avPacket = new nav.Packet();
|
|
610
|
+
this.avPacket.alloc();
|
|
611
|
+
this.avFrame = new nav.Frame();
|
|
612
|
+
this.avFrame.alloc();
|
|
613
|
+
this.initialized = true;
|
|
614
|
+
this.logger.info("node-av push decoder initialized", { meta: {
|
|
615
|
+
codec: this.config.codec,
|
|
616
|
+
output: this.outputMode,
|
|
617
|
+
hwAccel: this.activeHwAccel
|
|
618
|
+
} });
|
|
619
|
+
} catch (err) {
|
|
620
|
+
this.logger.error("node-av init error", { meta: { error: require_dist.errMsg(err) } });
|
|
621
|
+
} finally {
|
|
622
|
+
this.initializing = false;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Initialize the scaler after the first frame tells us the actual
|
|
627
|
+
* dimensions. Output pixel format: RGB24 for JPEG encoding, GRAY8
|
|
628
|
+
* for raw motion.
|
|
629
|
+
*
|
|
630
|
+
* Builds `scaler` + `dstFrame` on local variables and publishes
|
|
631
|
+
* them onto `this` in a single atomic step at the end. Captures
|
|
632
|
+
* `scalerEpoch` at entry; if `updateConfig` invalidated the scaler
|
|
633
|
+
* while this init was in flight (epoch mismatch), the locally
|
|
634
|
+
* built pair is disposed and discarded so the later init wins.
|
|
635
|
+
* Without the local-first approach, partial state (scaler set,
|
|
636
|
+
* dstFrame still null) could be observed by a concurrent
|
|
637
|
+
* `emitDecodedFrame` call.
|
|
638
|
+
*/
|
|
639
|
+
async initScaler(srcW, srcH, srcFmt) {
|
|
640
|
+
if (this.scalerInitializing) return;
|
|
641
|
+
this.scalerInitializing = true;
|
|
642
|
+
const myEpoch = this.scalerEpoch;
|
|
643
|
+
let localScaler = null;
|
|
644
|
+
let localDstFrame = null;
|
|
645
|
+
try {
|
|
646
|
+
const nav = await getNodeAv();
|
|
647
|
+
if (this.destroyed || myEpoch !== this.scalerEpoch) return;
|
|
648
|
+
const scale = this.config.scale > 1 ? this.config.scale : 1;
|
|
649
|
+
const maxW = Math.floor(640 / scale);
|
|
650
|
+
const outWidth = Math.min(srcW, maxW);
|
|
651
|
+
const outHeight = Math.round(outWidth * srcH / srcW);
|
|
652
|
+
const dstFmt = this.outputMode === "gray" ? this.PIX_FMT_GRAY8 : this.PIX_FMT_RGB24;
|
|
653
|
+
const fmtName = this.outputMode === "gray" ? "gray8" : "rgb24";
|
|
654
|
+
localScaler = new nav.SoftwareScaleContext();
|
|
655
|
+
localScaler.getContext(srcW, srcH, srcFmt, outWidth, outHeight, dstFmt, this.SWS_FAST_BILINEAR);
|
|
656
|
+
const ret = localScaler.initContext();
|
|
657
|
+
if (ret < 0) {
|
|
658
|
+
this.logger.error("node-av: sws_init_context failed", { meta: { ret } });
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
localDstFrame = new nav.Frame();
|
|
662
|
+
localDstFrame.alloc();
|
|
663
|
+
localDstFrame.width = outWidth;
|
|
664
|
+
localDstFrame.height = outHeight;
|
|
665
|
+
localDstFrame.format = dstFmt;
|
|
666
|
+
const allocRet = localDstFrame.allocBuffer();
|
|
667
|
+
if (allocRet < 0) {
|
|
668
|
+
this.logger.error("node-av: dst frame allocBuffer failed", { meta: { ret: allocRet } });
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
if (this.destroyed || myEpoch !== this.scalerEpoch) return;
|
|
672
|
+
this.scaler?.[Symbol.dispose]?.();
|
|
673
|
+
this.dstFrame?.[Symbol.dispose]?.();
|
|
674
|
+
this.scaler = localScaler;
|
|
675
|
+
this.dstFrame = localDstFrame;
|
|
676
|
+
this.outWidth = outWidth;
|
|
677
|
+
this.outHeight = outHeight;
|
|
678
|
+
localScaler = null;
|
|
679
|
+
localDstFrame = null;
|
|
680
|
+
this.logger.info("node-av scaler initialized", { meta: {
|
|
681
|
+
srcWidth: srcW,
|
|
682
|
+
srcHeight: srcH,
|
|
683
|
+
outWidth,
|
|
684
|
+
outHeight,
|
|
685
|
+
format: fmtName
|
|
686
|
+
} });
|
|
687
|
+
} catch (err) {
|
|
688
|
+
this.logger.error("Scaler init failed", { meta: { error: require_dist.errMsg(err) } });
|
|
689
|
+
} finally {
|
|
690
|
+
localScaler?.[Symbol.dispose]?.();
|
|
691
|
+
localDstFrame?.[Symbol.dispose]?.();
|
|
692
|
+
this.scalerInitializing = false;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
pushPacket(packet) {
|
|
696
|
+
if (this.destroyed) return;
|
|
697
|
+
this.inputPackets++;
|
|
698
|
+
if (!this.initialized && !this.initializing && packet.keyframe) {
|
|
699
|
+
this.initDecoder().then(() => {
|
|
700
|
+
if (this.initialized) this.decodeRawData(packet.data, packet.pts ?? Date.now());
|
|
701
|
+
}).catch((err) => {
|
|
702
|
+
this.logger.error("node-av init failed", { meta: { error: require_dist.errMsg(err) } });
|
|
703
|
+
});
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
if (!this.initialized) return;
|
|
707
|
+
this.decodeRawData(packet.data, packet.pts ?? Date.now());
|
|
708
|
+
}
|
|
709
|
+
decodeRawData(data, pts) {
|
|
710
|
+
if (!this.parser || !this.codecCtx || !this.avPacket || !this.avFrame) return;
|
|
711
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
712
|
+
const bigPts = BigInt(pts);
|
|
713
|
+
let offset = 0;
|
|
714
|
+
while (offset < buf.length) {
|
|
715
|
+
const remaining = buf.subarray(offset);
|
|
716
|
+
const consumed = this.parser.parse2(this.codecCtx, this.avPacket, remaining, bigPts, bigPts, offset);
|
|
717
|
+
if (consumed < 0) {
|
|
718
|
+
this.logger.warn("node-av parser error", { meta: { ret: consumed } });
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
offset += consumed;
|
|
722
|
+
if (this.avPacket.size > 0) {
|
|
723
|
+
this.decodePacket();
|
|
724
|
+
this.avPacket.unref();
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
decodePacket() {
|
|
729
|
+
if (!this.codecCtx || !this.avFrame) return;
|
|
730
|
+
const sendRet = this.codecCtx.sendPacketSync(this.avPacket);
|
|
731
|
+
if (sendRet < 0 && sendRet !== this.EAGAIN) {
|
|
732
|
+
this.logger.warn("node-av sendPacket error", { meta: { ret: sendRet } });
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
while (true) {
|
|
736
|
+
if (this.codecCtx.receiveFrameSync(this.avFrame) < 0) break;
|
|
737
|
+
const frame = this.transferHwFrame(this.avFrame);
|
|
738
|
+
if (!frame) continue;
|
|
739
|
+
this.emitDecodedFrame(frame);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
emitDecodedFrame(frame) {
|
|
743
|
+
const now = performance.now();
|
|
744
|
+
if (this.minIntervalMs > 0 && now - this.lastEmitTime < this.minIntervalMs) {
|
|
745
|
+
this.droppedFrames++;
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
if (!this.scaler && !this.scalerInitializing) {
|
|
749
|
+
this.initScaler(frame.width, frame.height, frame.format);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
if (!this.dstFrame || !this.scaler) return;
|
|
753
|
+
const decodeStart = performance.now();
|
|
754
|
+
if (this.frameSink === "shm" && this.outputMode !== "jpeg") {
|
|
755
|
+
this.scaleIntoRingSlot(frame, decodeStart);
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
try {
|
|
759
|
+
this.dstFrame.makeWritable();
|
|
760
|
+
this.scaler.scaleFrameSync(this.dstFrame, frame);
|
|
761
|
+
} catch (err) {
|
|
762
|
+
this.logger.warn("node-av scale error", { meta: { error: require_dist.errMsg(err) } });
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
if (!this.firstFrameLogged) {
|
|
766
|
+
this.firstFrameLogged = true;
|
|
767
|
+
const channels = this.outputMode === "gray" ? 1 : 3;
|
|
768
|
+
const ls = this.dstFrame.linesize;
|
|
769
|
+
const buf = this.dstFrame.toBuffer();
|
|
770
|
+
this.logger.info("dstFrame after scale", { meta: {
|
|
771
|
+
phase: "frame-debug",
|
|
772
|
+
width: this.dstFrame.width,
|
|
773
|
+
height: this.dstFrame.height,
|
|
774
|
+
linesize: [
|
|
775
|
+
ls[0],
|
|
776
|
+
ls[1],
|
|
777
|
+
ls[2]
|
|
778
|
+
],
|
|
779
|
+
expectedStride: this.dstFrame.width * channels,
|
|
780
|
+
bufLen: buf.length,
|
|
781
|
+
expectedPacked: this.dstFrame.width * channels * this.dstFrame.height,
|
|
782
|
+
srcFormat: frame.format ?? "?"
|
|
783
|
+
} });
|
|
784
|
+
if (this.outputMode !== "gray") import("node:fs").then((fsModule) => {
|
|
785
|
+
import("node:path").then((pathModule) => {
|
|
786
|
+
const dumpPath = pathModule.join(process.cwd(), "camstack-data", "debug-frame-rgb24.raw");
|
|
787
|
+
fsModule.writeFileSync(dumpPath, buf);
|
|
788
|
+
this.logger.info("Dumped first RGB24 frame", { meta: {
|
|
789
|
+
phase: "frame-debug",
|
|
790
|
+
path: dumpPath,
|
|
791
|
+
bytes: buf.length
|
|
792
|
+
} });
|
|
793
|
+
}).catch(() => {});
|
|
794
|
+
}).catch(() => {});
|
|
795
|
+
}
|
|
796
|
+
const rawBuf = this.extractPackedBuffer(this.dstFrame);
|
|
797
|
+
if (this.outputMode === "jpeg") this.encodeAndEmitJpeg(rawBuf, decodeStart);
|
|
798
|
+
else if (this.outputMode === "rgb") this.emitRawFrame(rawBuf, "rgb", decodeStart);
|
|
799
|
+
else this.emitRawFrame(rawBuf, "gray", decodeStart);
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Scale a decoded frame **directly into a shared-memory ring slot** — the
|
|
803
|
+
* zero write-side copy path (Phase 5 / D9 Task 7c).
|
|
804
|
+
*
|
|
805
|
+
* `beginFrame` reserves the slot (its seqlock open / odd — readers skip it);
|
|
806
|
+
* `SoftwareScaleContext.scaleSync` (the low-level `sws_scale` mapping) writes
|
|
807
|
+
* the packed pixels straight into the slot buffer with `linesize = width ×
|
|
808
|
+
* bpp` — no linesize padding, so there is nothing to strip and nothing to
|
|
809
|
+
* memcpy; `commitFrame` writes the metadata, closes the seqlock and publishes
|
|
810
|
+
* the slot. The decoded source planes feed `scaleSync` as `srcSlice` +
|
|
811
|
+
* `srcStride` (`frame.data` / `frame.linesize`).
|
|
812
|
+
*
|
|
813
|
+
* Used only for raw output formats (rgb/bgr/gray). The jpeg path keeps the
|
|
814
|
+
* `dstFrame` + sharp encode route because sharp must consume an RGB buffer
|
|
815
|
+
* before the final (variable-length) jpeg bytes exist.
|
|
816
|
+
*/
|
|
817
|
+
scaleIntoRingSlot(frame, decodeStart) {
|
|
818
|
+
if (!this.scaler) return;
|
|
819
|
+
const format = this.outputMode === "gray" ? "gray" : "rgb";
|
|
820
|
+
const channels = this.outputMode === "gray" ? 1 : 3;
|
|
821
|
+
const sink = this.ensureFrameRingSink();
|
|
822
|
+
const reserved = sink.beginFrame(this.outWidth, this.outHeight, format);
|
|
823
|
+
if (reserved === null) {
|
|
824
|
+
this.droppedFrames++;
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
const srcPlanes = frame.data;
|
|
828
|
+
if (!srcPlanes || srcPlanes.length === 0) {
|
|
829
|
+
sink.abortFrame(reserved.slot);
|
|
830
|
+
this.droppedFrames++;
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
const dstStride = this.outWidth * channels;
|
|
834
|
+
try {
|
|
835
|
+
const srcStrides = Array.from(frame.linesize).slice(0, srcPlanes.length);
|
|
836
|
+
this.scaler.scaleSync(Array.from(srcPlanes), srcStrides, 0, frame.height, [reserved.buffer], [dstStride]);
|
|
837
|
+
} catch (err) {
|
|
838
|
+
this.logger.warn("node-av scale-into-slot error", { meta: { error: require_dist.errMsg(err) } });
|
|
839
|
+
sink.abortFrame(reserved.slot);
|
|
840
|
+
this.droppedFrames++;
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
const decodeMs = performance.now() - decodeStart;
|
|
844
|
+
this.totalDecodeTimeMs += decodeMs;
|
|
845
|
+
this.outputFrames++;
|
|
846
|
+
this.lastEmitTime = performance.now();
|
|
847
|
+
if (!this.firstFrameLogged) {
|
|
848
|
+
this.firstFrameLogged = true;
|
|
849
|
+
this.logger.info("node-av: scaled directly into shm ring slot", { meta: {
|
|
850
|
+
phase: "frame-debug",
|
|
851
|
+
width: this.outWidth,
|
|
852
|
+
height: this.outHeight,
|
|
853
|
+
dstStride,
|
|
854
|
+
format
|
|
855
|
+
} });
|
|
856
|
+
}
|
|
857
|
+
if (this.outputFrames === 1 || this.outputFrames % 500 === 0) this.logger.info("node-av frame emitted", { meta: {
|
|
858
|
+
frameNumber: this.outputFrames,
|
|
859
|
+
width: this.outWidth,
|
|
860
|
+
height: this.outHeight,
|
|
861
|
+
format,
|
|
862
|
+
decodeMs,
|
|
863
|
+
sink: "shm",
|
|
864
|
+
subs: this.handleCallbacks.size
|
|
865
|
+
} });
|
|
866
|
+
const handle = sink.commitFrame(reserved.slot, {
|
|
867
|
+
width: this.outWidth,
|
|
868
|
+
height: this.outHeight,
|
|
869
|
+
format,
|
|
870
|
+
pts: performance.now(),
|
|
871
|
+
byteLength: dstStride * this.outHeight
|
|
872
|
+
});
|
|
873
|
+
if (handle === null) {
|
|
874
|
+
this.droppedFrames++;
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
const delivered = {
|
|
878
|
+
handle,
|
|
879
|
+
timestamp: Date.now()
|
|
880
|
+
};
|
|
881
|
+
for (const cb of this.handleCallbacks) cb(delivered);
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Extract packed pixel buffer from a decoded frame.
|
|
885
|
+
* FFmpeg's av_frame_get_buffer() may pad each row to alignment (32/64 bytes).
|
|
886
|
+
* Sharp and WASM consumers expect tightly-packed rows (stride = width * channels).
|
|
887
|
+
* If linesize matches expected stride, return the buffer directly (zero-copy).
|
|
888
|
+
*/
|
|
889
|
+
extractPackedBuffer(frame) {
|
|
890
|
+
const channels = this.outputMode === "gray" ? 1 : 3;
|
|
891
|
+
const expectedStride = frame.width * channels;
|
|
892
|
+
const actualStride = frame.linesize[0] ?? expectedStride;
|
|
893
|
+
const src = frame.data?.[0];
|
|
894
|
+
if (!src) return frame.toBuffer();
|
|
895
|
+
if (actualStride === expectedStride) return Buffer.from(src.buffer, src.byteOffset, expectedStride * frame.height);
|
|
896
|
+
const dst = Buffer.allocUnsafe(expectedStride * frame.height);
|
|
897
|
+
for (let y = 0; y < frame.height; y++) src.copy(dst, y * expectedStride, y * actualStride, y * actualStride + expectedStride);
|
|
898
|
+
return dst;
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Encode RGB24 raw buffer as JPEG and emit.
|
|
902
|
+
*
|
|
903
|
+
* Drops the frame (and counts it) when `MAX_JPEG_INFLIGHT` encodes
|
|
904
|
+
* are already pending — prevents unbounded growth of the
|
|
905
|
+
* fire-and-forget promise chain when sharp cannot keep up with the
|
|
906
|
+
* decode rate.
|
|
907
|
+
*/
|
|
908
|
+
encodeAndEmitJpeg(rgb, decodeStart) {
|
|
909
|
+
if (!this.sharpFn) return;
|
|
910
|
+
if (this.jpegEncodeInFlight >= NodeAvDecoderSession.MAX_JPEG_INFLIGHT) {
|
|
911
|
+
this.droppedFrames++;
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
this.jpegEncodeInFlight++;
|
|
915
|
+
this.sharpFn(rgb, { raw: {
|
|
916
|
+
width: this.outWidth,
|
|
917
|
+
height: this.outHeight,
|
|
918
|
+
channels: 3
|
|
919
|
+
} }).jpeg({
|
|
920
|
+
quality: 80,
|
|
921
|
+
mozjpeg: false
|
|
922
|
+
}).toBuffer().then((jpegBuf) => {
|
|
923
|
+
if (this.destroyed) return;
|
|
924
|
+
this.emitRawFrame(jpegBuf, "jpeg", decodeStart);
|
|
925
|
+
}).catch((err) => {
|
|
926
|
+
this.logger.warn("sharp jpeg encode error", { meta: { error: require_dist.errMsg(err) } });
|
|
927
|
+
}).finally(() => {
|
|
928
|
+
this.jpegEncodeInFlight--;
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
emitRawFrame(data, format, decodeStart) {
|
|
932
|
+
const decodeMs = performance.now() - decodeStart;
|
|
933
|
+
this.totalDecodeTimeMs += decodeMs;
|
|
934
|
+
this.outputFrames++;
|
|
935
|
+
this.lastEmitTime = performance.now();
|
|
936
|
+
if (this.outputFrames === 1 || this.outputFrames % 500 === 0) this.logger.info("node-av frame emitted", { meta: {
|
|
937
|
+
frameNumber: this.outputFrames,
|
|
938
|
+
width: this.outWidth,
|
|
939
|
+
height: this.outHeight,
|
|
940
|
+
format,
|
|
941
|
+
decodeMs,
|
|
942
|
+
sink: this.frameSink,
|
|
943
|
+
subs: this.frameSink === "shm" ? this.handleCallbacks.size : this.frameCallbacks.size
|
|
944
|
+
} });
|
|
945
|
+
if (this.frameSink === "shm") {
|
|
946
|
+
this.emitShmFrame(data, format);
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
const decodedFrame = {
|
|
950
|
+
data,
|
|
951
|
+
width: this.outWidth,
|
|
952
|
+
height: this.outHeight,
|
|
953
|
+
format,
|
|
954
|
+
timestamp: Date.now()
|
|
955
|
+
};
|
|
956
|
+
for (const cb of this.frameCallbacks) cb(decodedFrame);
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Write a decoded frame into this stream's shared-memory ring and deliver
|
|
960
|
+
* the resulting `FrameHandle` to `onFrameHandle` subscribers. No pixel
|
|
961
|
+
* `Buffer` is handed to callbacks — consumers open the segment and read the
|
|
962
|
+
* pixels zero-copy via a `FrameRingReader`.
|
|
963
|
+
*/
|
|
964
|
+
emitShmFrame(data, format) {
|
|
965
|
+
const handle = this.ensureFrameRingSink().writeFrame(data, {
|
|
966
|
+
width: this.outWidth,
|
|
967
|
+
height: this.outHeight,
|
|
968
|
+
format,
|
|
969
|
+
pts: performance.now(),
|
|
970
|
+
byteLength: data.byteLength
|
|
971
|
+
});
|
|
972
|
+
if (handle === null) {
|
|
973
|
+
this.droppedFrames++;
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
const frame = {
|
|
977
|
+
handle,
|
|
978
|
+
timestamp: Date.now()
|
|
979
|
+
};
|
|
980
|
+
for (const cb of this.handleCallbacks) cb(frame);
|
|
981
|
+
}
|
|
982
|
+
onFrame(callback) {
|
|
983
|
+
this.frameCallbacks.add(callback);
|
|
984
|
+
return () => {
|
|
985
|
+
this.frameCallbacks.delete(callback);
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
/**
|
|
989
|
+
* Subscribe to shared-memory frame handles. Fires only when the session was
|
|
990
|
+
* created with `frameSink: 'shm'`; in the legacy `'callback'` mode no
|
|
991
|
+
* handles are produced and this subscription never fires.
|
|
992
|
+
*/
|
|
993
|
+
onFrameHandle(callback) {
|
|
994
|
+
this.handleCallbacks.add(callback);
|
|
995
|
+
return () => {
|
|
996
|
+
this.handleCallbacks.delete(callback);
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
updateConfig(update) {
|
|
1000
|
+
const prevFormat = this.config.outputFormat;
|
|
1001
|
+
this.config = {
|
|
1002
|
+
...this.config,
|
|
1003
|
+
...update
|
|
1004
|
+
};
|
|
1005
|
+
if (update.maxFps !== void 0) this.minIntervalMs = update.maxFps > 0 ? 1e3 / update.maxFps : 0;
|
|
1006
|
+
if (update.outputFormat !== void 0 && update.outputFormat !== prevFormat) {
|
|
1007
|
+
const prevMode = this.outputMode;
|
|
1008
|
+
this.outputMode = NodeAvDecoderSession.resolveOutputMode(update.outputFormat);
|
|
1009
|
+
if (this.outputMode === prevMode) return;
|
|
1010
|
+
this.scalerEpoch++;
|
|
1011
|
+
if (this.scaler) {
|
|
1012
|
+
this.scaler[Symbol.dispose]?.();
|
|
1013
|
+
this.scaler = null;
|
|
1014
|
+
}
|
|
1015
|
+
if (this.dstFrame) {
|
|
1016
|
+
this.dstFrame[Symbol.dispose]?.();
|
|
1017
|
+
this.dstFrame = null;
|
|
1018
|
+
}
|
|
1019
|
+
if (this.outputMode === "jpeg" && !this.sharpFn) getSharp().then((fn) => {
|
|
1020
|
+
this.sharpFn = fn;
|
|
1021
|
+
}).catch(() => {});
|
|
1022
|
+
this.logger.info("node-av: output format changed — scaler will reinit", { meta: {
|
|
1023
|
+
from: prevFormat,
|
|
1024
|
+
to: update.outputFormat,
|
|
1025
|
+
mode: this.outputMode
|
|
1026
|
+
} });
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
async destroy() {
|
|
1030
|
+
if (this.destroyed) return;
|
|
1031
|
+
this.destroyed = true;
|
|
1032
|
+
this.frameCallbacks.clear();
|
|
1033
|
+
this.handleCallbacks.clear();
|
|
1034
|
+
this.frameRingSink?.destroy();
|
|
1035
|
+
this.frameRingSink = null;
|
|
1036
|
+
this.dstFrame?.[Symbol.dispose]?.();
|
|
1037
|
+
this.avFrame?.[Symbol.dispose]?.();
|
|
1038
|
+
this.avPacket?.[Symbol.dispose]?.();
|
|
1039
|
+
this.scaler?.[Symbol.dispose]?.();
|
|
1040
|
+
this.parser?.[Symbol.dispose]?.();
|
|
1041
|
+
this.swTransferFrame?.[Symbol.dispose]?.();
|
|
1042
|
+
this.codecCtx?.[Symbol.dispose]?.();
|
|
1043
|
+
this.hwDevice?.free();
|
|
1044
|
+
this.dstFrame = null;
|
|
1045
|
+
this.avFrame = null;
|
|
1046
|
+
this.avPacket = null;
|
|
1047
|
+
this.scaler = null;
|
|
1048
|
+
this.parser = null;
|
|
1049
|
+
this.codecCtx = null;
|
|
1050
|
+
this.swTransferFrame = null;
|
|
1051
|
+
this.hwDevice = null;
|
|
1052
|
+
}
|
|
1053
|
+
getStats() {
|
|
1054
|
+
const uptimeSec = Math.max((Date.now() - this.startTime) / 1e3, 1);
|
|
1055
|
+
return {
|
|
1056
|
+
inputFps: this.inputPackets / uptimeSec,
|
|
1057
|
+
outputFps: this.outputFrames / uptimeSec,
|
|
1058
|
+
avgDecodeTimeMs: this.outputFrames > 0 ? this.totalDecodeTimeMs / this.outputFrames : 0,
|
|
1059
|
+
droppedFrames: this.droppedFrames
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
get isPullMode() {
|
|
1063
|
+
return false;
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
//#endregion
|
|
1067
|
+
//#region src/decoder-nodeav/addon/index.ts
|
|
1068
|
+
var FRAME_BUFFER_CAPACITY = 32;
|
|
1069
|
+
var DecoderNodeAvAddon = class extends require_dist.BaseAddon {
|
|
1070
|
+
/**
|
|
1071
|
+
* Sessions are stored as the concrete `NodeAvDecoderSession` (the only type
|
|
1072
|
+
* this addon ever creates) so `getShmStats` can reach the per-session shm
|
|
1073
|
+
* ring sink — `IDecoderSession` does not expose it.
|
|
1074
|
+
*/
|
|
1075
|
+
sessions = /* @__PURE__ */ new Map();
|
|
1076
|
+
/** Pixel-frame buffers — populated only for `frameSink: 'callback'` sessions. */
|
|
1077
|
+
frameBuffers = /* @__PURE__ */ new Map();
|
|
1078
|
+
/** `FrameHandle` buffers — populated only for `frameSink: 'shm'` sessions. */
|
|
1079
|
+
handleBuffers = /* @__PURE__ */ new Map();
|
|
1080
|
+
unsubscribers = /* @__PURE__ */ new Map();
|
|
1081
|
+
sessionMeta = /* @__PURE__ */ new Map();
|
|
1082
|
+
/**
|
|
1083
|
+
* Per-shm-segment `FrameRingReader` cache backing `getFrame`. Opens each
|
|
1084
|
+
* named segment once and reuses the reader for every later handle on it;
|
|
1085
|
+
* built in `onInitialize` (it needs the scoped logger) and closed in
|
|
1086
|
+
* `onShutdown`.
|
|
1087
|
+
*/
|
|
1088
|
+
frameReaders = null;
|
|
1089
|
+
/** Running `getFrame` hit/miss counters surfaced via `getShmStats`. */
|
|
1090
|
+
getFrameHits = 0;
|
|
1091
|
+
getFrameMisses = 0;
|
|
1092
|
+
constructor() {
|
|
1093
|
+
super(require_dist.DEFAULT_DECODER_HWACCEL_CONFIG);
|
|
1094
|
+
}
|
|
1095
|
+
globalSettingsSchema() {
|
|
1096
|
+
return this.schema({ sections: [{
|
|
1097
|
+
id: "hwaccel",
|
|
1098
|
+
title: "Hardware acceleration",
|
|
1099
|
+
tab: "decoder",
|
|
1100
|
+
description: "Backend used by node-av decoder sessions. \"Auto\" defers to the probed best; concrete backends force it. Changes apply to NEW sessions — existing sessions keep the backend they were created with.",
|
|
1101
|
+
fields: [this.field({
|
|
1102
|
+
type: "select",
|
|
1103
|
+
key: "hwaccel",
|
|
1104
|
+
label: "Preferred backend",
|
|
1105
|
+
options: [...require_dist.HWACCEL_OPTIONS],
|
|
1106
|
+
default: "auto",
|
|
1107
|
+
immediate: true
|
|
1108
|
+
}), this.field({
|
|
1109
|
+
type: "text",
|
|
1110
|
+
key: "probedBestHwaccel",
|
|
1111
|
+
label: "Probed best",
|
|
1112
|
+
description: "Auto-detected best decoder backend on this host. Click the refresh icon to re-run the probe.",
|
|
1113
|
+
readonlyField: true,
|
|
1114
|
+
default: "",
|
|
1115
|
+
actions: [{
|
|
1116
|
+
action: "reprobe-hwaccel",
|
|
1117
|
+
icon: "refresh-cw",
|
|
1118
|
+
tooltip: "Re-probe hwaccel"
|
|
1119
|
+
}]
|
|
1120
|
+
})]
|
|
1121
|
+
}] });
|
|
1122
|
+
}
|
|
1123
|
+
async onInitialize() {
|
|
1124
|
+
this.ctx.logger.info("node-av decoder addon initialized");
|
|
1125
|
+
this.frameReaders = new _camstack_shm_ring.FrameRingReaderCache(this.ctx.logger);
|
|
1126
|
+
if (!this.config.probedBestHwaccel) this.reprobeHwaccel().catch((err) => {
|
|
1127
|
+
this.ctx.logger.warn("nodeav: auto-reprobe hwaccel failed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
1128
|
+
});
|
|
1129
|
+
return [{
|
|
1130
|
+
capability: require_dist.decoderCapability,
|
|
1131
|
+
provider: this
|
|
1132
|
+
}];
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Resolve the effective hwaccel backend for a new session. Reads
|
|
1136
|
+
* this addon's own `hwaccel` setting first. `'auto'` defers to the
|
|
1137
|
+
* session's local resolver (`ctx.kernel.hwaccel`) which probes the
|
|
1138
|
+
* host and picks. No more orchestrator round-trip — decoder addon
|
|
1139
|
+
* is self-sufficient for this setting as of phase 2d.
|
|
1140
|
+
*/
|
|
1141
|
+
resolveHwAccelPref() {
|
|
1142
|
+
return this.config.hwaccel;
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* Re-run the platform probe on this host and persist the detected
|
|
1146
|
+
* backend as `probedBestHwaccel`. The operator's `hwaccel` setting
|
|
1147
|
+
* is intentionally left alone — the probe only updates the hint.
|
|
1148
|
+
*/
|
|
1149
|
+
async reprobeHwaccel() {
|
|
1150
|
+
const resolver = this.ctx.kernel.hwaccel;
|
|
1151
|
+
if (!resolver) {
|
|
1152
|
+
this.ctx.logger.warn("reprobeHwaccel: no kernel hwaccel resolver — returning none");
|
|
1153
|
+
await this.ctx.settings?.writeAddonStore({ probedBestHwaccel: "none" });
|
|
1154
|
+
return { backend: "none" };
|
|
1155
|
+
}
|
|
1156
|
+
try {
|
|
1157
|
+
const res = await resolver.resolve();
|
|
1158
|
+
const backend = res.preferred[0] ?? "none";
|
|
1159
|
+
await this.ctx.settings?.writeAddonStore({ probedBestHwaccel: backend });
|
|
1160
|
+
this.ctx.logger.info("reprobeHwaccel: wrote probedBestHwaccel", { meta: {
|
|
1161
|
+
backend,
|
|
1162
|
+
rationale: res.rationale,
|
|
1163
|
+
preferred: res.preferred
|
|
1164
|
+
} });
|
|
1165
|
+
return { backend };
|
|
1166
|
+
} catch (err) {
|
|
1167
|
+
this.ctx.logger.warn("reprobeHwaccel failed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
1168
|
+
await this.ctx.settings?.writeAddonStore({ probedBestHwaccel: "none" });
|
|
1169
|
+
return { backend: "none" };
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
async supportsCodec(input) {
|
|
1173
|
+
return [
|
|
1174
|
+
"h264",
|
|
1175
|
+
"h265",
|
|
1176
|
+
"hevc"
|
|
1177
|
+
].includes(input.codec.toLowerCase());
|
|
1178
|
+
}
|
|
1179
|
+
async getInfo() {
|
|
1180
|
+
return {
|
|
1181
|
+
id: "decoder-nodeav",
|
|
1182
|
+
name: "Decoder (node-av)",
|
|
1183
|
+
isPullMode: false,
|
|
1184
|
+
priority: 10
|
|
1185
|
+
};
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* The cluster node id of this decoder — the Moleculer nodeID (`hub`,
|
|
1189
|
+
* `dev-agent-0`, …), not the addon id. Stamped into session-owned
|
|
1190
|
+
* `FrameHandle`s so a downstream consumer routes `getFrame` to the node
|
|
1191
|
+
* that holds the shm ring. A hierarchical broker nodeID (`hub/...`) is
|
|
1192
|
+
* collapsed to the cluster-visible parent; in-process boot is left as-is.
|
|
1193
|
+
* Mirrors the resolution `PipelineRunnerAddon.onInitialize` performs.
|
|
1194
|
+
*/
|
|
1195
|
+
resolveLocalNodeId() {
|
|
1196
|
+
const raw = this.ctx.kernel.localNodeId ?? this.ctx.id;
|
|
1197
|
+
return raw.includes("/") ? raw.split("/")[0] : raw;
|
|
1198
|
+
}
|
|
1199
|
+
async createSession(config) {
|
|
1200
|
+
const sessionId = (0, node_crypto.randomUUID)();
|
|
1201
|
+
const hwaccel = this.resolveHwAccelPref();
|
|
1202
|
+
const { frameSink } = config;
|
|
1203
|
+
const nodeId = this.resolveLocalNodeId();
|
|
1204
|
+
const session = new NodeAvDecoderSession(config, this.ctx.logger, {
|
|
1205
|
+
hwaccel,
|
|
1206
|
+
hwaccelResolver: this.ctx.kernel.hwaccel,
|
|
1207
|
+
frameSink,
|
|
1208
|
+
nodeId
|
|
1209
|
+
});
|
|
1210
|
+
const unsub = frameSink === "shm" ? this.wireShmSink(sessionId, session) : this.wireCallbackSink(sessionId, session);
|
|
1211
|
+
this.sessions.set(sessionId, session);
|
|
1212
|
+
this.unsubscribers.set(sessionId, unsub);
|
|
1213
|
+
this.sessionMeta.set(sessionId, {
|
|
1214
|
+
codec: config.codec,
|
|
1215
|
+
outputFormat: config.outputFormat,
|
|
1216
|
+
createdAtMs: Date.now()
|
|
1217
|
+
});
|
|
1218
|
+
this.ctx.logger.info("node-av: created session", { meta: {
|
|
1219
|
+
sessionId,
|
|
1220
|
+
codec: config.codec,
|
|
1221
|
+
hwaccelPref: hwaccel,
|
|
1222
|
+
frameSink,
|
|
1223
|
+
nodeId
|
|
1224
|
+
} });
|
|
1225
|
+
return {
|
|
1226
|
+
sessionId,
|
|
1227
|
+
nodeId
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
/**
|
|
1231
|
+
* Subscribe a `'callback'` session's pixel frames into a per-session
|
|
1232
|
+
* ring buffer drained by `pullFrames`.
|
|
1233
|
+
*/
|
|
1234
|
+
wireCallbackSink(sessionId, session) {
|
|
1235
|
+
const ringBuffer = new require_dist.RingBuffer(FRAME_BUFFER_CAPACITY);
|
|
1236
|
+
this.frameBuffers.set(sessionId, ringBuffer);
|
|
1237
|
+
return session.onFrame((frame) => {
|
|
1238
|
+
const { format } = frame;
|
|
1239
|
+
if (format !== "jpeg" && format !== "rgb" && format !== "bgr" && format !== "yuv420" && format !== "gray") return;
|
|
1240
|
+
const arrayBuf = new ArrayBuffer(frame.data.byteLength);
|
|
1241
|
+
new Uint8Array(arrayBuf).set(frame.data);
|
|
1242
|
+
const capFrame = {
|
|
1243
|
+
data: new Uint8Array(arrayBuf),
|
|
1244
|
+
width: frame.width,
|
|
1245
|
+
height: frame.height,
|
|
1246
|
+
format,
|
|
1247
|
+
timestamp: frame.timestamp
|
|
1248
|
+
};
|
|
1249
|
+
ringBuffer.push(capFrame);
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Subscribe a `'shm'` session's `FrameHandle`s into a per-session ring
|
|
1254
|
+
* buffer drained by `pullHandles`. No pixel bytes cross the cap boundary
|
|
1255
|
+
* — the broker opens the named segment and reads the pixels zero-copy.
|
|
1256
|
+
*/
|
|
1257
|
+
wireShmSink(sessionId, session) {
|
|
1258
|
+
const handleRing = new require_dist.RingBuffer(FRAME_BUFFER_CAPACITY);
|
|
1259
|
+
this.handleBuffers.set(sessionId, handleRing);
|
|
1260
|
+
return session.onFrameHandle((frame) => {
|
|
1261
|
+
handleRing.push(frame.handle);
|
|
1262
|
+
});
|
|
1263
|
+
}
|
|
1264
|
+
async destroySession(input) {
|
|
1265
|
+
const { sessionId } = input;
|
|
1266
|
+
const session = this.sessions.get(sessionId);
|
|
1267
|
+
if (!session) throw new Error(`decoder-nodeav: unknown sessionId ${sessionId}`);
|
|
1268
|
+
const unsub = this.unsubscribers.get(sessionId);
|
|
1269
|
+
if (unsub) unsub();
|
|
1270
|
+
await session.destroy();
|
|
1271
|
+
this.sessions.delete(sessionId);
|
|
1272
|
+
this.frameBuffers.delete(sessionId);
|
|
1273
|
+
this.handleBuffers.delete(sessionId);
|
|
1274
|
+
this.unsubscribers.delete(sessionId);
|
|
1275
|
+
this.sessionMeta.delete(sessionId);
|
|
1276
|
+
this.ctx.logger.info("node-av: destroyed session", { meta: { sessionId } });
|
|
1277
|
+
}
|
|
1278
|
+
async listActiveSessions() {
|
|
1279
|
+
const out = [];
|
|
1280
|
+
for (const [sessionId, meta] of this.sessionMeta) out.push({
|
|
1281
|
+
sessionId,
|
|
1282
|
+
codec: meta.codec,
|
|
1283
|
+
outputFormat: meta.outputFormat,
|
|
1284
|
+
createdAtMs: meta.createdAtMs
|
|
1285
|
+
});
|
|
1286
|
+
return out;
|
|
1287
|
+
}
|
|
1288
|
+
async pushPacket(input) {
|
|
1289
|
+
const session = this.sessions.get(input.sessionId);
|
|
1290
|
+
if (!session) throw new Error(`decoder-nodeav: unknown sessionId ${input.sessionId}`);
|
|
1291
|
+
const rawData = input.packet.data;
|
|
1292
|
+
const data = Buffer.isBuffer(rawData) ? rawData : rawData instanceof Uint8Array ? Buffer.from(rawData.buffer, rawData.byteOffset, rawData.byteLength) : Buffer.from(rawData);
|
|
1293
|
+
session.pushPacket({
|
|
1294
|
+
...input.packet,
|
|
1295
|
+
data
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
async openStream(input) {
|
|
1299
|
+
if (!this.sessions.get(input.sessionId)) throw new Error(`decoder-nodeav: unknown sessionId ${input.sessionId}`);
|
|
1300
|
+
input.url;
|
|
1301
|
+
}
|
|
1302
|
+
async pullFrames(input) {
|
|
1303
|
+
if (!this.sessions.has(input.sessionId)) throw new Error(`decoder-nodeav: unknown sessionId ${input.sessionId}`);
|
|
1304
|
+
const ringBuffer = this.frameBuffers.get(input.sessionId);
|
|
1305
|
+
if (!ringBuffer) return [];
|
|
1306
|
+
return ringBuffer.drain(input.maxCount);
|
|
1307
|
+
}
|
|
1308
|
+
async pullHandles(input) {
|
|
1309
|
+
if (!this.sessions.has(input.sessionId)) throw new Error(`decoder-nodeav: unknown sessionId ${input.sessionId}`);
|
|
1310
|
+
const handleRing = this.handleBuffers.get(input.sessionId);
|
|
1311
|
+
if (!handleRing) return [];
|
|
1312
|
+
return handleRing.drain(input.maxCount);
|
|
1313
|
+
}
|
|
1314
|
+
async updateConfig(input) {
|
|
1315
|
+
const session = this.sessions.get(input.sessionId);
|
|
1316
|
+
if (!session) throw new Error(`decoder-nodeav: unknown sessionId ${input.sessionId}`);
|
|
1317
|
+
session.updateConfig(input.config);
|
|
1318
|
+
}
|
|
1319
|
+
async getStats(input) {
|
|
1320
|
+
const session = this.sessions.get(input.sessionId);
|
|
1321
|
+
if (!session) throw new Error(`decoder-nodeav: unknown sessionId ${input.sessionId}`);
|
|
1322
|
+
return session.getStats();
|
|
1323
|
+
}
|
|
1324
|
+
/**
|
|
1325
|
+
* Read back the pixels a `FrameHandle` refers to from this node's shm ring
|
|
1326
|
+
* (Phase 5 / D9 downstream access). Returns `null` when the slot was
|
|
1327
|
+
* already recycled (latest-wins drop) or the segment could not be opened.
|
|
1328
|
+
* The reader cache opens each named segment once and reuses the reader.
|
|
1329
|
+
*
|
|
1330
|
+
* The reader hands back a `Buffer` view over its **reusable scratch
|
|
1331
|
+
* buffer**, only valid until the next `read` on the same reader; the
|
|
1332
|
+
* cap caller may keep the frame across reads, so the pixels are copied
|
|
1333
|
+
* into a fresh detached `ArrayBuffer` here. This matches the existing
|
|
1334
|
+
* callback-path copy in `wireCallbackSink`.
|
|
1335
|
+
*/
|
|
1336
|
+
async getFrame(input) {
|
|
1337
|
+
const frame = this.frameReaders?.read(input.handle) ?? null;
|
|
1338
|
+
if (!frame) {
|
|
1339
|
+
this.getFrameMisses += 1;
|
|
1340
|
+
return null;
|
|
1341
|
+
}
|
|
1342
|
+
this.getFrameHits += 1;
|
|
1343
|
+
const arrayBuf = new ArrayBuffer(frame.data.byteLength);
|
|
1344
|
+
new Uint8Array(arrayBuf).set(frame.data);
|
|
1345
|
+
return {
|
|
1346
|
+
data: new Uint8Array(arrayBuf),
|
|
1347
|
+
width: frame.width,
|
|
1348
|
+
height: frame.height,
|
|
1349
|
+
format: frame.format,
|
|
1350
|
+
timestamp: frame.timestamp
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
/**
|
|
1354
|
+
* shm ring usage stats for a `frameSink: 'shm'` session — slot geometry,
|
|
1355
|
+
* frames written, byte budget, plus this addon's running `getFrame`
|
|
1356
|
+
* hit/miss counters. Returns `null` for an unknown session or one whose
|
|
1357
|
+
* shm ring has not yet been armed by a first decoded frame.
|
|
1358
|
+
*/
|
|
1359
|
+
async getShmStats(input) {
|
|
1360
|
+
const stats = this.sessions.get(input.sessionId)?.frameRingSinkOrNull?.getShmStats() ?? null;
|
|
1361
|
+
if (!stats) return null;
|
|
1362
|
+
return {
|
|
1363
|
+
sessionId: input.sessionId,
|
|
1364
|
+
...stats,
|
|
1365
|
+
budgetMb: RING_BUDGET_MB,
|
|
1366
|
+
getFrameHits: this.getFrameHits,
|
|
1367
|
+
getFrameMisses: this.getFrameMisses
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
async onShutdown() {
|
|
1371
|
+
this.ctx.logger.info("node-av decoder addon shutdown — destroying all sessions");
|
|
1372
|
+
const destroyPromises = [];
|
|
1373
|
+
for (const [sessionId, session] of this.sessions) {
|
|
1374
|
+
const unsub = this.unsubscribers.get(sessionId);
|
|
1375
|
+
if (unsub) unsub();
|
|
1376
|
+
destroyPromises.push(session.destroy());
|
|
1377
|
+
}
|
|
1378
|
+
await Promise.all(destroyPromises);
|
|
1379
|
+
this.sessions.clear();
|
|
1380
|
+
this.frameBuffers.clear();
|
|
1381
|
+
this.handleBuffers.clear();
|
|
1382
|
+
this.unsubscribers.clear();
|
|
1383
|
+
this.sessionMeta.clear();
|
|
1384
|
+
this.frameReaders?.close();
|
|
1385
|
+
this.frameReaders = null;
|
|
1386
|
+
}
|
|
1387
|
+
};
|
|
1388
|
+
//#endregion
|
|
1458
1389
|
exports.DecoderFrameRingSink = DecoderFrameRingSink;
|
|
1459
1390
|
exports.DecoderNodeAvAddon = DecoderNodeAvAddon;
|
|
1460
1391
|
exports.NodeAvDecoderSession = NodeAvDecoderSession;
|
|
1461
1392
|
exports.default = DecoderNodeAvAddon;
|
|
1462
1393
|
exports.makeSegmentName = makeSegmentName;
|
|
1463
|
-
//# sourceMappingURL=index.js.map
|