Stream Ingestion Pipeline Architecture
The Stream Ingestion Pipeline (internal/stream/) is responsible for establishing resilient connections to IP cameras, handling network disruptions, demuxing incoming RTP packets, extracting codec parameter sets, and pushing structured video frames into the Ring Buffer.
1. Connection Lifecycle & Dial Semaphore
When dozens or hundreds of cameras reboot simultaneously (e.g. after a site power restoration), uncoordinated connection attempts could overwhelm both the server network stack and the camera CPUs.
RUSEON Core implements strict connection governance:
dialSemaphore: A bounded token bucket limiting concurrent in-flight RTSP handshakes to a maximum of 10 simultaneous dials.- Exponential Backoff with Jitter: If a camera fails to respond, reconnect intervals increase dynamically ($1\text{s} \to 2\text{s} \to 4\text{s} \to \dots \to 30\text{s}$) with randomized jitter to prevent thundering herd collisions.
2. RTP Depacketization & NAL Extraction
IP cameras transmit video split across thousands of RTP packets per second:
- RFC 6184 (H.264) & RFC 7798 (H.265) Depacketizer:
- Reassembles fragmented FU-A (Fragmentation Units) into continuous NAL units.
- Handles single NAL unit packets and STAP-A aggregation packets.
- Annex B Start Code Normalization:
- Prepends standard 4-byte Annex B start codes (
0x00 0x00 0x00 0x01).
- Prepends standard 4-byte Annex B start codes (
- Timestamp Normalization:
- Converts 90kHz RTP clock timestamps into monotonically increasing microsecond Presentation Timestamps (PTS) to compensate for camera clock drift.
3. Codec Parameter Extraction & Caching
Whenever the NAL parser detects Sequence Parameter Sets (SPS) or Picture Parameter Sets (PPS):
- It extracts video width, height, profile, level, and aspect ratio.
- It updates the stream's
CodecParamsatomically. - This ensures any newly created HLS muxer or WebRTC session immediately possesses valid initialization metadata.