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