import type { Nullable, ResolvedSiteProfile } from "../types/index.js"; import { EventEmitter } from "node:events"; import type { FFmpegProcess } from "../utils/index.js"; import type { FMP4SegmenterResult } from "./fmp4Segmenter.js"; import type { Page } from "puppeteer-core"; import type { Readable } from "node:stream"; import type { RecoveryMetrics } from "./recovery.js"; /** * HLS segment and playlist storage for a stream. This includes the fMP4 initialization segment (codec configuration), media segments (.m4s files), and the current * playlist content. The playlistReady promise allows callers to wait for the first playlist to be generated. * * Note: HLSState is co-located with the registry because it is part of StreamRegistryEntry. Moving it to hlsSegments.ts would create a circular dependency since * hlsSegments.ts imports getStream from registry.ts. */ export interface HLSState { initSegment: Nullable; initSegmentReady: Promise; playlist: string; playlistReady: Promise; segmentEmitter: EventEmitter; segments: Map; signalInitSegmentReady: () => void; signalPlaylistReady: () => void; } /** * Stream-specific information for idle detection. */ export interface StreamInfo { lastPlaylistRequest: number; storeKey: string; } /** * Registry entry for an active stream. This is the single source of truth for all stream data, including browser state, HLS segments, and the segmenter reference. */ export interface StreamRegistryEntry { channelName: Nullable; clientAddress: Nullable; ffmpegProcess: Nullable; hls: HLSState; id: number; mpegTsClientCount: number; info: StreamInfo; page: Page; profile: ResolvedSiteProfile; rawCaptureStream: Nullable; segmenter: Nullable; startTime: Date; stopMonitor: Nullable<() => RecoveryMetrics>; streamIdStr: string; url: string; } /** * Gets the next unique stream ID by incrementing the counter. Each call returns a new, higher ID that has never been used before in this process lifetime. * @returns The next unique stream ID. */ export declare function getNextStreamId(): number; /** * Registers a stream in the registry. This should be called after stream setup is complete and the stream is ready to serve data. * @param entry - The stream registry entry to add. */ export declare function registerStream(entry: StreamRegistryEntry): void; /** * Unregisters a stream from the registry. This should be called during stream cleanup to remove the stream from tracking. * @param id - The numeric stream ID to remove. */ export declare function unregisterStream(id: number): void; /** * Gets a stream entry by its ID. * @param id - The numeric stream ID to look up. * @returns The stream entry if found, undefined otherwise. */ export declare function getStream(id: number): StreamRegistryEntry | undefined; /** * Gets all stream entries in the registry. * @returns Array of all stream registry entries. */ export declare function getAllStreams(): StreamRegistryEntry[]; /** * Gets the total number of streams in the registry. * @returns The number of active streams. */ export declare function getStreamCount(): number; /** * Updates the last playlist request timestamp for a stream. This should be called whenever a playlist or segment is requested to keep the idle timeout accurate. * @param id - The numeric stream ID. */ export declare function updateLastAccess(id: number): void; /** * Creates the initial HLS state for a new stream. This sets up empty segment storage and the playlist readiness signaling mechanism. * @returns A new HLSState object ready to receive segments. */ export declare function createHLSState(): HLSState; /** * Memory usage breakdown for a stream's HLS segment storage. */ export interface StreamMemoryUsage { initSegment: number; segments: number; total: number; } /** * Calculates the memory usage for a single stream's HLS segment storage. This measures the Buffer sizes of the init segment and all media segments currently retained * in memory. * @param entry - The stream registry entry to measure. * @returns Memory usage breakdown in bytes. */ export declare function getStreamMemoryUsage(entry: StreamRegistryEntry): StreamMemoryUsage; /** * Calculates the total segment memory usage across all active streams. This is useful for monitoring overall memory consumption by HLS buffers. * @returns Total memory usage in bytes across all streams. */ export declare function getTotalSegmentMemory(): number; /** * Gets the size in bytes of the last segment stored for a stream. Used by the monitor to detect dead capture pipelines that produce empty segments (18 bytes observed) * while the video element appears healthy. * @param entry - The stream registry entry to query. * @returns Segment size in bytes, or null if no segmenter exists. */ export declare function getLastSegmentSize(entry: StreamRegistryEntry): Nullable;