@camstack/addon-pipeline 1.1.39 → 1.1.40

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 (38) hide show
  1. package/dist/audio-analyzer/index.js +168 -61
  2. package/dist/audio-analyzer/index.mjs +168 -61
  3. package/dist/detection-pipeline/index.js +2 -2
  4. package/dist/detection-pipeline/index.mjs +2 -2
  5. package/dist/{dist-DbGdZ8Nr.js → dist-BJTPkJFw.js} +247 -3
  6. package/dist/{dist-DvvYzO58.mjs → dist-v6cKLmU3.mjs} +247 -3
  7. package/dist/{frame-handle-plane-B7jMIZc2.js → frame-handle-plane-BP8YV4sF.js} +54 -5
  8. package/dist/{frame-handle-plane-BFzoIfkd.mjs → frame-handle-plane-DKAXTtfn.mjs} +54 -5
  9. package/dist/motion-wasm/index.js +1 -1
  10. package/dist/motion-wasm/index.mjs +1 -1
  11. package/dist/pipeline-runner/index.js +893 -12
  12. package/dist/pipeline-runner/index.mjs +892 -13
  13. package/dist/recorder/index.js +2 -2
  14. package/dist/recorder/index.mjs +2 -2
  15. package/dist/session-decode/decode-worker-child.js +511 -0
  16. package/dist/session-decode/decode-worker-child.mjs +510 -0
  17. package/dist/stream-broker/_stub.js +44 -44
  18. package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-DezFy4Fu.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-6I7-nufj.mjs} +3 -3
  19. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-DqJvWBKS.mjs +26 -0
  20. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-dGka8yZY.mjs +26 -0
  21. package/dist/stream-broker/{hostInit-BOf0hg9q.mjs → hostInit-CUkFGqiI.mjs} +3 -3
  22. package/dist/stream-broker/index.js +268 -38
  23. package/dist/stream-broker/index.mjs +268 -38
  24. package/dist/stream-broker/remoteEntry.js +1 -1
  25. package/dist/worker-protocol-BCfO8gUF.js +67 -0
  26. package/dist/worker-protocol-pk7qdYXt.mjs +56 -0
  27. package/embed-dist/assets/{MaskShapeCanvas-DI4BY7W2-Br2fqwe7.js → MaskShapeCanvas-DI4BY7W2-Cr_fCzY5.js} +1 -1
  28. package/embed-dist/assets/{MotionZonesSettings-NcxxQN8r-DYXRf30g.js → MotionZonesSettings-NcxxQN8r-COcvoHDH.js} +1 -1
  29. package/embed-dist/assets/{PrivacyMaskSettings-APgPLF7p-DLpqLgPo.js → PrivacyMaskSettings-APgPLF7p-Bv-bbyJl.js} +1 -1
  30. package/embed-dist/assets/index-Ctbq9AXh.css +2 -0
  31. package/embed-dist/assets/index-jMj5PyJk.js +81 -0
  32. package/embed-dist/index.html +2 -2
  33. package/package.json +6 -3
  34. package/python/inference_pool.py +3 -1
  35. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-DhY3MZ2C.mjs +0 -26
  36. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-BsrrdOBU.mjs +0 -26
  37. package/embed-dist/assets/index-C5UpuPr8.css +0 -2
  38. package/embed-dist/assets/index-CR367rYR.js +0 -81
@@ -1,10 +1,10 @@
1
1
  const require_model_download_service_C_IHWnXx = require("../model-download-service-C-IHWnXx-DxM2DSns.js");
2
- const require_dist = require("../dist-DbGdZ8Nr.js");
2
+ const require_dist = require("../dist-BJTPkJFw.js");
3
3
  const require_hub_hostname = require("../hub-hostname-DAJXlOgV.js");
4
+ let node_child_process = require("node:child_process");
4
5
  let node_fs = require("node:fs");
5
6
  let node_path = require("node:path");
6
7
  node_path = require_model_download_service_C_IHWnXx.__toESM(node_path);
7
- let node_child_process = require("node:child_process");
8
8
  //#region src/recorder/segment-path.ts
9
9
  function segmentRelPath(deviceId, profile, startMs, durMs, bytes) {
10
10
  if (profile.includes("/")) throw new Error(`segmentRelPath: profile must not contain '/': ${profile}`);
@@ -1,9 +1,9 @@
1
- import { A as recordingCapability, B as DeviceType, I as errMsg, J as selectAssignedProfileSlots, L as BaseAddon, M as storageEvictableCapability, T as migrateConfigToBands, U as hydrateSchema, V as EventCategory, at as string, it as record, s as EVENT_PAD_MS, u as RecordingConfigSchema } from "../dist-DvvYzO58.mjs";
1
+ import { A as recordingCapability, B as DeviceType, I as errMsg, J as selectAssignedProfileSlots, L as BaseAddon, M as storageEvictableCapability, T as migrateConfigToBands, U as hydrateSchema, V as EventCategory, at as string, it as record, s as EVENT_PAD_MS, u as RecordingConfigSchema } from "../dist-v6cKLmU3.mjs";
2
2
  import { t as resolveHubHostname } from "../hub-hostname-cCknRYKj.mjs";
3
3
  import { t as createFileDataPlaneHandler } from "../model-download-service-C-IHWnXx-BPy6aoAx.mjs";
4
+ import { spawn } from "node:child_process";
4
5
  import { promises } from "node:fs";
5
6
  import path from "node:path";
6
- import { spawn } from "node:child_process";
7
7
  //#region src/recorder/segment-path.ts
8
8
  function segmentRelPath(deviceId, profile, startMs, durMs, bytes) {
9
9
  if (profile.includes("/")) throw new Error(`segmentRelPath: profile must not contain '/': ${profile}`);
@@ -0,0 +1,511 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_worker_protocol = require("../worker-protocol-BCfO8gUF.js");
3
+ //#region src/session-decode/frame-slot.ts
4
+ function toReply(held) {
5
+ return {
6
+ frameId: held.frameId,
7
+ timestamp: held.timestamp,
8
+ width: held.width,
9
+ height: held.height
10
+ };
11
+ }
12
+ /**
13
+ * Latest-wins decoded-frame slot + reserved-for-`toBuffer` holder +
14
+ * pending-pull flag. One instance per decode-worker child session.
15
+ */
16
+ var FrameSlot = class {
17
+ nextFrameId = 1;
18
+ slot = null;
19
+ /**
20
+ * The frame handed to the MOST RECENT `pull`, retained so a following
21
+ * `toBuffer(frameId)` still resolves even though `publish` keeps advancing
22
+ * the latest-wins `slot`. Freed when the next `pull` supersedes it (or on
23
+ * `free()`).
24
+ */
25
+ reserved = null;
26
+ pendingPull = false;
27
+ /**
28
+ * Publish a newly decoded frame into the latest-wins slot, freeing
29
+ * whatever the slot held before (the drop-older half of "latest-wins").
30
+ * Returns the reply to send if a `pull` was pending (resolving it
31
+ * immediately and reserving the frame); otherwise returns `null` and the
32
+ * frame simply waits in the slot for the next `pull`.
33
+ */
34
+ publish(frame, timestamp, minIntervalMs) {
35
+ if (minIntervalMs > 0 && this.slot && timestamp - this.slot.timestamp < minIntervalMs) {
36
+ frame.free();
37
+ return null;
38
+ }
39
+ const previous = this.slot;
40
+ const frameId = this.nextFrameId++;
41
+ this.slot = {
42
+ frameId,
43
+ frame,
44
+ timestamp,
45
+ width: frame.width,
46
+ height: frame.height
47
+ };
48
+ previous?.frame.free();
49
+ if (this.pendingPull) {
50
+ this.pendingPull = false;
51
+ return this.reserveSlot();
52
+ }
53
+ return null;
54
+ }
55
+ /**
56
+ * Hand out the newest slot frame and RESERVE it (freeing the previously
57
+ * reserved frame), clearing the slot. Returns `null` if no frame has been
58
+ * decoded yet — the caller should then call `markPendingPull()`.
59
+ */
60
+ pull() {
61
+ if (!this.slot) return null;
62
+ return this.reserveSlot();
63
+ }
64
+ /** Record that a pull is waiting for the next `publish` to resolve it. */
65
+ markPendingPull() {
66
+ this.pendingPull = true;
67
+ }
68
+ /** The reserved frame for `frameId`, or `null` if unknown/stale. */
69
+ toBuffer(frameId) {
70
+ if (!this.reserved || this.reserved.frameId !== frameId) return null;
71
+ return this.reserved.frame;
72
+ }
73
+ /** Free both the slot and the reserved frame (teardown). */
74
+ free() {
75
+ this.slot?.frame.free();
76
+ this.slot = null;
77
+ this.reserved?.frame.free();
78
+ this.reserved = null;
79
+ }
80
+ reserveSlot() {
81
+ const held = this.slot;
82
+ if (!held) throw new Error("frame-slot: reserveSlot called with no slot held");
83
+ const previousReserved = this.reserved;
84
+ this.reserved = held;
85
+ this.slot = null;
86
+ previousReserved?.frame.free();
87
+ return toReply(held);
88
+ }
89
+ };
90
+ //#endregion
91
+ //#region src/session-decode/decode-worker-child.ts
92
+ /** Re-dial backoff on a transient dial error/EOF (mirrors the decoder's PULL_REDIAL_MS). */
93
+ var REDIAL_MS = 3e3;
94
+ var _nav = null;
95
+ var _consts = null;
96
+ async function getNodeAv() {
97
+ if (!_nav) _nav = await import("node-av");
98
+ return _nav;
99
+ }
100
+ async function getConstants() {
101
+ if (!_consts) _consts = await import("node-av/constants");
102
+ return _consts;
103
+ }
104
+ function errMessage(err) {
105
+ if (err instanceof Error) return err.message;
106
+ return String(err);
107
+ }
108
+ /**
109
+ * RTSP low-latency demuxer options — mirrors
110
+ * `addon-decoder-nodeav/src/pull-demuxer-options.ts` (duplicated here rather
111
+ * than cross-imported: this file is a standalone forked entry point, not an
112
+ * addon consumer of that package). `analyzeduration`/`probesize` stay small
113
+ * but non-zero — zeroing them starves the Reolink rfc4571 restream demuxer.
114
+ */
115
+ function buildDemuxerOptions() {
116
+ return {
117
+ rtsp_transport: "tcp",
118
+ fflags: "nobuffer",
119
+ analyzeduration: "1000000",
120
+ probesize: "1000000",
121
+ max_delay: "0",
122
+ reorder_queue_size: "0",
123
+ user_agent: "decode-worker"
124
+ };
125
+ }
126
+ /** Ordered hwaccel candidates per platform — first successful create() wins, else software. */
127
+ function candidateBackends() {
128
+ switch (process.platform) {
129
+ case "darwin": return ["videotoolbox"];
130
+ case "linux": return [
131
+ "vaapi",
132
+ "qsv",
133
+ "cuda"
134
+ ];
135
+ case "win32": return [
136
+ "d3d11va",
137
+ "dxva2",
138
+ "cuda"
139
+ ];
140
+ default: return [];
141
+ }
142
+ }
143
+ /** Map a canonical backend name to the node-av `AV_HWDEVICE_TYPE_*` constant. */
144
+ function backendToHwDeviceConst(backend, consts) {
145
+ switch (backend) {
146
+ case "videotoolbox": return consts.AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
147
+ case "cuda":
148
+ case "nvdec": return consts.AV_HWDEVICE_TYPE_CUDA;
149
+ case "vaapi": return consts.AV_HWDEVICE_TYPE_VAAPI;
150
+ case "qsv": return consts.AV_HWDEVICE_TYPE_QSV;
151
+ case "d3d11va": return consts.AV_HWDEVICE_TYPE_D3D11VA;
152
+ case "dxva2": return consts.AV_HWDEVICE_TYPE_DXVA2;
153
+ case "amf": return consts.AV_HWDEVICE_TYPE_AMF;
154
+ case "vdpau": return consts.AV_HWDEVICE_TYPE_VDPAU;
155
+ case "drm": return consts.AV_HWDEVICE_TYPE_DRM;
156
+ default: return null;
157
+ }
158
+ }
159
+ /** Clamp to [min,max] then floor to the nearest even number (YUV420P chroma is 2x2 subsampled). */
160
+ function clampEven(value, min, max) {
161
+ const bounded = Math.min(Math.max(value, min), max);
162
+ return bounded - bounded % 2;
163
+ }
164
+ /** Resolve `FrameImageOptions.crop` against the decoded frame's dimensions, defaulting to the full frame. */
165
+ function resolveCropRegion(frameWidth, frameHeight, crop) {
166
+ const left = clampEven(crop?.left ?? 0, 0, Math.max(0, frameWidth - 2));
167
+ const top = clampEven(crop?.top ?? 0, 0, Math.max(0, frameHeight - 2));
168
+ return {
169
+ left,
170
+ top,
171
+ width: clampEven(crop?.width ?? frameWidth - left, 2, frameWidth - left),
172
+ height: clampEven(crop?.height ?? frameHeight - top, 2, frameHeight - top)
173
+ };
174
+ }
175
+ /** Resolve `FrameImageOptions.resize`, defaulting to the (already cropped) source dimensions. */
176
+ function resolveResizeTarget(cropWidth, cropHeight, resize) {
177
+ if (!resize) return {
178
+ width: cropWidth,
179
+ height: cropHeight
180
+ };
181
+ return {
182
+ width: Math.max(1, Math.round(resize.width)),
183
+ height: Math.max(1, Math.round(resize.height))
184
+ };
185
+ }
186
+ /**
187
+ * The forked child's whole runtime: decode-loop lifecycle, the latest-wins
188
+ * frame slot, and the ROI scaler for `toBuffer`. One instance per process.
189
+ */
190
+ var DecodeWorkerChild = class {
191
+ nav = null;
192
+ consts = null;
193
+ hwContext = null;
194
+ demuxer = null;
195
+ decoder = null;
196
+ abortController = null;
197
+ started = false;
198
+ stopped = false;
199
+ sessionFormat = "rgb";
200
+ minIntervalMs = 0;
201
+ /**
202
+ * The latest-wins decoded-frame slot + reserved-for-`toBuffer` holder +
203
+ * pending-pull flag — extracted to `FrameSlot` (Epic C P2 Task 2 / I2) so
204
+ * the reserve-on-pull invariant (`e3c78a9b`) is unit-tested without
205
+ * node-av. See that file's doc comment for the full semantics.
206
+ */
207
+ frames = new FrameSlot();
208
+ /**
209
+ * Mirrors whether `this.frames` currently has a pull waiting, so
210
+ * `teardown` knows to resolve it with `{kind:'ended'}` — `FrameSlot`
211
+ * intentionally exposes no pending-pull query, only `markPendingPull()`,
212
+ * so the child tracks this in lockstep (set in `handlePull`, cleared
213
+ * whenever `consumeDecodedFrame`'s `publish` resolves it).
214
+ */
215
+ pendingPull = false;
216
+ ended = false;
217
+ redialTimer = null;
218
+ handleMessage(message) {
219
+ if (!require_worker_protocol.isWorkerRequest(message)) {
220
+ this.sendError("decode-worker-child: malformed request");
221
+ return;
222
+ }
223
+ switch (message.kind) {
224
+ case "start":
225
+ this.handleStart(message.source, message.opts);
226
+ return;
227
+ case "pull":
228
+ this.handlePull();
229
+ return;
230
+ case "toBuffer":
231
+ this.handleToBuffer(message.frameId, message.opts);
232
+ return;
233
+ case "stop":
234
+ this.handleStop();
235
+ return;
236
+ }
237
+ }
238
+ /** Channel teardown (explicit `stop` or the parent disconnecting) — dispose, then exit. */
239
+ teardown() {
240
+ this.stopped = true;
241
+ this.ended = true;
242
+ if (this.redialTimer) {
243
+ clearTimeout(this.redialTimer);
244
+ this.redialTimer = null;
245
+ }
246
+ this.abortController?.abort();
247
+ this.closeInput();
248
+ this.frames.free();
249
+ this.hwContext?.[Symbol.dispose]?.();
250
+ this.hwContext = null;
251
+ if (this.pendingPull) {
252
+ this.pendingPull = false;
253
+ this.send({ kind: "ended" });
254
+ }
255
+ }
256
+ handleStart(source, opts) {
257
+ if (this.started) {
258
+ process.stderr.write("decode-worker-child: duplicate start ignored\n");
259
+ return;
260
+ }
261
+ this.started = true;
262
+ this.sessionFormat = opts.format ?? "rgb";
263
+ this.minIntervalMs = opts.fps && opts.fps > 0 ? 1e3 / opts.fps : 0;
264
+ this.runDecodeLoop(source.restreamUrl).catch((err) => {
265
+ this.sendError(`decode-worker-child: decode loop crashed — ${errMessage(err)}`);
266
+ });
267
+ }
268
+ handlePull() {
269
+ if (this.ended) {
270
+ this.send({ kind: "ended" });
271
+ return;
272
+ }
273
+ const reply = this.frames.pull();
274
+ if (reply) {
275
+ this.send(this.toFrameReply(reply));
276
+ return;
277
+ }
278
+ this.pendingPull = true;
279
+ this.frames.markPendingPull();
280
+ }
281
+ handleToBuffer(frameId, opts) {
282
+ const frame = this.frames.toBuffer(frameId);
283
+ if (!frame) {
284
+ this.sendError(`decode-worker-child: toBuffer for unknown or stale frameId ${frameId}`, frameId);
285
+ return;
286
+ }
287
+ try {
288
+ const bytes = this.scaleRoiToBuffer(frame, opts);
289
+ this.send({
290
+ kind: "buffer",
291
+ frameId,
292
+ bytes
293
+ });
294
+ } catch (err) {
295
+ this.sendError(`decode-worker-child: toBuffer failed — ${errMessage(err)}`, frameId);
296
+ }
297
+ }
298
+ handleStop() {
299
+ this.teardown();
300
+ process.exit(0);
301
+ }
302
+ async runDecodeLoop(url) {
303
+ const nav = await getNodeAv();
304
+ const C = await getConstants();
305
+ if (this.stopped) return;
306
+ this.nav = nav;
307
+ this.consts = C;
308
+ nav.Log.setLevel(C.AV_LOG_FATAL);
309
+ await this.ensureHwContext(nav, C);
310
+ while (!this.stopped) {
311
+ try {
312
+ await this.dialAndDecode(nav, C, url);
313
+ } catch (err) {
314
+ if (this.stopped) break;
315
+ process.stderr.write(`decode-worker-child: dial ended — re-dial scheduled: ${errMessage(err)}\n`);
316
+ } finally {
317
+ this.closeInput();
318
+ }
319
+ if (this.stopped) break;
320
+ await this.sleep(REDIAL_MS);
321
+ }
322
+ }
323
+ /**
324
+ * One dial: open the demuxer, pick the video stream, build a HW/SW decoder
325
+ * that downloads + normalises frames to YUV420P system memory (so the ROI
326
+ * scaler always reads plain software planes regardless of hwaccel), and
327
+ * pump `demuxer.packets → decoder.frames → consumeDecodedFrame` until the
328
+ * stream ends, errors, or teardown aborts it.
329
+ *
330
+ * Copied from `nodeav-decoder-session.ts` `pullDialAndDecode` (lines
331
+ * ~916-996): same demuxer options, same `rescale.pixelFormat: YUV420P`
332
+ * download-always contract (keeping decoded frames on the GPU leaked the
333
+ * VAAPI surface pool on every re-dial — see that file's comment), same
334
+ * `exitOnError: false` bad-frame tolerance.
335
+ */
336
+ async dialAndDecode(nav, C, url) {
337
+ this.abortController = new AbortController();
338
+ const demuxer = await nav.Demuxer.open(url, {
339
+ options: buildDemuxerOptions(),
340
+ signal: this.abortController.signal
341
+ });
342
+ if (this.stopped) {
343
+ demuxer[Symbol.dispose]?.();
344
+ return;
345
+ }
346
+ this.demuxer = demuxer;
347
+ const videoStream = demuxer.video();
348
+ if (!videoStream) throw new Error("decode-worker-child: pull input has no video stream");
349
+ const decoder = await nav.Decoder.create(videoStream, {
350
+ ...this.hwContext ? { hardware: this.hwContext } : {},
351
+ rescale: { pixelFormat: C.AV_PIX_FMT_YUV420P },
352
+ exitOnError: false
353
+ });
354
+ if (this.stopped) {
355
+ decoder[Symbol.dispose]?.();
356
+ return;
357
+ }
358
+ this.decoder = decoder;
359
+ for await (const frame of decoder.frames(demuxer.packets(videoStream.index))) {
360
+ if (this.stopped) break;
361
+ if (!frame) continue;
362
+ this.consumeDecodedFrame(frame);
363
+ }
364
+ }
365
+ /**
366
+ * Publish a decoded frame into the latest-wins slot (mirrors Scrypted's
367
+ * `libav.py:32,50-53` single-slot queue) via `FrameSlot`, which handles
368
+ * drop-older, the `minIntervalMs` throttle, and resolving a pending `pull`.
369
+ */
370
+ consumeDecodedFrame(frame) {
371
+ if (frame.isHwFrame()) {
372
+ frame.free();
373
+ return;
374
+ }
375
+ const reply = this.frames.publish(frame, Date.now(), this.minIntervalMs);
376
+ if (reply) {
377
+ this.pendingPull = false;
378
+ this.send(this.toFrameReply(reply));
379
+ }
380
+ }
381
+ toFrameReply(reply) {
382
+ return {
383
+ kind: "frame",
384
+ frameId: reply.frameId,
385
+ timestamp: reply.timestamp,
386
+ width: reply.width,
387
+ height: reply.height,
388
+ format: this.sessionFormat
389
+ };
390
+ }
391
+ /**
392
+ * ROI `toBuffer`: crop (via plane-pointer offset, not a re-decode) + resize
393
+ * + format-convert a retained decoded frame with `SoftwareScaleContext`.
394
+ * Writes straight into a tightly-packed destination buffer via the
395
+ * low-level `scaleSync` (no `dstFrame` + row-strip step needed — the same
396
+ * zero-copy destination technique as `scaleIntoRingSlot` in
397
+ * `nodeav-decoder-session.ts`, adapted to also OFFSET the source planes so
398
+ * a single scaler call handles the crop.
399
+ */
400
+ scaleRoiToBuffer(frame, opts) {
401
+ const nav = this.nav;
402
+ const C = this.consts;
403
+ if (!nav || !C) throw new Error("node-av not initialized");
404
+ const region = resolveCropRegion(frame.width, frame.height, opts?.crop);
405
+ const target = resolveResizeTarget(region.width, region.height, opts?.resize);
406
+ const format = opts?.format ?? this.sessionFormat;
407
+ const dstFmt = format === "gray" ? C.AV_PIX_FMT_GRAY8 : C.AV_PIX_FMT_RGB24;
408
+ const channels = format === "gray" ? 1 : 3;
409
+ const planes = frame.data;
410
+ const strides = frame.linesize;
411
+ const yPlane = planes?.[0];
412
+ const uPlane = planes?.[1];
413
+ const vPlane = planes?.[2];
414
+ if (!yPlane || !uPlane || !vPlane) throw new Error("decode-worker-child: decoded frame missing planar YUV420P data");
415
+ const yStride = strides[0] ?? 0;
416
+ const cStride = strides[1] ?? 0;
417
+ const ySlice = yPlane.subarray(region.top * yStride + region.left);
418
+ const uSlice = uPlane.subarray(region.top / 2 * cStride + region.left / 2);
419
+ const vSlice = vPlane.subarray(region.top / 2 * cStride + region.left / 2);
420
+ const scaler = new nav.SoftwareScaleContext();
421
+ try {
422
+ scaler.getContext(region.width, region.height, C.AV_PIX_FMT_YUV420P, target.width, target.height, dstFmt, C.SWS_FAST_BILINEAR);
423
+ const initRet = scaler.initContext();
424
+ if (initRet < 0) throw new Error(`sws_init_context failed: ${initRet}`);
425
+ const dstStride = target.width * channels;
426
+ const dstBuffer = Buffer.allocUnsafe(dstStride * target.height);
427
+ const scaledHeight = scaler.scaleSync([
428
+ ySlice,
429
+ uSlice,
430
+ vSlice
431
+ ], [
432
+ yStride,
433
+ cStride,
434
+ cStride
435
+ ], 0, region.height, [dstBuffer], [dstStride]);
436
+ if (scaledHeight < 0) throw new Error(`sws_scale failed: ${scaledHeight}`);
437
+ if (scaledHeight !== target.height) throw new Error(`scaler produced ${scaledHeight} of ${target.height} rows`);
438
+ return dstBuffer;
439
+ } finally {
440
+ scaler[Symbol.dispose]?.();
441
+ }
442
+ }
443
+ /**
444
+ * Resolve the HW context ONCE (reused across every re-dial, freed in
445
+ * `teardown`). Tries the platform's ordered candidate list; the first
446
+ * `HardwareContext.create` that succeeds wins; every failure falls through
447
+ * to software decode (`hardware` omitted from `Decoder.create`).
448
+ */
449
+ async ensureHwContext(nav, C) {
450
+ for (const backend of candidateBackends()) {
451
+ const deviceType = backendToHwDeviceConst(backend, C);
452
+ if (deviceType === null) continue;
453
+ const hw = nav.HardwareContext.create(deviceType);
454
+ if (!hw) {
455
+ process.stderr.write(`decode-worker-child: hwaccel candidate '${backend}' failed — trying next\n`);
456
+ continue;
457
+ }
458
+ this.hwContext = hw;
459
+ return;
460
+ }
461
+ process.stderr.write("decode-worker-child: no hwaccel backend available — using software decode\n");
462
+ }
463
+ /** Interruptible re-dial backoff — `teardown` clears the pending timer. */
464
+ sleep(ms) {
465
+ return new Promise((resolve) => {
466
+ this.redialTimer = setTimeout(() => {
467
+ this.redialTimer = null;
468
+ resolve();
469
+ }, ms);
470
+ });
471
+ }
472
+ /** Dispose the CURRENT dial's demuxer + decoder (the HW context is reused across dials). */
473
+ closeInput() {
474
+ this.decoder?.[Symbol.dispose]?.();
475
+ this.decoder = null;
476
+ this.demuxer?.[Symbol.dispose]?.();
477
+ this.demuxer = null;
478
+ }
479
+ send(reply) {
480
+ process.send?.(reply);
481
+ }
482
+ sendError(message, frameId) {
483
+ process.stderr.write(`${message}\n`);
484
+ this.send(frameId === void 0 ? {
485
+ kind: "error",
486
+ message
487
+ } : {
488
+ kind: "error",
489
+ message,
490
+ frameId
491
+ });
492
+ }
493
+ };
494
+ var worker = new DecodeWorkerChild();
495
+ process.on("message", (message) => {
496
+ worker.handleMessage(message);
497
+ });
498
+ process.on("disconnect", () => {
499
+ worker.teardown();
500
+ process.exit(0);
501
+ });
502
+ process.on("uncaughtException", (err) => {
503
+ process.stderr.write(`decode-worker-child: uncaught exception — ${errMessage(err)}\n`);
504
+ worker.teardown();
505
+ process.exit(1);
506
+ });
507
+ process.on("unhandledRejection", (reason) => {
508
+ process.stderr.write(`decode-worker-child: unhandled rejection — ${errMessage(reason)}\n`);
509
+ });
510
+ //#endregion
511
+ exports.DecodeWorkerChild = DecodeWorkerChild;