@trillboards/edge-platform-rtsp 0.2.8 → 0.2.9

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.
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.RTSPStreamCameraProvider = exports.volumeFreeBytes = exports.buildSegmentOutputArgs = exports.InputStreamProbe = exports.parseInputVideoStreamLine = exports.parseSegmentListCsv = exports.RING_FREE_FRACTION = exports.SEGMENT_SUSPEND_MAX_MS = exports.SEGMENT_SUSPEND_BASE_MS = exports.CLOSED_SEGMENTS_DIRNAME = exports.RECORDABLE_CODECS = exports.streamSessionStatsFor = exports.__resetStreamAdmissionForTests = exports.FrameRing = exports.MjpegFrameSplitter = exports.jpegFrameEnd = exports.JPEG_MALFORMED = exports.JPEG_NEED_MORE = exports.DEFAULT_MAX_STREAM_SESSIONS_PER_SOURCE = exports.RtspStreamStaleError = exports.RtspStreamAdmissionError = void 0;
26
+ exports.RTSPStreamCameraProvider = exports.volumeFreeBytes = exports.buildSegmentOutputArgs = exports.readVideoStartSec = exports.videoStartSecFromMoov = exports.audioPlanFor = exports.describeAudioSource = exports.aacBitrateFor = exports.InputStreamProbe = exports.parseInputAudioStreamLine = exports.parseInputVideoStreamLine = exports.parseSegmentListCsv = exports.AUDIO_SUSPEND_MAX_MS = exports.AUDIO_SUSPEND_BASE_MS = exports.AUDIO_START_DEADLINE_MS = exports.RING_FREE_FRACTION = exports.SEGMENT_SUSPEND_MAX_MS = exports.SEGMENT_SUSPEND_BASE_MS = exports.CLOSED_SEGMENTS_DIRNAME = exports.RECORDABLE_CODECS = exports.streamSessionStatsFor = exports.__resetStreamAdmissionForTests = exports.FrameRing = exports.MjpegFrameSplitter = exports.jpegFrameEnd = exports.JPEG_MALFORMED = exports.JPEG_NEED_MORE = exports.DEFAULT_MAX_STREAM_SESSIONS_PER_SOURCE = exports.RtspStreamStaleError = exports.RtspStreamAdmissionError = void 0;
27
27
  const child_process_1 = require("child_process");
28
28
  const events_1 = require("events");
29
29
  const fs = __importStar(require("fs"));
@@ -228,6 +228,31 @@ const ffmpegStderr_1 = require("./ffmpegStderr");
228
228
  * second output, opened its input and then produced nothing is read as the
229
229
  * output's failure: the next session runs look-only and recording retries
230
230
  * after a doubling cooldown (`SEGMENT_SUSPEND_BASE_MS`..`SEGMENT_SUSPEND_MAX_MS`).
231
+ *
232
+ * ── THE CAMERA'S AUDIO RIDES IN THE SAME SEGMENTS ─────────────────────────
233
+ *
234
+ * When ffmpeg's input description names an audio stream, the second output
235
+ * also maps the camera's first audio track (`-map 0:a:0?`): AAC is copied as
236
+ * is; every other codec (G.711, G.726, Opus, PCM, …) is transcoded ONCE to
237
+ * AAC-LC at the camera's own rate and channel count (`audioPlanFor`). The look
238
+ * output keeps `-an`, the video stays a copy, and a camera with no audio track
239
+ * gets exactly the video-only argv. Measured on the mediamtx rig (ffmpeg
240
+ * 6.1.1; the CPU cost is in `aacBitrateFor`), three facts shape the code:
241
+ *
242
+ * 1. The audio starts at the first audio PACKET and the copy at the first
243
+ * video KEYFRAME, so with audio mapped the segment list's zero moves
244
+ * earlier by that gap (0.905 s on a 6 fps HEVC camera; one GOP at most).
245
+ * `anchorFor` reads the gap back from the first segment's own edit list.
246
+ * 2. A camera that ANNOUNCES a microphone and sends no audio packets stalls
247
+ * the whole second output: the AAC encoder never starts, so the segment
248
+ * muxer never writes a file (40 s, nothing on disk, look path fine). With
249
+ * audio flowing the first file exists within 0.1 s of the first frame, so
250
+ * no file `AUDIO_START_DEADLINE_MS` after the first frame is a stall.
251
+ * 3. Audio is the junior track. Any failure of a session that mapped audio —
252
+ * a stall, a death before the first frame, a write error — suspends AUDIO
253
+ * (`AUDIO_SUSPEND_BASE_MS`..`AUDIO_SUSPEND_MAX_MS`) and the next session
254
+ * records video exactly as before. Only a video-only failure suspends
255
+ * recording.
231
256
  */
232
257
  /** The recorder's persistent-session lane is full. Degrade, never queue. */
233
258
  class RtspStreamAdmissionError extends Error {
@@ -789,6 +814,26 @@ exports.SEGMENT_SUSPEND_MAX_MS = 3600000;
789
814
  const SEGMENT_OUTPUT_FAILURE_RE = /(Failed to open segment[^\n]*|No space left on device|Read-only file system|Disk quota exceeded|Could not write header[^\n]*|Error (?:muxing|submitting|writing) (?:a )?packet[^\n]*)/i;
790
815
  /** The ring may hold at most this fraction of the volume's free bytes. */
791
816
  exports.RING_FREE_FRACTION = 0.1;
817
+ /**
818
+ * A session that maps audio and has written no segment file this long after
819
+ * its first frame is stalled on a microphone that sends nothing. Measured: with
820
+ * audio flowing the first file existed within 0.1 s of the first frame on three
821
+ * rig cameras (polled every 100 ms); with an announced-but-silent track, no file
822
+ * in 40 s.
823
+ */
824
+ exports.AUDIO_START_DEADLINE_MS = 15000;
825
+ /** First audio pause after a session with audio mapped failed. */
826
+ exports.AUDIO_SUSPEND_BASE_MS = 10 * 60000;
827
+ /**
828
+ * Ceiling on that pause. Each retry of a camera whose audio keeps failing costs
829
+ * one look-path restart and, for a stall, the deadline's worth of video, so it
830
+ * settles at one retry a day rather than one an hour.
831
+ */
832
+ exports.AUDIO_SUSPEND_MAX_MS = 24 * 3600000;
833
+ /** AAC-LC's ceiling, enforced by ffmpeg's encoder: 6,144 bits per 1,024-sample frame per channel. */
834
+ const AAC_MAX_BITS_PER_FRAME_PER_CHANNEL = 6144;
835
+ /** Transcode target per channel where the ceiling allows it (11.025 kHz and up). */
836
+ const AAC_TARGET_BPS_PER_CHANNEL = 64000;
792
837
  /**
793
838
  * A segment cannot end after it was observed closed. A predicted end more
794
839
  * than this past the poll that found it means the anchor is late.
@@ -854,15 +899,74 @@ function parseInputVideoStreamLine(line) {
854
899
  };
855
900
  }
856
901
  exports.parseInputVideoStreamLine = parseInputVideoStreamLine;
902
+ /** ffmpeg's channel-layout words, as its input description prints them. */
903
+ const CHANNEL_LAYOUTS = new Map([
904
+ ['mono', 1],
905
+ ['stereo', 2],
906
+ ['2.1', 3],
907
+ ['3.0', 3],
908
+ ['3.0(back)', 3],
909
+ ['4.0', 4],
910
+ ['quad', 4],
911
+ ['quad(side)', 4],
912
+ ['3.1', 4],
913
+ ['5.0', 5],
914
+ ['5.0(side)', 5],
915
+ ['4.1', 5],
916
+ ['5.1', 6],
917
+ ['5.1(side)', 6],
918
+ ['6.0', 6],
919
+ ['hexagonal', 6],
920
+ ['6.1', 7],
921
+ ['7.0', 7],
922
+ ['7.1', 8],
923
+ ['octagonal', 8],
924
+ ]);
925
+ /**
926
+ * One `Stream #0:N: Audio: …` line from ffmpeg's input description, or null.
927
+ * Real lines (mediamtx rig, ffmpeg 6.1.1): `Audio: pcm_alaw, 8000 Hz, mono,
928
+ * s16, 64 kb/s`, `Audio: aac (LC), 16000 Hz, mono, fltp`, `Audio: opus, 48000
929
+ * Hz, stereo, fltp`; older builds print `1 channels`.
930
+ */
931
+ function parseInputAudioStreamLine(line) {
932
+ const m = /^\s*Stream #0:\d+(?:\[[^\]]*\])?(?:\([^)]*\))?:\s*Audio:\s*([A-Za-z0-9_]+)(.*)$/.exec(line);
933
+ if (!m)
934
+ return null;
935
+ const fields = (m[2] ?? '').split(',').map((f) => f.trim());
936
+ let sampleRate = null;
937
+ let channels = null;
938
+ for (const field of fields) {
939
+ const hz = /^(\d{3,6})\s*Hz$/.exec(field);
940
+ if (hz && sampleRate === null)
941
+ sampleRate = Number(hz[1]);
942
+ const count = /^(\d{1,2})\s+channels?\b/.exec(field);
943
+ if (count && channels === null)
944
+ channels = Number(count[1]);
945
+ const layout = CHANNEL_LAYOUTS.get(field.toLowerCase());
946
+ if (layout !== undefined && channels === null)
947
+ channels = layout;
948
+ }
949
+ return {
950
+ codec: m[1].toLowerCase(),
951
+ sampleRate: sampleRate !== null && sampleRate > 0 ? sampleRate : null,
952
+ channels: channels !== null && channels > 0 ? channels : null,
953
+ };
954
+ }
955
+ exports.parseInputAudioStreamLine = parseInputAudioStreamLine;
857
956
  /**
858
- * Reads ffmpeg's stderr as it arrives and stops at the first input video
859
- * stream, or at the first output description — whichever comes first. Bounded:
860
- * it holds at most one partial line.
957
+ * Reads ffmpeg's stderr as it arrives and finishes at the end of the INPUT
958
+ * description — the first line after `Input #0,` that is not indented (`Stream
959
+ * mapping:`, `Output #N`, a component's `[x @ …]` line). It answers with the
960
+ * first video stream, plus the first audio stream when the input has one; the
961
+ * audio line can come before or after the video line, which is why it does not
962
+ * stop at the video. Bounded: it holds at most one partial line.
861
963
  */
862
964
  class InputStreamProbe {
863
965
  constructor() {
864
966
  this.partial = '';
865
967
  this.inInput = false;
968
+ this.video = null;
969
+ this.audio = null;
866
970
  /** The input was opened (ffmpeg described it). */
867
971
  this.inputOpened = false;
868
972
  this.done = false;
@@ -881,31 +985,258 @@ class InputStreamProbe {
881
985
  this.inputOpened = true;
882
986
  continue;
883
987
  }
884
- if (/^(Output #\d+|Stream mapping:)/.test(line)) {
885
- this.done = true;
886
- return null;
887
- }
988
+ const ended = this.inInput ? line.length > 0 && !/^\s/.test(line) : /^(Output #\d+|Stream mapping:)/.test(line);
989
+ if (ended)
990
+ return this.finish();
888
991
  if (!this.inInput)
889
992
  continue;
890
- const info = parseInputVideoStreamLine(line);
891
- if (info) {
892
- this.info = info;
893
- this.done = true;
894
- return info;
993
+ if (!this.video) {
994
+ const video = parseInputVideoStreamLine(line);
995
+ if (video) {
996
+ this.video = video;
997
+ continue;
998
+ }
895
999
  }
1000
+ if (!this.audio)
1001
+ this.audio = parseInputAudioStreamLine(line);
896
1002
  }
897
1003
  return null;
898
1004
  }
1005
+ finish() {
1006
+ this.done = true;
1007
+ if (!this.video)
1008
+ return null;
1009
+ this.info = this.audio ? { ...this.video, audio: this.audio } : this.video;
1010
+ return this.info;
1011
+ }
899
1012
  }
900
1013
  exports.InputStreamProbe = InputStreamProbe;
1014
+ /**
1015
+ * The transcode's `-b:a`: 64 kb/s per channel, capped at AAC-LC's ceiling of
1016
+ * 6,144 bits per 1,024-sample frame per channel — which at 8 kHz, where almost
1017
+ * every CCTV microphone sits (G.711), is 48,000 b/s, the most AAC can spend on
1018
+ * it. Null when the camera named no rate or channel count.
1019
+ *
1020
+ * Measured (ffmpeg 6.1.1, one core of the dev box, one hour of 8 kHz G.711
1021
+ * A-law pink noise plus a tone): decode + AAC at 48 kb/s costs 40.9 s of CPU
1022
+ * per hour of audio, 1.14% of one core per camera, at 33.9 dB signal-to-
1023
+ * distortion against the decoded G.711 (G.711's own floor is ~38 dB). 48 kHz
1024
+ * stereo Opus to 128 kb/s costs 110 s an hour, 3.1%. A copy (AAC) costs no
1025
+ * codec work. ffmpeg's `-aac_coder fast` halves the cost at 30.9 dB.
1026
+ */
1027
+ function aacBitrateFor(sampleRate, channels) {
1028
+ if (!sampleRate || !channels)
1029
+ return null;
1030
+ const ceiling = Math.floor((AAC_MAX_BITS_PER_FRAME_PER_CHANNEL * sampleRate) / 1024);
1031
+ return Math.min(AAC_TARGET_BPS_PER_CHANNEL, ceiling) * channels;
1032
+ }
1033
+ exports.aacBitrateFor = aacBitrateFor;
1034
+ /** The camera's own audio in ffmpeg's words: `pcm_alaw 8000 Hz 1ch`. */
1035
+ function describeAudioSource(audio) {
1036
+ return [audio.codec, audio.sampleRate ? `${audio.sampleRate} Hz` : null, audio.channels ? `${audio.channels}ch` : null]
1037
+ .filter((part) => part !== null)
1038
+ .join(' ');
1039
+ }
1040
+ exports.describeAudioSource = describeAudioSource;
1041
+ /**
1042
+ * What the second output does with this audio stream: copy AAC (MP4-legal as
1043
+ * is), transcode anything else once to AAC, and leave out a stream ffmpeg
1044
+ * could not identify (it cannot be decoded, so mapping it would only fail).
1045
+ */
1046
+ function audioPlanFor(audio) {
1047
+ if (!audio || !audio.codec || audio.codec === 'none')
1048
+ return null;
1049
+ const source = describeAudioSource(audio);
1050
+ if (audio.codec === 'aac')
1051
+ return { mode: 'copy', bitrateBps: null, source };
1052
+ return { mode: 'aac', bitrateBps: aacBitrateFor(audio.sampleRate, audio.channels), source };
1053
+ }
1054
+ exports.audioPlanFor = audioPlanFor;
1055
+ function sameAudioPlan(a, b) {
1056
+ if (!a || !b)
1057
+ return a === b;
1058
+ return a.mode === b.mode && a.bitrateBps === b.bitrateBps && a.source === b.source;
1059
+ }
1060
+ function sameAudioInfo(a, b) {
1061
+ if (!a || !b)
1062
+ return a === b;
1063
+ return a.codec === b.codec && a.sampleRate === b.sampleRate && a.channels === b.channels;
1064
+ }
1065
+ // ---------------------------------------------------------------------------
1066
+ // The video's start inside a segment, read from the segment's own edit list
1067
+ // ---------------------------------------------------------------------------
1068
+ /** A moov larger than this is not one of our segments (its tables are kilobytes). */
1069
+ const MAX_SEGMENT_MOOV_BYTES = 16 * 1024 * 1024;
1070
+ /** A lead longer than any GOP a camera sends is a misread, not a lead. */
1071
+ const MAX_VIDEO_LEAD_SEC = 60;
1072
+ function mp4Children(buf, start, end) {
1073
+ const out = [];
1074
+ let o = start;
1075
+ while (o + 8 <= end) {
1076
+ let size = buf.readUInt32BE(o);
1077
+ const type = buf.toString('latin1', o + 4, o + 8);
1078
+ let header = 8;
1079
+ if (size === 1) {
1080
+ if (o + 16 > end)
1081
+ break;
1082
+ const big = buf.readBigUInt64BE(o + 8);
1083
+ if (big > BigInt(Number.MAX_SAFE_INTEGER))
1084
+ break;
1085
+ size = Number(big);
1086
+ header = 16;
1087
+ }
1088
+ else if (size === 0) {
1089
+ size = end - o;
1090
+ }
1091
+ if (size < header || o + size > end)
1092
+ break;
1093
+ out.push({ type, start: o + header, end: o + size });
1094
+ o += size;
1095
+ }
1096
+ return out;
1097
+ }
1098
+ function mp4Child(buf, parent, type) {
1099
+ return mp4Children(buf, parent.start, parent.end).find((b) => b.type === type) ?? null;
1100
+ }
1101
+ /** A full box's timescale: mvhd and mdhd both carry it at 12 (version 0) or 20 (version 1). */
1102
+ function fullBoxTimescale(buf, box) {
1103
+ const v1 = buf[box.start] === 1;
1104
+ const at = box.start + (v1 ? 20 : 12);
1105
+ if (at + 4 > box.end)
1106
+ return null;
1107
+ const ts = buf.readUInt32BE(at);
1108
+ return ts > 0 ? ts : null;
1109
+ }
1110
+ /**
1111
+ * Where the video track starts on a segment's timeline, in seconds, from its
1112
+ * `moov`: the leading empty edits (movie timescale) minus the first media
1113
+ * edit's start (track timescale). ffmpeg's mov muxer writes exactly that pair
1114
+ * — an empty edit of the first sample's presentation time, then a media edit
1115
+ * from its composition offset — so the difference is the first video packet's
1116
+ * decode time: 0 for a video-only copy (with or without B-frames), and the
1117
+ * audio lead when the audio started first. 0 for anything it cannot read.
1118
+ */
1119
+ function videoStartSecFromMoov(moov) {
1120
+ const root = { type: 'moov', start: 0, end: moov.length };
1121
+ const mvhd = mp4Child(moov, root, 'mvhd');
1122
+ const movieTimescale = mvhd ? fullBoxTimescale(moov, mvhd) : null;
1123
+ if (!movieTimescale)
1124
+ return 0;
1125
+ for (const trak of mp4Children(moov, root.start, root.end).filter((b) => b.type === 'trak')) {
1126
+ const mdia = mp4Child(moov, trak, 'mdia');
1127
+ const hdlr = mdia ? mp4Child(moov, mdia, 'hdlr') : null;
1128
+ if (!mdia || !hdlr || hdlr.end - hdlr.start < 12)
1129
+ continue;
1130
+ if (moov.toString('latin1', hdlr.start + 8, hdlr.start + 12) !== 'vide')
1131
+ continue;
1132
+ const mdhd = mp4Child(moov, mdia, 'mdhd');
1133
+ const trackTimescale = mdhd ? fullBoxTimescale(moov, mdhd) : null;
1134
+ const edts = mp4Child(moov, trak, 'edts');
1135
+ const elst = edts ? mp4Child(moov, edts, 'elst') : null;
1136
+ if (!trackTimescale || !elst || elst.end - elst.start < 8)
1137
+ return 0;
1138
+ const v1 = moov[elst.start] === 1;
1139
+ const entrySize = v1 ? 20 : 12;
1140
+ const count = moov.readUInt32BE(elst.start + 4);
1141
+ let emptySec = 0;
1142
+ let mediaSec = 0;
1143
+ for (let i = 0; i < count; i += 1) {
1144
+ const at = elst.start + 8 + i * entrySize;
1145
+ if (at + entrySize > elst.end)
1146
+ return 0;
1147
+ const duration = v1 ? Number(moov.readBigUInt64BE(at)) : moov.readUInt32BE(at);
1148
+ const mediaTime = v1 ? Number(moov.readBigInt64BE(at + 8)) : moov.readInt32BE(at + 4);
1149
+ if (mediaTime === -1) {
1150
+ emptySec += duration / movieTimescale;
1151
+ continue;
1152
+ }
1153
+ mediaSec = mediaTime / trackTimescale;
1154
+ break;
1155
+ }
1156
+ const lead = emptySec - mediaSec;
1157
+ return Number.isFinite(lead) && lead > 0 && lead <= MAX_VIDEO_LEAD_SEC ? lead : 0;
1158
+ }
1159
+ return 0;
1160
+ }
1161
+ exports.videoStartSecFromMoov = videoStartSecFromMoov;
1162
+ /** `videoStartSecFromMoov` of a closed segment file; 0 when it cannot be read. */
1163
+ function readVideoStartSec(file) {
1164
+ let fd;
1165
+ try {
1166
+ fd = fs.openSync(file, 'r');
1167
+ }
1168
+ catch {
1169
+ return 0;
1170
+ }
1171
+ try {
1172
+ const size = fs.fstatSync(fd).size;
1173
+ const head = Buffer.alloc(16);
1174
+ let offset = 0;
1175
+ while (offset + 8 <= size) {
1176
+ const n = fs.readSync(fd, head, 0, 16, offset);
1177
+ if (n < 8)
1178
+ return 0;
1179
+ let boxSize = head.readUInt32BE(0);
1180
+ const type = head.toString('latin1', 4, 8);
1181
+ let header = 8;
1182
+ if (boxSize === 1) {
1183
+ if (n < 16)
1184
+ return 0;
1185
+ const big = head.readBigUInt64BE(8);
1186
+ if (big > BigInt(Number.MAX_SAFE_INTEGER))
1187
+ return 0;
1188
+ boxSize = Number(big);
1189
+ header = 16;
1190
+ }
1191
+ else if (boxSize === 0) {
1192
+ boxSize = size - offset;
1193
+ }
1194
+ if (boxSize < header || offset + boxSize > size)
1195
+ return 0;
1196
+ if (type === 'moov') {
1197
+ if (boxSize - header > MAX_SEGMENT_MOOV_BYTES)
1198
+ return 0;
1199
+ const moov = Buffer.alloc(boxSize - header);
1200
+ if (fs.readSync(fd, moov, 0, moov.length, offset + header) !== moov.length)
1201
+ return 0;
1202
+ return videoStartSecFromMoov(moov);
1203
+ }
1204
+ offset += boxSize;
1205
+ }
1206
+ return 0;
1207
+ }
1208
+ catch {
1209
+ return 0;
1210
+ }
1211
+ finally {
1212
+ fs.closeSync(fd);
1213
+ }
1214
+ }
1215
+ exports.readVideoStartSec = readVideoStartSec;
901
1216
  /** The second output, exactly. Pure, so the argv can be asserted. */
902
1217
  function buildSegmentOutputArgs(o) {
1218
+ const audio = o.audio ?? null;
903
1219
  return [
904
1220
  '-map',
905
1221
  '0:v',
1222
+ // The FIRST audio track only, and optional: a camera whose next session
1223
+ // no longer has it records video rather than failing the output.
1224
+ ...(audio ? ['-map', '0:a:0?'] : []),
906
1225
  '-c:v',
907
1226
  'copy',
908
1227
  ...(o.tagHvc1 ? ['-tag:v', 'hvc1'] : []),
1228
+ ...(audio
1229
+ ? [
1230
+ '-c:a',
1231
+ audio.mode === 'copy' ? 'copy' : 'aac',
1232
+ ...(audio.mode === 'aac' && audio.bitrateBps !== null ? ['-b:a', String(audio.bitrateBps)] : []),
1233
+ // The camera's own codec, rate and channels travel inside the file:
1234
+ // a transcode leaves AAC in the box, and this is the one place that
1235
+ // still says it began as, say, 8 kHz G.711.
1236
+ '-metadata:s:a:0',
1237
+ `handler_name=camera ${audio.source}`,
1238
+ ]
1239
+ : []),
909
1240
  // The copy starts at the first KEYFRAME, but the output timeline starts at
910
1241
  // the first packet received, and the segment list reports the first
911
1242
  // segment as starting at 0 regardless. Measured on the mediamtx rig: a
@@ -1045,6 +1376,16 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1045
1376
  this.segmentSuspension = null;
1046
1377
  this.segmentSuspendTimer = null;
1047
1378
  this.consecutiveOutputFailures = 0;
1379
+ /** The camera's audio left out of the segments after a session that mapped it failed. */
1380
+ this.audioSuspension = null;
1381
+ this.audioSuspendTimer = null;
1382
+ this.consecutiveAudioFailures = 0;
1383
+ this.audioCounters = {
1384
+ failures: 0,
1385
+ stalls: 0,
1386
+ lastVideoLeadMs: null,
1387
+ lastError: null,
1388
+ };
1048
1389
  this.measuredSegmentBytes = 0;
1049
1390
  this.measuredSegmentSeconds = 0;
1050
1391
  this.segCounters = {
@@ -1218,11 +1559,15 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1218
1559
  this.degradedReason = null;
1219
1560
  this.currentRestartDelay = this.restartDelayMs;
1220
1561
  this.startingUp = true;
1562
+ // An audio pause outlives close(); its end must still bring the audio back.
1563
+ this.armAudioRetry();
1221
1564
  let lastErr;
1222
1565
  // A failure the second output caused is not the recorder refusing this
1223
- // feed: it buys one look-only attempt that the validation budget does not
1224
- // pay for, so recording can never be what degrades a feed to per-grab.
1225
- let recordingRetryGranted = false;
1566
+ // feed: it buys one more attempt that the validation budget does not pay
1567
+ // for, so recording can never be what degrades a feed to per-grab. At most
1568
+ // two — each one strips the second output (audio first, then recording),
1569
+ // so the third session is look-only.
1570
+ let recordingRetriesGranted = 0;
1226
1571
  try {
1227
1572
  for (let attempt = 1; attempt <= this.openValidationAttempts; attempt += 1) {
1228
1573
  const outputFailuresBefore = this.segCounters.outputFailures;
@@ -1238,8 +1583,8 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1238
1583
  catch (err) {
1239
1584
  lastErr = err;
1240
1585
  this.stopProcess();
1241
- if (!recordingRetryGranted && this.segCounters.outputFailures > outputFailuresBefore) {
1242
- recordingRetryGranted = true;
1586
+ if (recordingRetriesGranted < 2 && this.segCounters.outputFailures > outputFailuresBefore) {
1587
+ recordingRetriesGranted += 1;
1243
1588
  attempt -= 1;
1244
1589
  }
1245
1590
  if (attempt < this.openValidationAttempts && this.restartDelayMs > 0) {
@@ -1366,6 +1711,10 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1366
1711
  clearTimeout(this.segmentSuspendTimer);
1367
1712
  this.segmentSuspendTimer = null;
1368
1713
  }
1714
+ if (this.audioSuspendTimer) {
1715
+ clearTimeout(this.audioSuspendTimer);
1716
+ this.audioSuspendTimer = null;
1717
+ }
1369
1718
  // The session's own exit finishes its recording (final list read, partial
1370
1719
  // deleted); a process that exits first leaves it for the next open().
1371
1720
  this.stopProcess();
@@ -1600,6 +1949,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1600
1949
  segmentSeconds: recording.plan.segmentSeconds,
1601
1950
  tagHvc1: recording.plan.tagHvc1,
1602
1951
  minSegDuration: recording.plan.minSegDuration,
1952
+ audio: recording.plan.audio,
1603
1953
  }
1604
1954
  : null);
1605
1955
  this.sessionStarts += 1;
@@ -1825,11 +2175,25 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1825
2175
  tagHvc1: codec !== 'h264',
1826
2176
  // Present since ffmpeg 5; an older binary gets the bare segment_time.
1827
2177
  minSegDuration: this.ffmpegMajorVersion >= 5,
2178
+ audio: this.planAudio(),
1828
2179
  };
1829
2180
  if (this.segmentSuspension && this.now() < this.segmentSuspension.untilMs)
1830
2181
  return null;
1831
2182
  return plan;
1832
2183
  }
2184
+ /**
2185
+ * The camera's audio for the next session — decided ONLY from ffmpeg's own
2186
+ * description of this input, never speculated: a session that has not been
2187
+ * described yet records video only, exactly as before, and the description
2188
+ * of an audio track then costs one reconfigure. Speculating would put audio
2189
+ * options on every camera with no microphone (ffmpeg warns that `-b:a` was
2190
+ * never used) and risk the stall above on a camera nobody has seen yet.
2191
+ */
2192
+ planAudio() {
2193
+ if (this.audioSuspension && this.now() < this.audioSuspension.untilMs)
2194
+ return null;
2195
+ return audioPlanFor(this.streamInfo?.audio);
2196
+ }
1833
2197
  /** Does a running session's second output already give what `want` asks for? */
1834
2198
  sessionSatisfies(have, want) {
1835
2199
  if (!want)
@@ -1838,7 +2202,8 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1838
2202
  return false;
1839
2203
  if (have.dir !== want.dir ||
1840
2204
  have.segmentSeconds !== want.segmentSeconds ||
1841
- have.minSegDuration !== want.minSegDuration) {
2205
+ have.minSegDuration !== want.minSegDuration ||
2206
+ !sameAudioPlan(have.audio, want.audio)) {
1842
2207
  return false;
1843
2208
  }
1844
2209
  if (have.tagHvc1 === want.tagHvc1)
@@ -1864,7 +2229,8 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1864
2229
  return;
1865
2230
  this.segCounters.reconfigures += 1;
1866
2231
  this.log('info', want
1867
- ? `RTSP stream session restarting to record ${want.segmentSeconds}s segments`
2232
+ ? `RTSP stream session restarting to record ${want.segmentSeconds}s segments` +
2233
+ (want.audio ? ` with the camera's audio (${want.audio.mode === 'copy' ? 'copied' : 'to AAC'}, ${want.audio.source})` : '')
1868
2234
  : 'RTSP stream session restarting to stop segment recording');
1869
2235
  session.superseded = true;
1870
2236
  this.reconfigurePendingFor = session;
@@ -1881,8 +2247,9 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1881
2247
  return;
1882
2248
  const previous = this.streamInfo;
1883
2249
  this.streamInfo = info;
1884
- if (!previous || previous.codec !== info.codec)
2250
+ if (!previous || previous.codec !== info.codec || !sameAudioInfo(previous.audio, info.audio)) {
1885
2251
  this.reconcileRecording();
2252
+ }
1886
2253
  }
1887
2254
  /** The work directory for a session that will record, or null for look-only. */
1888
2255
  prepareSessionRecording() {
@@ -1910,6 +2277,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1910
2277
  handled: new Set(),
1911
2278
  anchor: null,
1912
2279
  closedCount: 0,
2280
+ outputStarted: false,
1913
2281
  };
1914
2282
  }
1915
2283
  /** Pause recording, doubling from SEGMENT_SUSPEND_BASE_MS, then reconcile again. */
@@ -1937,6 +2305,75 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1937
2305
  this.segmentSuspendTimer = null;
1938
2306
  }
1939
2307
  }
2308
+ /**
2309
+ * Leave the camera's audio out of the segments for a doubling pause
2310
+ * (`AUDIO_SUSPEND_BASE_MS`..`AUDIO_SUSPEND_MAX_MS`), then reconcile again.
2311
+ * Counted as a second-output failure too, so `open()` does not spend a
2312
+ * validation attempt on it; video recording is untouched.
2313
+ */
2314
+ suspendAudio(reason, stall) {
2315
+ this.segCounters.outputFailures += 1;
2316
+ this.audioCounters.failures += 1;
2317
+ if (stall)
2318
+ this.audioCounters.stalls += 1;
2319
+ const exponent = Math.min(this.consecutiveAudioFailures, 16);
2320
+ const cooldownMs = Math.min(exports.AUDIO_SUSPEND_MAX_MS, exports.AUDIO_SUSPEND_BASE_MS * 2 ** exponent);
2321
+ this.consecutiveAudioFailures += 1;
2322
+ this.audioSuspension = { untilMs: this.now() + cooldownMs };
2323
+ this.audioCounters.lastError = `${reason}; audio paused ${Math.round(cooldownMs / 1000)}s, video keeps recording`;
2324
+ this.log('warn', `segment recording: ${this.audioCounters.lastError}`);
2325
+ this.armAudioRetry();
2326
+ }
2327
+ /**
2328
+ * The ONE thing that brings audio back after a pause: reconcile when the
2329
+ * pause ends. Armed by `suspendAudio`, and re-armed by `open()` — `close()`
2330
+ * cancels the timer but keeps the pause, so a feed reopened mid-pause would
2331
+ * otherwise record video only until some unrelated restart.
2332
+ */
2333
+ armAudioRetry() {
2334
+ if (this.audioSuspendTimer)
2335
+ clearTimeout(this.audioSuspendTimer);
2336
+ this.audioSuspendTimer = null;
2337
+ if (!this.audioSuspension)
2338
+ return;
2339
+ const remainingMs = this.audioSuspension.untilMs - this.now();
2340
+ if (remainingMs <= 0)
2341
+ return; // already over: the next plan maps audio
2342
+ this.audioSuspendTimer = setTimeout(() => {
2343
+ this.audioSuspendTimer = null;
2344
+ this.reconcileRecording();
2345
+ }, remainingMs);
2346
+ if (typeof this.audioSuspendTimer.unref === 'function')
2347
+ this.audioSuspendTimer.unref();
2348
+ }
2349
+ /**
2350
+ * A session that maps audio and has written no file `AUDIO_START_DEADLINE_MS`
2351
+ * after its first frame is stalled on a microphone that sends nothing: drop
2352
+ * the audio and restart the session video-only. Checked once, at the deadline.
2353
+ */
2354
+ checkAudioStall(session, rec, nowMs) {
2355
+ if (!rec.plan.audio || rec.outputStarted || session.firstFrameAtMs === null)
2356
+ return false;
2357
+ if (nowMs - session.firstFrameAtMs < exports.AUDIO_START_DEADLINE_MS)
2358
+ return false;
2359
+ let written = rec.handled.size > 0;
2360
+ if (!written) {
2361
+ try {
2362
+ written = fs.readdirSync(rec.workDir).some((name) => /^seg_\d+\.mp4$/i.test(name));
2363
+ }
2364
+ catch {
2365
+ written = false;
2366
+ }
2367
+ }
2368
+ if (written) {
2369
+ rec.outputStarted = true;
2370
+ return false;
2371
+ }
2372
+ this.suspendAudio(`the segment output wrote nothing ${Math.round(exports.AUDIO_START_DEADLINE_MS / 1000)}s after the first frame with ` +
2373
+ `the camera's audio (${rec.plan.audio.source}) mapped: its microphone sends no audio`, true);
2374
+ this.reconcileRecording();
2375
+ return true;
2376
+ }
1940
2377
  ensureSegmentPolling() {
1941
2378
  if (this.segmentPollTimer)
1942
2379
  return;
@@ -1972,6 +2409,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1972
2409
  }
1973
2410
  if (rec.pending.length > 0 && this.flushPending(session) > 0)
1974
2411
  this.enforceRing();
2412
+ this.checkAudioStall(session, rec, nowMs);
1975
2413
  }
1976
2414
  catch (err) {
1977
2415
  this.segCounters.lastError = `segment list: ${errorText(err)}`;
@@ -2069,12 +2507,22 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2069
2507
  * at the first frame's wall clock and every later one at its offset from it.
2070
2508
  * When ffmpeg starts the output timeline at 0 — measured 0.000000 for H.264
2071
2509
  * and HEVC through mediamtx — that is exactly first frame + startSec.
2510
+ *
2511
+ * With the camera's audio mapped the timeline starts at the first audio
2512
+ * PACKET instead, which can arrive before the first video keyframe (rig:
2513
+ * 0.905 s on a 6 fps HEVC camera, every listed time shifted by it). The first
2514
+ * frame then sits at the first segment's VIDEO start, which the file's own
2515
+ * edit list states (`readVideoStartSec`), so that is the anchor's media time.
2516
+ * Without it every segment of the session would be dated up to a GOP late.
2072
2517
  */
2073
2518
  anchorFor(rec, entry, firstFrameAtMs, stream) {
2074
2519
  if (!rec.anchor) {
2520
+ const leadSec = rec.plan.audio && firstFrameAtMs !== null ? readVideoStartSec(path.join(rec.workDir, entry.name)) : 0;
2521
+ if (rec.plan.audio && firstFrameAtMs !== null)
2522
+ this.audioCounters.lastVideoLeadMs = Math.round(leadSec * 1000);
2075
2523
  rec.anchor = {
2076
2524
  wallMs: firstFrameAtMs ?? this.now() - (entry.endSec - entry.startSec) * 1000,
2077
- startSec: entry.startSec,
2525
+ startSec: entry.startSec + leadSec,
2078
2526
  };
2079
2527
  this.persistAnchor(rec, stream);
2080
2528
  }
@@ -2220,15 +2668,28 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2220
2668
  // - it closed nothing and ffmpeg named a write failure.
2221
2669
  // Either way the next session must not carry it. A camera that dropped
2222
2670
  // mid-segment says neither, and keeps recording through its restart.
2671
+ // When the session carried the camera's audio, the audio is what goes:
2672
+ // the next session records video exactly as it did before audio existed,
2673
+ // and only a failure of THAT session pauses recording.
2223
2674
  const judged = !session.superseded && !session.detached && rec.closedCount === 0;
2224
- if (judged && session.probe.inputOpened && session.firstFrameAtMs === null) {
2675
+ const noFrame = judged && session.probe.inputOpened && session.firstFrameAtMs === null;
2676
+ const writeFailure = judged && !noFrame && SEGMENT_OUTPUT_FAILURE_RE.test(stderr);
2677
+ if ((noFrame || writeFailure) && rec.plan.audio) {
2678
+ this.suspendAudio(noFrame
2679
+ ? `a session with the camera's audio (${rec.plan.audio.source}) mapped opened its input and produced no frame`
2680
+ : `segment output failed with the camera's audio (${rec.plan.audio.source}) mapped: ` +
2681
+ (0, RTSPCameraProvider_1.summarizeFfmpegStderr)(stderr.match(SEGMENT_OUTPUT_FAILURE_RE)?.[0] ?? '', 120), false);
2682
+ }
2683
+ else if (noFrame) {
2225
2684
  this.suspendRecording('a session with segment recording opened its input and produced no frame');
2226
2685
  }
2227
- else if (judged && SEGMENT_OUTPUT_FAILURE_RE.test(stderr)) {
2686
+ else if (writeFailure) {
2228
2687
  this.suspendRecording(`segment output failed: ${(0, RTSPCameraProvider_1.summarizeFfmpegStderr)(stderr.match(SEGMENT_OUTPUT_FAILURE_RE)?.[0] ?? '', 120)}`);
2229
2688
  }
2230
2689
  else if (rec.closedCount > 0) {
2231
2690
  this.consecutiveOutputFailures = 0;
2691
+ if (rec.plan.audio)
2692
+ this.consecutiveAudioFailures = 0;
2232
2693
  }
2233
2694
  // Everything left was never listed: the segment ffmpeg was still writing,
2234
2695
  // the list and the anchor. A listed file the OS would not let us move yet
@@ -2514,6 +2975,38 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2514
2975
  lastAnchorDriftMs: c.lastAnchorDriftMs,
2515
2976
  lastSegmentAtMs: c.lastSegmentAtMs,
2516
2977
  lastError: c.lastError,
2978
+ audio: this.audioStats(),
2979
+ };
2980
+ }
2981
+ /** The camera's audio in the segments. Derived on every call, like the rest. */
2982
+ audioStats() {
2983
+ const source = this.streamInfo?.audio ?? null;
2984
+ const suspension = this.audioSuspension && this.now() < this.audioSuspension.untilMs ? this.audioSuspension : null;
2985
+ const live = this.session?.recording?.plan.audio ?? null;
2986
+ let state;
2987
+ if (live)
2988
+ state = live.mode === 'copy' ? 'copy' : 'transcode';
2989
+ else if (!this.streamInfo)
2990
+ state = 'unknown';
2991
+ else if (!source)
2992
+ state = 'no_audio_track';
2993
+ else if (!audioPlanFor(source))
2994
+ state = 'unsupported_codec';
2995
+ else if (suspension)
2996
+ state = 'suspended';
2997
+ else
2998
+ state = 'waiting_for_session';
2999
+ return {
3000
+ state,
3001
+ codec: source?.codec ?? null,
3002
+ sampleRate: source?.sampleRate ?? null,
3003
+ channels: source?.channels ?? null,
3004
+ bitrateBps: live?.bitrateBps ?? null,
3005
+ failures: this.audioCounters.failures,
3006
+ stalls: this.audioCounters.stalls,
3007
+ suspendedUntilMs: suspension ? suspension.untilMs : null,
3008
+ lastVideoLeadMs: this.audioCounters.lastVideoLeadMs,
3009
+ lastError: this.audioCounters.lastError,
2517
3010
  };
2518
3011
  }
2519
3012
  recordingState(suspended) {