@apocaliss92/nodelink-js 0.4.7 → 0.4.8

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/index.d.ts CHANGED
@@ -1311,6 +1311,7 @@ export declare class BaichuanRtspServer extends EventEmitter<{
1311
1311
  private authNonces;
1312
1312
  private readonly AUTH_REALM;
1313
1313
  private readonly NONCE_TIMEOUT_MS;
1314
+ private readonly lazyMetadata;
1314
1315
  private connectedClients;
1315
1316
  private nativeStreamActive;
1316
1317
  private clientConnectionServer;
@@ -1330,10 +1331,20 @@ export declare class BaichuanRtspServer extends EventEmitter<{
1330
1331
  private tempStreamGenerator;
1331
1332
  private nativeFanout;
1332
1333
  private noClientAutoStopTimer;
1334
+ /** Fires if camera never sends frames after stream start (sleeping), even with clients connected. */
1335
+ private noFrameDeadlineTimer;
1333
1336
  /** After last RTSP client; 0 = never auto-stop native stream. */
1334
1337
  private readonly nativeStreamIdleStopMs;
1335
1338
  /** Primed-but-no-PLAY timeout; 0 = disabled. */
1336
1339
  private readonly nativeStreamPrimeIdleStopMs;
1340
+ /**
1341
+ * Max time to wait for the first camera frame after stream start.
1342
+ * If no frames arrive within this window, the native stream is stopped
1343
+ * (camera is sleeping). Prevents the BaichuanVideoStream watchdog from
1344
+ * firing and waking the camera when no real viewer is watching.
1345
+ * 0 = disabled. Defaults to nativeStreamPrimeIdleStopMs * 2 when > 0.
1346
+ */
1347
+ private readonly nativeStreamNoFrameDeadlineMs;
1337
1348
  private readonly PREBUFFER_MAX_MS;
1338
1349
  private prebuffer;
1339
1350
  private static isAdtsAacFrame;
@@ -1344,6 +1355,8 @@ export declare class BaichuanRtspServer extends EventEmitter<{
1344
1355
  private static splitAnnexBNals;
1345
1356
  private static stripAdtsHeader;
1346
1357
  constructor(options: BaichuanRtspServerOptions);
1358
+ /** Number of currently connected RTSP clients. */
1359
+ get clientCount(): number;
1347
1360
  /**
1348
1361
  * Generate a new nonce for Digest authentication
1349
1362
  */
@@ -1369,6 +1382,7 @@ export declare class BaichuanRtspServer extends EventEmitter<{
1369
1382
  */
1370
1383
  private generateWwwAuthenticateHeader;
1371
1384
  private clearNoClientAutoStopTimer;
1385
+ private clearNoFrameDeadlineTimer;
1372
1386
  private setFlowVideoType;
1373
1387
  /**
1374
1388
  * Start the RTSP server.
@@ -1381,6 +1395,21 @@ export declare class BaichuanRtspServer extends EventEmitter<{
1381
1395
  * @param initialBuffer - Any bytes already read during path parsing/auth
1382
1396
  */
1383
1397
  acceptConnection(socket: net.Socket, initialBuffer?: Buffer): void;
1398
+ /**
1399
+ * Inject an already-accepted client socket from a multiplexer
1400
+ * (e.g. `LocalRtspMux`) that owns the listening port.
1401
+ *
1402
+ * The mux reads the first RTSP request line to determine the target path,
1403
+ * then hands the socket over. Any bytes already consumed during routing
1404
+ * are replayed back onto the socket via `unshift()` so the RTSP parser in
1405
+ * `handleRtspConnection` sees the complete original request.
1406
+ *
1407
+ * @param socket - Client TCP socket, already accepted by the mux.
1408
+ * @param preReadData - Bytes the mux has already pulled off the socket
1409
+ * while parsing the request line. Replayed via `socket.unshift()`
1410
+ * before any further reads.
1411
+ */
1412
+ injectSocket(socket: net.Socket, preReadData: Buffer): void;
1384
1413
  /**
1385
1414
  * Handle RTSP connection from a client.
1386
1415
  */
@@ -1481,13 +1510,34 @@ export declare interface BaichuanRtspServerOptions {
1481
1510
  * - "rfc4571": RFC4571 framing: 2-byte length + RTP packet
1482
1511
  */
1483
1512
  tcpRtpFraming?: "rtsp-interleaved" | "rfc4571";
1484
- /** Credentials for RTSP authentication (optional) */
1513
+ /**
1514
+ * Credentials for RTSP authentication (optional).
1515
+ *
1516
+ * Each entry carries either the plaintext `password` or a pre-computed
1517
+ * Digest `ha1 = MD5(username ":" realm ":" password)`. The HA1 variant is
1518
+ * preferred when the consumer stores only hashed credentials (e.g. the
1519
+ * manager reuses dashboard-user HA1 values without ever holding plaintext).
1520
+ *
1521
+ * When both are supplied, `ha1` wins for Digest validation while `password`
1522
+ * is used for Basic authentication.
1523
+ */
1485
1524
  credentials?: Array<{
1486
1525
  username: string;
1487
1526
  password: string;
1527
+ ha1?: string;
1528
+ } | {
1529
+ username: string;
1530
+ password?: string;
1531
+ ha1: string;
1488
1532
  }>;
1489
1533
  /** Require authentication for RTSP connections (default: false if no credentials set) */
1490
1534
  requireAuth?: boolean;
1535
+ /**
1536
+ * Digest authentication realm advertised to clients. Must match the realm
1537
+ * used when pre-computing HA1 values in `credentials[*].ha1`.
1538
+ * Default: "BaichuanRtspServer" (kept for backward compatibility).
1539
+ */
1540
+ authRealm?: string;
1491
1541
  /**
1492
1542
  * External identifier for dedicated socket session.
1493
1543
  * When provided, a dedicated BaichuanClient is created for the stream,
@@ -1500,6 +1550,18 @@ export declare interface BaichuanRtspServerOptions {
1500
1550
  * start() still performs metadata fetch and codec detection.
1501
1551
  */
1502
1552
  externalListener?: boolean;
1553
+ /**
1554
+ * When true, the server runs in **mux mode**: `start()` skips creating
1555
+ * or listening on any TCP server (a shared multiplexer — e.g.
1556
+ * `LocalRtspMux` — owns the public RTSP port and routes accepted sockets
1557
+ * here via `injectSocket()`). `stop()` likewise skips closing a TCP
1558
+ * server it does not own.
1559
+ *
1560
+ * Semantically equivalent to `externalListener: true`, but expressed from
1561
+ * the point of view of the multiplexer that will drive this instance.
1562
+ * Either flag is sufficient; both may be set together.
1563
+ */
1564
+ muxMode?: boolean;
1503
1565
  /**
1504
1566
  * Ms after the last RTSP client disconnects before stopping the native Baichuan stream.
1505
1567
  * 0 = keep the native stream running (matches rtsp proxy idle timeout 0 / always-mounted sources).
@@ -1511,6 +1573,18 @@ export declare interface BaichuanRtspServerOptions {
1511
1573
  * 0 = disable. Default 15000 when nativeStreamIdleStopMs > 0, else 0.
1512
1574
  */
1513
1575
  nativeStreamPrimeIdleStopMs?: number;
1576
+ /**
1577
+ * When true, `start()` does NOT fetch stream metadata from the camera —
1578
+ * the metadata fetch is deferred to the first DESCRIBE. Useful for
1579
+ * battery / UDP cameras so binding the RTSP port at boot does not wake
1580
+ * them up when no client is listening.
1581
+ *
1582
+ * Trade-off: the very first DESCRIBE pays the metadata round-trip
1583
+ * latency. Subsequent connections hit the cached metadata.
1584
+ *
1585
+ * Default: false (keep existing behaviour).
1586
+ */
1587
+ lazyMetadata?: boolean;
1514
1588
  }
1515
1589
 
1516
1590
  export declare type BaichuanSerialPush = {
@@ -3415,6 +3489,8 @@ export declare function createNativeStream(api: ReolinkBaichuanApi, channel: num
3415
3489
  variant?: NativeVideoStreamVariant;
3416
3490
  /** Optional dedicated BaichuanClient for stream isolation. When omitted, uses api.client (shared). */
3417
3491
  client?: BaichuanClient;
3492
+ /** Cancellation signal — aborting wakes the idle sleep and exits the generator promptly. */
3493
+ signal?: AbortSignal;
3418
3494
  }): AsyncGenerator<{
3419
3495
  audio: boolean;
3420
3496
  data: Buffer;
@@ -3525,6 +3601,22 @@ export declare type DebugOptions = {
3525
3601
  };
3526
3602
  };
3527
3603
 
3604
+ /**
3605
+ * Decide whether a new sleep-inference poll should emit an event, given the
3606
+ * previously committed state and any pending hysteresis candidate.
3607
+ *
3608
+ * Rules:
3609
+ * 1. First observation (committed === undefined): always adopt as committed.
3610
+ * Emit "sleeping" but not "awake" — awake is the implicit default, so
3611
+ * emitting on startup would be noisy.
3612
+ * 2. Inferred matches committed: stable, clear any pending candidate.
3613
+ * 3. Inferred differs from committed but matches the pending candidate:
3614
+ * increment count. When count reaches hysteresisPolls, commit and emit.
3615
+ * 4. Inferred differs from committed AND from any pending candidate:
3616
+ * restart the candidate at count=1 (does not emit).
3617
+ */
3618
+ export declare function decideSleepInferenceTransition(input: SleepInferenceInput): SleepInferenceDecision;
3619
+
3528
3620
  /**
3529
3621
  * Determine if H.265 should be transcoded to H.264 based on client capabilities.
3530
3622
  *
@@ -4217,6 +4309,7 @@ export declare class Go2rtcTcpServer extends EventEmitter<{
4217
4309
  private totalFramesReceived;
4218
4310
  private totalVideoFramesWritten;
4219
4311
  private prebuffer;
4312
+ private audioInfo;
4220
4313
  constructor(options: Go2rtcTcpServerOptions);
4221
4314
  /** Start listening. Resolves once the TCP server is bound. */
4222
4315
  start(): Promise<void>;
@@ -4228,18 +4321,45 @@ export declare class Go2rtcTcpServer extends EventEmitter<{
4228
4321
  get go2rtcSourceUrl(): string | undefined;
4229
4322
  /** Number of currently connected clients. */
4230
4323
  get clientCount(): number;
4324
+ /**
4325
+ * Subscribe to the raw native stream for diagnostic purposes.
4326
+ * The subscriber receives the same frames the MPEG-TS muxer consumes
4327
+ * (pre-muxing). Counts as a "consumer" so the native stream is kept alive
4328
+ * for the lifetime of the subscription. If the stream is not already
4329
+ * running (battery camera, prestart=false), this starts it.
4330
+ */
4331
+ subscribeDiagnostic(id: string): Promise<AsyncGenerator<NativeFrame, void, unknown>>;
4332
+ /** Unsubscribe a diagnostic session and release its consumer slot. */
4333
+ unsubscribeDiagnostic(id: string): void;
4334
+ /**
4335
+ * Returns ADTS AAC audio metadata detected from the native stream, or
4336
+ * null if no audio frame has been observed yet (e.g. video-only cameras
4337
+ * or before the first audio packet arrives).
4338
+ */
4339
+ getAudioInfo(): {
4340
+ codec: "aac-adts";
4341
+ sampleRate: number;
4342
+ channels: number;
4343
+ configHex: string;
4344
+ } | null;
4231
4345
  private handleClient;
4232
4346
  private feedClient;
4233
4347
  /**
4234
- * Convert a native frame to wire-ready Annex-B.
4235
- * Audio frames are skipped raw TCP carries only video (Annex-B).
4236
- * go2rtc auto-detects the codec from SPS/PPS/VPS NALUs.
4348
+ * Convert a native video frame to Annex-B.
4349
+ * Returns null for audio frames (handled separately by muxAudio).
4237
4350
  */
4238
- private convertFrame;
4351
+ private convertVideoFrame;
4239
4352
  /** Check if an Annex-B buffer contains a keyframe (IDR for H.264, IRAP for H.265). */
4240
4353
  private isAnnexBKeyframe;
4241
4354
  /** Split Annex-B byte stream into individual NAL units. */
4242
4355
  private static splitAnnexBNals;
4356
+ /** True if `b` starts with an ADTS AAC syncword (0xFFF). */
4357
+ private static isAdtsAacFrame;
4358
+ /**
4359
+ * Parse an ADTS header into {sampleRate, channels, AudioSpecificConfig hex}.
4360
+ * Returns null when the buffer is not a valid ADTS frame.
4361
+ */
4362
+ private static parseAdtsSamplingInfo;
4243
4363
  private startNativeStream;
4244
4364
  private stopNativeStream;
4245
4365
  private startStreamHealthMonitor;
@@ -4780,6 +4900,84 @@ export declare interface MotionEvent {
4780
4900
  source?: "md" | "pir" | "unknown";
4781
4901
  }
4782
4902
 
4903
+ /**
4904
+ * Stateful MPEG-TS muxer. Each instance has its own continuity counters —
4905
+ * create one per output stream (or per client connection for prebuffer replay).
4906
+ */
4907
+ export declare class MpegTsMuxer {
4908
+ private readonly videoStreamType;
4909
+ private readonly includeAudio;
4910
+ private patCc;
4911
+ private pmtCc;
4912
+ private videoCc;
4913
+ private audioCc;
4914
+ private framesSinceTableSend;
4915
+ private tablesSent;
4916
+ constructor(options: MpegTsMuxerOptions);
4917
+ /**
4918
+ * Mux a video frame (Annex-B H.264 or H.265) into MPEG-TS packets.
4919
+ * PAT and PMT are emitted before keyframes and periodically.
4920
+ *
4921
+ * @param annexBData - Annex-B video data (with start codes)
4922
+ * @param ptsUs - Presentation timestamp in microseconds
4923
+ * @param isKeyframe - Whether this is an IDR / IRAP frame
4924
+ */
4925
+ muxVideo(annexBData: Buffer, ptsUs: number, isKeyframe: boolean): Buffer;
4926
+ /**
4927
+ * Mux an audio frame (ADTS AAC) into MPEG-TS packets.
4928
+ * Returns an empty Buffer when includeAudio is false.
4929
+ *
4930
+ * @param adtsData - Raw ADTS AAC frame (starting with 0xFF 0xF1/0xF9 syncword)
4931
+ * @param ptsUs - Presentation timestamp in microseconds
4932
+ */
4933
+ muxAudio(adtsData: Buffer, ptsUs: number): Buffer;
4934
+ /** Reset all continuity counters and table state (e.g. after stream restart). */
4935
+ reset(): void;
4936
+ }
4937
+
4938
+ /**
4939
+ * MPEG-TS Muxer for H.264/H.265 video + ADTS AAC audio.
4940
+ *
4941
+ * Produces 188-byte MPEG-TS packets suitable for feeding to go2rtc via a
4942
+ * plain TCP connection (`tcp://127.0.0.1:{port}`). go2rtc auto-detects the
4943
+ * container format from the 0x47 sync byte and extracts both video and audio.
4944
+ *
4945
+ * Stream layout:
4946
+ * PID 0x0000 — PAT (Program Association Table)
4947
+ * PID 0x1000 — PMT (Program Map Table)
4948
+ * PID 0x0100 — Video elementary stream (H.264 or H.265, Annex-B)
4949
+ * PID 0x0101 — Audio elementary stream (AAC-ADTS, stream type 0x0F)
4950
+ *
4951
+ * Each `MpegTsMuxer` instance is independent: continuity counters are
4952
+ * per-instance so multiple concurrent streams do not corrupt each other.
4953
+ *
4954
+ * Usage:
4955
+ * const muxer = new MpegTsMuxer({ videoType: "H265", includeAudio: true });
4956
+ * const tsBytes = muxer.muxVideo(annexBBuffer, ptsUs, isKeyframe);
4957
+ * const tsBytes = muxer.muxAudio(adtsBuffer, ptsUs);
4958
+ */
4959
+ export declare interface MpegTsMuxerOptions {
4960
+ /** Video codec type. */
4961
+ videoType: "H264" | "H265";
4962
+ /**
4963
+ * Whether to include an audio PID (0x0101) in the PMT.
4964
+ * When true, audio frames muxed via muxAudio() are wrapped in PES packets
4965
+ * on PID 0x0101 (AAC-ADTS, stream type 0x0F).
4966
+ * Default: true.
4967
+ */
4968
+ includeAudio?: boolean;
4969
+ }
4970
+
4971
+ declare type NativeFrame = {
4972
+ audio: boolean;
4973
+ data: Buffer;
4974
+ codec: string | null;
4975
+ sampleRate: number | null;
4976
+ microseconds: number | null;
4977
+ videoType?: "H264" | "H265";
4978
+ isKeyframe?: boolean;
4979
+ };
4980
+
4783
4981
  export declare type NativeVideoStreamVariant = "default" | "autotrack" | "telephoto";
4784
4982
 
4785
4983
  /**
@@ -5218,7 +5416,17 @@ export declare class ReolinkBaichuanApi {
5218
5416
  private statePollingInterval;
5219
5417
  private udpSleepInferenceInterval;
5220
5418
  private readonly udpLastInferredSleepStateByChannel;
5419
+ /**
5420
+ * Per-channel pending sleep-state candidate for hysteresis.
5421
+ * When the inference flips to a new state we require N consecutive polls
5422
+ * of that same state before committing it — this filters out transient
5423
+ * flapping caused by non-waking traffic drifting in/out of the 10 s
5424
+ * getSleepStatus() observation window during stream teardown.
5425
+ */
5426
+ private readonly udpPendingSleepStateByChannel;
5221
5427
  private readonly udpSleepInferenceIntervalMs;
5428
+ /** Consecutive inference polls required to commit a new sleeping/awake state. */
5429
+ private readonly udpSleepInferenceHysteresisPolls;
5222
5430
  private lastMotionState;
5223
5431
  private lastAiState;
5224
5432
  private aiStatePollingDisabled;
@@ -8941,6 +9149,31 @@ export declare interface SirenStatusConfig {
8941
9149
  };
8942
9150
  }
8943
9151
 
9152
+ export declare interface SleepInferenceDecision {
9153
+ /** The event to dispatch, or null if no event should fire this poll. */
9154
+ emit: "sleeping" | "awake" | null;
9155
+ /** New committed state to store (never "unknown"). */
9156
+ nextCommitted: SleepState;
9157
+ /** New pending candidate to store, or undefined to clear it. */
9158
+ nextPending: SleepInferencePending | undefined;
9159
+ }
9160
+
9161
+ export declare interface SleepInferenceInput {
9162
+ /** State returned by the current getSleepStatus() poll — must not be "unknown". */
9163
+ inferred: Exclude<SleepState, "unknown">;
9164
+ /** Previously committed state (undefined = first observation after inference start). */
9165
+ committed: SleepState | undefined;
9166
+ /** Pending candidate state and consecutive match count, if any. */
9167
+ pending: SleepInferencePending | undefined;
9168
+ /** Consecutive polls of the same inferred state required to commit. */
9169
+ hysteresisPolls: number;
9170
+ }
9171
+
9172
+ export declare interface SleepInferencePending {
9173
+ state: SleepState;
9174
+ count: number;
9175
+ }
9176
+
8944
9177
  export declare type SleepState = "awake" | "sleeping" | "unknown";
8945
9178
 
8946
9179
  /**