@camstack/types 1.2.43 → 1.2.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/addon.js +8 -2
- package/dist/addon.mjs +8 -2
- package/dist/capabilities/index.d.ts +21 -3
- package/dist/capabilities/osd-manager.cap.d.ts +900 -0
- package/dist/capabilities/pipeline-analytics.cap.d.ts +1192 -74
- package/dist/capabilities/pipeline-orchestrator.cap.d.ts +10 -0
- package/dist/capabilities/pipeline-runner.cap.d.ts +6 -0
- package/dist/capabilities/schemas/streaming-shared.d.ts +2 -0
- package/dist/capabilities/server-management.cap.d.ts +3 -3
- package/dist/capabilities/stream-broker.cap.d.ts +32 -0
- package/dist/ffmpeg/fmp4-box-splitter.d.ts +113 -0
- package/dist/ffmpeg/fmp4-fragment-child.d.ts +85 -0
- package/dist/ffmpeg/fmp4-fragment-plane.d.ts +142 -0
- package/dist/ffmpeg/invocation.d.ts +4 -2
- package/dist/fmp4-box-splitter-B53u9-Nu.mjs +615 -0
- package/dist/fmp4-box-splitter-BkWH7O3L.js +686 -0
- package/dist/generated/addon-api.d.ts +162 -0
- package/dist/generated/cap-input-defaults.d.ts +1 -1
- package/dist/generated/capability-router-map.d.ts +5 -2
- package/dist/generated/device-proxy.d.ts +3 -1
- package/dist/generated/method-access-map.d.ts +1 -1
- package/dist/generated/system-proxy.d.ts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1114 -407
- package/dist/index.mjs +1075 -397
- package/dist/interfaces/camera-switches.d.ts +48 -0
- package/dist/interfaces/stream-broker.d.ts +19 -49
- package/dist/node.d.ts +4 -0
- package/dist/node.js +509 -3
- package/dist/node.mjs +507 -3
- package/dist/notification/schedule.d.ts +20 -0
- package/dist/{sleep-Bx9IIoT0.js → sleep-BOI-sVEA.js} +37 -1
- package/dist/{sleep-DtstvzWm.mjs → sleep-Bf5fBs7u.mjs} +37 -1
- package/dist/types/detection.d.ts +31 -0
- package/dist/types/pipeline-step.d.ts +22 -1
- package/package.json +1 -1
- package/dist/canonical-hash-7nfBbEqR.mjs +0 -35
- package/dist/canonical-hash-BcZHRHIx.js +0 -40
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
//#region src/ffmpeg/invocation.ts
|
|
3
|
+
var AUDIO_ENCODER_BY_CODEC = {
|
|
4
|
+
opus: "libopus",
|
|
5
|
+
aac: "aac",
|
|
6
|
+
pcmu: "pcm_mulaw",
|
|
7
|
+
pcma: "pcm_alaw"
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Camera-microphone audio, per codec. Lives HERE rather than in
|
|
11
|
+
* `encode-defaults.ts` only to avoid an import cycle (`encode-defaults` depends
|
|
12
|
+
* on these types); it is re-exported from there, which is where to read it.
|
|
13
|
+
*
|
|
14
|
+
* Every source in this repo is a mono camera mic. The former broker preset
|
|
15
|
+
* encoded Opus at `channels: 2`, spending bitrate duplicating one channel —
|
|
16
|
+
* that is the value this consolidation changed.
|
|
17
|
+
*/
|
|
18
|
+
var AUDIO_PRESETS = {
|
|
19
|
+
aac: {
|
|
20
|
+
kind: "encode",
|
|
21
|
+
codec: "aac",
|
|
22
|
+
bitrateKbps: 128,
|
|
23
|
+
sampleRateHz: 48e3,
|
|
24
|
+
channels: 1
|
|
25
|
+
},
|
|
26
|
+
opus: {
|
|
27
|
+
kind: "encode",
|
|
28
|
+
codec: "opus",
|
|
29
|
+
bitrateKbps: 64,
|
|
30
|
+
sampleRateHz: 48e3,
|
|
31
|
+
channels: 1
|
|
32
|
+
},
|
|
33
|
+
pcmu: {
|
|
34
|
+
kind: "encode",
|
|
35
|
+
codec: "pcmu",
|
|
36
|
+
sampleRateHz: 8e3,
|
|
37
|
+
channels: 1
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/** `-hide_banner -loglevel <level>` — every ffmpeg site opens with this. */
|
|
41
|
+
function logBannerArgs(level) {
|
|
42
|
+
return [
|
|
43
|
+
"-hide_banner",
|
|
44
|
+
"-loglevel",
|
|
45
|
+
level
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
/** `true` when the resolved value means "decode in software" (⇒ no `-hwaccel`). */
|
|
49
|
+
function isSoftwareDecode(decodeHwAccel) {
|
|
50
|
+
return !decodeHwAccel || decodeHwAccel === "none" || decodeHwAccel === "copy";
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Every INPUT option, in order, terminated by `-i <url>`. Nothing may be
|
|
54
|
+
* appended to this list by a caller — that is the whole point of the function.
|
|
55
|
+
*/
|
|
56
|
+
function buildInputArgs(input, decodeHwAccel) {
|
|
57
|
+
const args = [];
|
|
58
|
+
if (!isSoftwareDecode(decodeHwAccel)) args.push("-hwaccel", String(decodeHwAccel));
|
|
59
|
+
if (input.extraArgs?.length) args.push(...input.extraArgs);
|
|
60
|
+
if (input.analyzeDurationUs !== void 0) args.push("-analyzeduration", String(input.analyzeDurationUs));
|
|
61
|
+
if (input.probeSizeBytes !== void 0) args.push("-probesize", String(input.probeSizeBytes));
|
|
62
|
+
if (input.fflags?.length) for (const flag of input.fflags) args.push("-fflags", flag);
|
|
63
|
+
if (input.rtspTransport) args.push("-rtsp_transport", input.rtspTransport);
|
|
64
|
+
args.push("-i", input.url);
|
|
65
|
+
return args;
|
|
66
|
+
}
|
|
67
|
+
/** The `-vf` filter args, or `[]` when a consumer `-vf` already claims the slot. */
|
|
68
|
+
function buildVideoFilterArgs(scale, outputArgs) {
|
|
69
|
+
if (!scale) return [];
|
|
70
|
+
if (outputArgs.some((a) => a === "-vf")) return [];
|
|
71
|
+
if (scale.mode === "exact") return ["-vf", `scale=${scale.width}:${scale.height}`];
|
|
72
|
+
return ["-vf", `scale='min(${scale.width},iw)':'min(${scale.height},ih)':force_original_aspect_ratio=decrease:force_divisible_by=2`];
|
|
73
|
+
}
|
|
74
|
+
/** Rate-control args for an encode plan. */
|
|
75
|
+
function buildRateControlArgs(video) {
|
|
76
|
+
const kbps = video.bitrateKbps;
|
|
77
|
+
if (kbps === void 0) return [];
|
|
78
|
+
const rc = video.rateControl ?? {
|
|
79
|
+
kind: "cap",
|
|
80
|
+
vbvSeconds: 2
|
|
81
|
+
};
|
|
82
|
+
const bufsize = Math.max(1, Math.round(kbps * rc.vbvSeconds));
|
|
83
|
+
return [
|
|
84
|
+
...rc.kind === "cbr" ? ["-b:v", `${kbps}k`] : [],
|
|
85
|
+
"-maxrate",
|
|
86
|
+
`${kbps}k`,
|
|
87
|
+
"-bufsize",
|
|
88
|
+
`${bufsize}k`
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
/** The whole video block (`-vf` … `-c:v` … knobs), after `-i`. */
|
|
92
|
+
function buildVideoArgs(video, outputArgs) {
|
|
93
|
+
if (video.kind === "copy") return [
|
|
94
|
+
"-c:v",
|
|
95
|
+
"copy",
|
|
96
|
+
...video.bitstreamFilter ? ["-bsf:v", video.bitstreamFilter] : []
|
|
97
|
+
];
|
|
98
|
+
const args = [
|
|
99
|
+
...buildVideoFilterArgs(video.scale, outputArgs),
|
|
100
|
+
"-c:v",
|
|
101
|
+
video.encoder
|
|
102
|
+
];
|
|
103
|
+
if (video.preset !== void 0) args.push("-preset", video.preset);
|
|
104
|
+
if (video.tune !== void 0) args.push("-tune", video.tune);
|
|
105
|
+
if (video.profile !== void 0) args.push("-profile:v", video.profile);
|
|
106
|
+
if (video.level !== void 0) args.push("-level", video.level);
|
|
107
|
+
if (video.pixelFormat !== void 0) args.push("-pix_fmt", video.pixelFormat);
|
|
108
|
+
if (video.fps !== void 0) args.push("-r", String(video.fps));
|
|
109
|
+
if (video.gopFrames !== void 0) args.push("-g", String(video.gopFrames));
|
|
110
|
+
if (video.forceKeyFramesSeconds !== void 0) args.push("-force_key_frames", `expr:gte(t,n_forced*${video.forceKeyFramesSeconds})`);
|
|
111
|
+
if (video.bf !== void 0) args.push("-bf", String(video.bf));
|
|
112
|
+
args.push(...buildRateControlArgs(video));
|
|
113
|
+
if (video.bitstreamFilter !== void 0) args.push("-bsf:v", video.bitstreamFilter);
|
|
114
|
+
return args;
|
|
115
|
+
}
|
|
116
|
+
/** The whole audio block, after `-i`. */
|
|
117
|
+
function buildAudioArgs(audio) {
|
|
118
|
+
if (audio.kind === "none") return ["-an"];
|
|
119
|
+
if (audio.kind === "copy") return ["-c:a", "copy"];
|
|
120
|
+
const args = [];
|
|
121
|
+
if (audio.filter !== void 0) args.push("-af", audio.filter);
|
|
122
|
+
args.push("-c:a", AUDIO_ENCODER_BY_CODEC[audio.codec]);
|
|
123
|
+
if (audio.application !== void 0) args.push("-application", audio.application);
|
|
124
|
+
if (audio.frameDurationMs !== void 0) args.push("-frame_duration", String(audio.frameDurationMs));
|
|
125
|
+
if (audio.globalHeader === true) args.push("-flags", "+global_header");
|
|
126
|
+
if (audio.sampleRateHz !== void 0) args.push("-ar", String(audio.sampleRateHz));
|
|
127
|
+
if (audio.bitrateKbps !== void 0) args.push("-b:a", `${audio.bitrateKbps}k`);
|
|
128
|
+
if (audio.vbvBufferKbits !== void 0) args.push("-bufsize", `${audio.vbvBufferKbits}k`);
|
|
129
|
+
if (audio.channels !== void 0) args.push("-ac", String(audio.channels));
|
|
130
|
+
return args;
|
|
131
|
+
}
|
|
132
|
+
/** RTP output-leg args (`-payload_type`, `-ssrc`, `-sdp_file`, `-f rtp <url>`). */
|
|
133
|
+
function buildRtpOutputArgs(out) {
|
|
134
|
+
const args = [];
|
|
135
|
+
if (out.payloadType !== void 0) args.push("-payload_type", String(out.payloadType));
|
|
136
|
+
if (out.ssrc !== void 0) args.push("-ssrc", String(out.ssrc));
|
|
137
|
+
if (out.sdpFile !== void 0) args.push("-sdp_file", out.sdpFile);
|
|
138
|
+
args.push("-f", "rtp", out.url);
|
|
139
|
+
return args;
|
|
140
|
+
}
|
|
141
|
+
/** `true` when the sink is a raw elementary bytestream that cannot mux audio. */
|
|
142
|
+
function isElementaryVideoSink(sink) {
|
|
143
|
+
return sink.kind === "stdout" && (sink.container === "h264" || sink.container === "hevc");
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The fragmented-MP4 muxer flags, in the order the recorder has proven them
|
|
147
|
+
* (`recorder/addon/ffmpeg-args.ts` passes the same `movflags` string through
|
|
148
|
+
* `-segment_format_options`, across every vendor in the fleet):
|
|
149
|
+
*
|
|
150
|
+
* - `frag_keyframe` — cut a fragment at each key frame, so every fragment
|
|
151
|
+
* opens on a sync sample. HKSV's whole requirement.
|
|
152
|
+
* - `empty_moov` — write `ftyp`+`moov` up front with no samples in it, which
|
|
153
|
+
* is what makes the head a standalone INITIALISATION segment.
|
|
154
|
+
* - `default_base_moof` — fragment offsets are self-relative, so a fragment is
|
|
155
|
+
* demuxable without the bytes that preceded it. D31's byte-range read path
|
|
156
|
+
* depends on exactly this property of the recorder's segments.
|
|
157
|
+
*/
|
|
158
|
+
var FMP4_MOVFLAGS = "+frag_keyframe+empty_moov+default_base_moof";
|
|
159
|
+
/**
|
|
160
|
+
* The terminal sink args for every non-`rtp-outputs` sink. Exhaustive over the
|
|
161
|
+
* union so a new member cannot fall through to `['-f', container, 'pipe:1']`,
|
|
162
|
+
* which is what a plain `container` read would have done for `mp4` — a valid
|
|
163
|
+
* argv that writes a NON-fragmented, unseekable-to-a-pipe MP4 and produces one
|
|
164
|
+
* unusable byte stream.
|
|
165
|
+
*/
|
|
166
|
+
function buildStdoutOrRtspSinkArgs(sink) {
|
|
167
|
+
if (sink.kind === "rtsp-listen") return [
|
|
168
|
+
"-f",
|
|
169
|
+
"rtsp",
|
|
170
|
+
"-rtsp_transport",
|
|
171
|
+
"tcp",
|
|
172
|
+
"-rtsp_flags",
|
|
173
|
+
"listen",
|
|
174
|
+
sink.url
|
|
175
|
+
];
|
|
176
|
+
if (sink.kind === "rtp-outputs") return [];
|
|
177
|
+
return sink.container === "mp4" ? buildFmp4SinkArgs(sink) : [
|
|
178
|
+
"-f",
|
|
179
|
+
sink.container,
|
|
180
|
+
"pipe:1"
|
|
181
|
+
];
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* How far BELOW the negotiated fragment length `-min_frag_duration` is set.
|
|
185
|
+
*
|
|
186
|
+
* `-min_frag_duration` refuses to cut before that much media has accumulated,
|
|
187
|
+
* and then waits for the next key frame. Set to exactly `fragmentMs`, the
|
|
188
|
+
* commonest camera configuration in existence — a key-frame grid EQUAL to the
|
|
189
|
+
* requested fragment length — lands the deadline on the same instant as the key
|
|
190
|
+
* frame, loses the race, and skips to the following one: **every fragment comes
|
|
191
|
+
* out at twice the requested length.**
|
|
192
|
+
*
|
|
193
|
+
* Measured on the live fleet 2026-08-07, camera 615, `-c:v copy` (D84):
|
|
194
|
+
*
|
|
195
|
+
* | slot | GOP | `-min_frag_duration` | median gap |
|
|
196
|
+
* | --- | --- | --- | --- |
|
|
197
|
+
* | 1280×720 | 40 f @ 10 fps = 4.0 s | 4000 ms | **7944 ms** |
|
|
198
|
+
* | 1280×720 | 40 f @ 10 fps = 4.0 s | 3600 ms | 3973 ms |
|
|
199
|
+
* | 3840×2160 | 100 f @ 25 fps = 4.0 s | 4000 ms | 8042 ms |
|
|
200
|
+
* | 3840×2160 | 100 f @ 25 fps = 4.0 s | 3600 ms | 3998 ms |
|
|
201
|
+
*
|
|
202
|
+
* A doubled fragment is not a cosmetic overshoot: HKSV requires every fragment
|
|
203
|
+
* to be no longer than the length the controller SELECTED, so the shipped-but-
|
|
204
|
+
* inert phase-1 sink would have violated the contract on its first real clip.
|
|
205
|
+
*
|
|
206
|
+
* 10 % is chosen against the two failures either side of it. Too small and
|
|
207
|
+
* ordinary jitter (measured spread 3953-4096 ms) re-loses the race; too large
|
|
208
|
+
* and a source with a key frame slightly EARLY than the grid gets cut there,
|
|
209
|
+
* yielding a short fragment for no reason.
|
|
210
|
+
*/
|
|
211
|
+
var FMP4_MIN_FRAG_MARGIN = .9;
|
|
212
|
+
/** `-movflags … -min_frag_duration <us> -f mp4 pipe:1`. */
|
|
213
|
+
function buildFmp4SinkArgs(sink) {
|
|
214
|
+
return [
|
|
215
|
+
"-movflags",
|
|
216
|
+
FMP4_MOVFLAGS,
|
|
217
|
+
"-min_frag_duration",
|
|
218
|
+
String(Math.max(0, Math.round(sink.fragmentMs * FMP4_MIN_FRAG_MARGIN * 1e3))),
|
|
219
|
+
"-f",
|
|
220
|
+
"mp4",
|
|
221
|
+
"pipe:1"
|
|
222
|
+
];
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* A second output mapping source audio to RTP-over-UDP. `0:a:0?` makes the
|
|
226
|
+
* audio optional so a source with no audio skips it instead of failing the
|
|
227
|
+
* whole invocation.
|
|
228
|
+
*/
|
|
229
|
+
function buildAudioSidecarArgs(sidecar) {
|
|
230
|
+
return [
|
|
231
|
+
"-map",
|
|
232
|
+
"0:a:0?",
|
|
233
|
+
...buildAudioArgs(sidecar.codec === "pcma" ? {
|
|
234
|
+
kind: "encode",
|
|
235
|
+
codec: "pcma",
|
|
236
|
+
sampleRateHz: 8e3,
|
|
237
|
+
channels: 1
|
|
238
|
+
} : AUDIO_PRESETS[sidecar.codec]),
|
|
239
|
+
...buildRtpOutputArgs({
|
|
240
|
+
url: sidecar.rtpUrl,
|
|
241
|
+
sdpFile: sidecar.sdpFile
|
|
242
|
+
})
|
|
243
|
+
];
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Assemble the full ffmpeg argument list. Layout:
|
|
247
|
+
*
|
|
248
|
+
* -hide_banner -loglevel <level>
|
|
249
|
+
* [-hwaccel <backend|auto>] ─┐ INPUT options — strictly before -i.
|
|
250
|
+
* [<input.extraArgs>] │
|
|
251
|
+
* [-fflags <flag>…] │
|
|
252
|
+
* [-rtsp_transport tcp] │
|
|
253
|
+
* -i <url> ─┘
|
|
254
|
+
* <video block> <threads> <audio block> ─┐ OUTPUT options.
|
|
255
|
+
* <consumer outputArgs verbatim> │
|
|
256
|
+
* <sink> ─┘ terminal
|
|
257
|
+
*/
|
|
258
|
+
function buildFfmpegArgs(inv) {
|
|
259
|
+
const head = [...logBannerArgs(inv.logLevel), ...buildInputArgs(inv.input, inv.decodeHwAccel)];
|
|
260
|
+
const threadArgs = inv.threadCount > 0 ? ["-threads", String(inv.threadCount)] : [];
|
|
261
|
+
if (inv.sink.kind === "rtp-outputs") {
|
|
262
|
+
const videoLeg = inv.sink.video ? [
|
|
263
|
+
"-an",
|
|
264
|
+
"-map",
|
|
265
|
+
"0:v:0",
|
|
266
|
+
...buildVideoArgs(inv.video, inv.outputArgs),
|
|
267
|
+
...threadArgs,
|
|
268
|
+
...inv.outputArgs,
|
|
269
|
+
...buildRtpOutputArgs(inv.sink.video)
|
|
270
|
+
] : [];
|
|
271
|
+
const audioLeg = inv.sink.audio ? [
|
|
272
|
+
"-vn",
|
|
273
|
+
"-map",
|
|
274
|
+
"0:a:0?",
|
|
275
|
+
...buildAudioArgs(inv.audio),
|
|
276
|
+
...buildRtpOutputArgs(inv.sink.audio)
|
|
277
|
+
] : [];
|
|
278
|
+
return [
|
|
279
|
+
...head,
|
|
280
|
+
...videoLeg,
|
|
281
|
+
...audioLeg
|
|
282
|
+
];
|
|
283
|
+
}
|
|
284
|
+
const audioArgs = isElementaryVideoSink(inv.sink) ? ["-an"] : buildAudioArgs(inv.audio);
|
|
285
|
+
const sinkArgs = buildStdoutOrRtspSinkArgs(inv.sink);
|
|
286
|
+
return [
|
|
287
|
+
...head,
|
|
288
|
+
...buildVideoArgs(inv.video, inv.outputArgs),
|
|
289
|
+
...threadArgs,
|
|
290
|
+
...audioArgs,
|
|
291
|
+
...inv.outputArgs,
|
|
292
|
+
...sinkArgs,
|
|
293
|
+
...inv.audioSidecar ? buildAudioSidecarArgs(inv.audioSidecar) : []
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Hardware ENCODER ids per decode-hwaccel backend — a static, deterministic
|
|
298
|
+
* map (the same shape the reference NVR uses: platform → encoder, no probe).
|
|
299
|
+
*/
|
|
300
|
+
var ENCODER_IDS_BY_BACKEND = {
|
|
301
|
+
videotoolbox: {
|
|
302
|
+
h264: "h264_videotoolbox",
|
|
303
|
+
h265: "hevc_videotoolbox"
|
|
304
|
+
},
|
|
305
|
+
vaapi: {
|
|
306
|
+
h264: "h264_vaapi",
|
|
307
|
+
h265: "hevc_vaapi"
|
|
308
|
+
},
|
|
309
|
+
qsv: {
|
|
310
|
+
h264: "h264_qsv",
|
|
311
|
+
h265: "hevc_qsv"
|
|
312
|
+
},
|
|
313
|
+
cuda: {
|
|
314
|
+
h264: "h264_nvenc",
|
|
315
|
+
h265: "hevc_nvenc"
|
|
316
|
+
},
|
|
317
|
+
nvdec: {
|
|
318
|
+
h264: "h264_nvenc",
|
|
319
|
+
h265: "hevc_nvenc"
|
|
320
|
+
},
|
|
321
|
+
amf: {
|
|
322
|
+
h264: "h264_amf",
|
|
323
|
+
h265: "hevc_amf"
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* The hardware encoder for a target codec on `backend`, or the software one.
|
|
328
|
+
* `'auto'` is NOT a backend identity (it is an instruction to ffmpeg), so it
|
|
329
|
+
* maps to software encoding.
|
|
330
|
+
*/
|
|
331
|
+
function pickVideoEncoder(target, backend, useHardware) {
|
|
332
|
+
const software = target === "h264" ? "libx264" : "libx265";
|
|
333
|
+
if (!useHardware || backend === null || isSoftwareDecode(backend) || backend === "auto") return software;
|
|
334
|
+
const ids = ENCODER_IDS_BY_BACKEND[backend.toLowerCase()];
|
|
335
|
+
if (!ids) return software;
|
|
336
|
+
return target === "h264" ? ids.h264 : ids.h265;
|
|
337
|
+
}
|
|
338
|
+
/** Map an `EncodeProfile.audio` to an audio plan. */
|
|
339
|
+
function audioPlanFromEncodeProfile(audio) {
|
|
340
|
+
if (audio === "passthrough") return { kind: "none" };
|
|
341
|
+
if (audio.codec === "copy") return { kind: "copy" };
|
|
342
|
+
return {
|
|
343
|
+
kind: "encode",
|
|
344
|
+
codec: audio.codec,
|
|
345
|
+
...audio.bitrateKbps !== void 0 ? { bitrateKbps: audio.bitrateKbps } : {},
|
|
346
|
+
...audio.sampleRateHz !== void 0 ? { sampleRateHz: audio.sampleRateHz } : {},
|
|
347
|
+
...audio.channels !== void 0 ? { channels: audio.channels } : {}
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Adapt an `EncodeProfile` (the operator/consumer-facing shape) into an
|
|
352
|
+
* {@link FfmpegInvocation}. This is the ONLY bridge between the two models —
|
|
353
|
+
* a second one is how the repo grew two argv builders that disagreed about
|
|
354
|
+
* hardware.
|
|
355
|
+
*
|
|
356
|
+
* Smart video copy: when the source already speaks the requested codec the
|
|
357
|
+
* encode block is elided entirely and ffmpeg runs as a re-muxer on the video
|
|
358
|
+
* plane. Width / height / fps / bitrate in the profile are a downstream BUDGET,
|
|
359
|
+
* not a forced rescale.
|
|
360
|
+
*/
|
|
361
|
+
function invocationFromEncodeProfile(input) {
|
|
362
|
+
const v = input.profile.video;
|
|
363
|
+
const shouldCopy = v.codec === "copy" || input.forceReencode !== true && v.codec === input.sourceCodec;
|
|
364
|
+
const scale = v.width !== void 0 && v.height !== void 0 ? {
|
|
365
|
+
mode: "fit",
|
|
366
|
+
width: v.width,
|
|
367
|
+
height: v.height
|
|
368
|
+
} : null;
|
|
369
|
+
const target = v.codec === "h265" ? "h265" : "h264";
|
|
370
|
+
const video = shouldCopy ? {
|
|
371
|
+
kind: "copy",
|
|
372
|
+
...input.bitstreamFilter !== void 0 ? { bitstreamFilter: input.bitstreamFilter } : {}
|
|
373
|
+
} : {
|
|
374
|
+
kind: "encode",
|
|
375
|
+
encoder: pickVideoEncoder(target, input.decodeHwAccel, input.hardwareEncoders === true),
|
|
376
|
+
scale,
|
|
377
|
+
...v.preset !== void 0 ? { preset: v.preset } : {},
|
|
378
|
+
...v.tune !== void 0 ? { tune: v.tune } : {},
|
|
379
|
+
...v.profile !== void 0 ? { profile: v.profile } : {},
|
|
380
|
+
...v.level !== void 0 ? { level: v.level } : {},
|
|
381
|
+
...input.pixelFormat !== void 0 ? { pixelFormat: input.pixelFormat } : {},
|
|
382
|
+
...v.fps !== void 0 ? { fps: v.fps } : {},
|
|
383
|
+
...v.gopFrames !== void 0 ? { gopFrames: v.gopFrames } : {},
|
|
384
|
+
...input.forceKeyFramesSeconds !== void 0 ? { forceKeyFramesSeconds: input.forceKeyFramesSeconds } : {},
|
|
385
|
+
...v.bf !== void 0 ? { bf: v.bf } : {},
|
|
386
|
+
...v.bitrateKbps !== void 0 ? { bitrateKbps: v.bitrateKbps } : {},
|
|
387
|
+
...input.rateControl !== void 0 ? { rateControl: input.rateControl } : {},
|
|
388
|
+
...input.bitstreamFilter !== void 0 ? { bitstreamFilter: input.bitstreamFilter } : {}
|
|
389
|
+
};
|
|
390
|
+
return {
|
|
391
|
+
logLevel: input.logLevel ?? "error",
|
|
392
|
+
decodeHwAccel: input.decodeHwAccel,
|
|
393
|
+
input: {
|
|
394
|
+
url: input.sourceUrl,
|
|
395
|
+
rtspTransport: "tcp",
|
|
396
|
+
fflags: ["+discardcorrupt"],
|
|
397
|
+
...input.profile.inputArgs?.length ? { extraArgs: input.profile.inputArgs } : {}
|
|
398
|
+
},
|
|
399
|
+
video,
|
|
400
|
+
audio: audioPlanFromEncodeProfile(input.profile.audio),
|
|
401
|
+
threadCount: input.threadCount ?? 0,
|
|
402
|
+
outputArgs: input.profile.outputArgs ?? [],
|
|
403
|
+
sink: input.sink,
|
|
404
|
+
...input.audioSidecar !== void 0 ? { audioSidecar: input.audioSidecar } : {}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
//#endregion
|
|
408
|
+
//#region src/utils/canonical-hash.ts
|
|
409
|
+
/**
|
|
410
|
+
* Deterministic SHA-256 hash of an arbitrary serialisable value. The
|
|
411
|
+
* canonical form sorts object keys alphabetically at every depth so two
|
|
412
|
+
* structurally-equal inputs with different key insertion orders produce
|
|
413
|
+
* the same hash. Returns a 64-char lowercase hex digest.
|
|
414
|
+
*
|
|
415
|
+
* Used by export adapters (Alexa, HAP) to short-circuit re-discovery /
|
|
416
|
+
* accessory-rebuild work when the upstream shape is byte-identical to
|
|
417
|
+
* the last applied state — preventing user-visible "re-discovery"
|
|
418
|
+
* notifications on every addon-runner respawn. Each respawn re-fires
|
|
419
|
+
* `DeviceBindingsChanged` for every cap registration, which without
|
|
420
|
+
* this guard would propagate redundant pushes.
|
|
421
|
+
*
|
|
422
|
+
* Note: this is a SYMPTOMATIC fix layered on top of the binding-change
|
|
423
|
+
* subscription. The proper fix is a single "device ready" lifecycle
|
|
424
|
+
* barrier so exports react only when the full cap set has landed —
|
|
425
|
+
* tracked separately for post-HA-integration work.
|
|
426
|
+
*/
|
|
427
|
+
function canonicalHash(value) {
|
|
428
|
+
const canonical = JSON.stringify(value, replaceWithSortedKeys);
|
|
429
|
+
return createHash("sha256").update(canonical ?? "").digest("hex");
|
|
430
|
+
}
|
|
431
|
+
function replaceWithSortedKeys(_key, value) {
|
|
432
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
433
|
+
const obj = value;
|
|
434
|
+
const out = {};
|
|
435
|
+
for (const k of Object.keys(obj).toSorted()) out[k] = obj[k];
|
|
436
|
+
return out;
|
|
437
|
+
}
|
|
438
|
+
return value;
|
|
439
|
+
}
|
|
440
|
+
//#endregion
|
|
441
|
+
//#region src/ffmpeg/fmp4-box-splitter.ts
|
|
442
|
+
var DEFAULT_MAX_UNIT_BYTES = 16 * 1024 * 1024;
|
|
443
|
+
/** Header size for a normal box, and for one carrying a 64-bit `largesize`. */
|
|
444
|
+
var BOX_HEADER_BYTES = 8;
|
|
445
|
+
var LARGE_BOX_HEADER_BYTES = 16;
|
|
446
|
+
var Fmp4BoxSplitter = class {
|
|
447
|
+
maxUnitBytes;
|
|
448
|
+
/** Bytes of the CURRENT unit plus any partial box after it. */
|
|
449
|
+
buffer = new Uint8Array(0);
|
|
450
|
+
/** Where the current unit starts inside {@link buffer}. */
|
|
451
|
+
unitStart = 0;
|
|
452
|
+
/** Where the box scanner has reached inside {@link buffer}. */
|
|
453
|
+
cursor = 0;
|
|
454
|
+
state = "init";
|
|
455
|
+
nextSequence = 0;
|
|
456
|
+
faultReason = null;
|
|
457
|
+
interstitial = /* @__PURE__ */ new Set();
|
|
458
|
+
constructor(options = {}) {
|
|
459
|
+
this.maxUnitBytes = options.maxUnitBytes ?? DEFAULT_MAX_UNIT_BYTES;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Non-null once the stream cannot be split. The splitter emits nothing
|
|
463
|
+
* further, so a caller polls this to kill the child rather than watching a
|
|
464
|
+
* silent stall — a fragmenter that quietly stops producing looks exactly like
|
|
465
|
+
* a camera with no motion.
|
|
466
|
+
*/
|
|
467
|
+
get fault() {
|
|
468
|
+
return this.faultReason;
|
|
469
|
+
}
|
|
470
|
+
/** Bytes currently held. The memory bound, observable rather than asserted. */
|
|
471
|
+
get pendingBytes() {
|
|
472
|
+
return this.buffer.length - this.unitStart;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Top-level box types seen BETWEEN fragments and discarded — `mfra`, `free`,
|
|
476
|
+
* a stray `sidx`. Reported rather than dropped in silence: they are legal and
|
|
477
|
+
* useless to a fragment consumer, but a type nobody expected showing up here
|
|
478
|
+
* is the first symptom of a muxer that is not writing what we think it is.
|
|
479
|
+
*/
|
|
480
|
+
get discardedInterstitialTypes() {
|
|
481
|
+
return [...this.interstitial];
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Feed bytes; get back whatever units completed. Returns `[]` once faulted.
|
|
485
|
+
*/
|
|
486
|
+
push(chunk) {
|
|
487
|
+
if (this.faultReason !== null || chunk.length === 0) return [];
|
|
488
|
+
this.append(chunk);
|
|
489
|
+
if (this.pendingBytes > this.maxUnitBytes) return this.fail(`a single fMP4 unit exceeded ${this.maxUnitBytes} bytes — this stream is not fragmented`);
|
|
490
|
+
return this.drainBoxes();
|
|
491
|
+
}
|
|
492
|
+
append(chunk) {
|
|
493
|
+
if (this.buffer.length === 0) {
|
|
494
|
+
this.buffer = chunk.slice();
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
const next = new Uint8Array(this.buffer.length + chunk.length);
|
|
498
|
+
next.set(this.buffer, 0);
|
|
499
|
+
next.set(chunk, this.buffer.length);
|
|
500
|
+
this.buffer = next;
|
|
501
|
+
}
|
|
502
|
+
/** Consume every COMPLETE top-level box now in the buffer. */
|
|
503
|
+
drainBoxes() {
|
|
504
|
+
const units = [];
|
|
505
|
+
for (;;) {
|
|
506
|
+
const header = this.readHeader();
|
|
507
|
+
if (this.faultReason !== null) return units;
|
|
508
|
+
if (header === null) break;
|
|
509
|
+
if (this.cursor + header.totalBytes > this.buffer.length) break;
|
|
510
|
+
const boxStart = this.cursor;
|
|
511
|
+
const boxEnd = boxStart + header.totalBytes;
|
|
512
|
+
this.cursor = boxEnd;
|
|
513
|
+
const unit = this.consumeBox(header.type, boxStart, boxEnd);
|
|
514
|
+
if (this.faultReason !== null) return units;
|
|
515
|
+
if (unit !== null) units.push(unit);
|
|
516
|
+
}
|
|
517
|
+
this.compact();
|
|
518
|
+
return units;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Apply one box to the state machine. Returns a unit when this box CLOSED
|
|
522
|
+
* one, `null` otherwise.
|
|
523
|
+
*/
|
|
524
|
+
consumeBox(type, boxStart, boxEnd) {
|
|
525
|
+
if (this.state === "init") {
|
|
526
|
+
if (type !== "moof") return null;
|
|
527
|
+
if (boxStart === this.unitStart) {
|
|
528
|
+
this.fail("a moof arrived before any initialisation box — there is no ftyp/moov to send");
|
|
529
|
+
return null;
|
|
530
|
+
}
|
|
531
|
+
const init = this.emit("init", this.unitStart, boxStart);
|
|
532
|
+
this.unitStart = boxStart;
|
|
533
|
+
this.state = "fragment";
|
|
534
|
+
return init;
|
|
535
|
+
}
|
|
536
|
+
if (this.state === "idle") {
|
|
537
|
+
if (type !== "moof") {
|
|
538
|
+
this.interstitial.add(type);
|
|
539
|
+
this.unitStart = boxEnd;
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
this.unitStart = boxStart;
|
|
543
|
+
this.state = "fragment";
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
if (type !== "mdat") return null;
|
|
547
|
+
const fragment = this.emit("fragment", this.unitStart, boxEnd);
|
|
548
|
+
this.unitStart = boxEnd;
|
|
549
|
+
this.state = "idle";
|
|
550
|
+
return fragment;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Parse the header at {@link cursor}, or `null` when too few bytes have
|
|
554
|
+
* arrived to know. Faults on a size the splitter cannot honour.
|
|
555
|
+
*/
|
|
556
|
+
readHeader() {
|
|
557
|
+
const available = this.buffer.length - this.cursor;
|
|
558
|
+
if (available < BOX_HEADER_BYTES) return null;
|
|
559
|
+
const view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
560
|
+
const size = view.getUint32(this.cursor);
|
|
561
|
+
const type = String.fromCharCode(this.buffer[this.cursor + 4] ?? 0, this.buffer[this.cursor + 5] ?? 0, this.buffer[this.cursor + 6] ?? 0, this.buffer[this.cursor + 7] ?? 0);
|
|
562
|
+
if (size === 0) {
|
|
563
|
+
this.fail(`box "${type}" declares size 0 (to EOF) — an unbounded box cannot be fragmented`);
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
if (size === 1) {
|
|
567
|
+
if (available < LARGE_BOX_HEADER_BYTES) return null;
|
|
568
|
+
const large = view.getBigUint64(this.cursor + BOX_HEADER_BYTES);
|
|
569
|
+
if (large > BigInt(this.maxUnitBytes)) {
|
|
570
|
+
this.fail(`box "${type}" declares ${large} bytes, over the ${this.maxUnitBytes} byte bound`);
|
|
571
|
+
return null;
|
|
572
|
+
}
|
|
573
|
+
return {
|
|
574
|
+
type,
|
|
575
|
+
totalBytes: Number(large)
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
if (size < BOX_HEADER_BYTES) {
|
|
579
|
+
this.fail(`box "${type}" declares an impossible size of ${size} bytes`);
|
|
580
|
+
return null;
|
|
581
|
+
}
|
|
582
|
+
return {
|
|
583
|
+
type,
|
|
584
|
+
totalBytes: size
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
emit(kind, start, end) {
|
|
588
|
+
const sequence = this.nextSequence;
|
|
589
|
+
this.nextSequence += 1;
|
|
590
|
+
return {
|
|
591
|
+
kind,
|
|
592
|
+
data: this.buffer.slice(start, end),
|
|
593
|
+
sequence
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Drop everything already emitted or discarded. Without this the buffer is
|
|
598
|
+
* the whole stream and the process dies in hours, not minutes.
|
|
599
|
+
*/
|
|
600
|
+
compact() {
|
|
601
|
+
if (this.unitStart === 0) return;
|
|
602
|
+
this.buffer = this.buffer.slice(this.unitStart);
|
|
603
|
+
this.cursor -= this.unitStart;
|
|
604
|
+
this.unitStart = 0;
|
|
605
|
+
}
|
|
606
|
+
fail(reason) {
|
|
607
|
+
this.faultReason = reason;
|
|
608
|
+
this.buffer = new Uint8Array(0);
|
|
609
|
+
this.unitStart = 0;
|
|
610
|
+
this.cursor = 0;
|
|
611
|
+
return [];
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
//#endregion
|
|
615
|
+
export { buildAudioArgs as a, buildVideoArgs as c, logBannerArgs as d, pickVideoEncoder as f, audioPlanFromEncodeProfile as i, invocationFromEncodeProfile as l, canonicalHash as n, buildFfmpegArgs as o, AUDIO_PRESETS as r, buildInputArgs as s, Fmp4BoxSplitter as t, isSoftwareDecode as u };
|