Skip to content

Stream engine design ​

The node has one job: open each channel once, keep it alive, and hand the same bytes to many viewers over TS, HLS and relays. This page explains the main decisions. The full notes, with measurements, are in core/internal/node/DESIGN.md.

Copy mode only ​

ffmpeg always runs with -c copy and a single output: MPEG-TS on a pipe to the node. The node never re-encodes. A copy costs very little CPU, so a node is limited by memory and network, not by cores.

ffmpeg flags differ between 4.4 and today's builds (for example -stimeout became -timeout for RTSP in 5.0). The node does not keep a table of versions. On startup it asks each ffmpeg binary what it supports (-h full, -protocols) and builds flags from that. It works with 4.4, 5.1, 6.1, 7.1, 8.1 and git master, and a channel can be pinned to one build.

ffmpeg is never run through a shell. Extra flags come only from an allowlist and only from admins. The node always sets -protocol_whitelist.

LLOD: no ffmpeg at all ​

For HTTP or HTTPS MPEG-TS sources, LLOD mode skips ffmpeg. A Go reader takes the bytes from the source and writes them straight into the same ring buffer. It opens faster, uses almost no CPU, and keeps provider credentials out of the process list (with ffmpeg they show up in /proc/<pid>/cmdline).

The ring buffer ​

Every input feeds one ring buffer per channel. Every viewer reads from it.

  • Fixed memory. The size is seconds of measured bitrate (6 seconds by default), with a cap per channel (16 MB) and a cap per node (2048 MB). Over the node cap, all buffers shrink a little.
  • No copy per viewer. The buffer is made of immutable chunks. Readers keep a position. The writer never waits for anyone.
  • Clean starts. The node realigns on 188 byte TS packets, keeps the latest PAT/PMT and marks keyframes (from the TS random access flag, or by looking for H.264 and HEVC IDR frames when the source does not set it). A new viewer starts at the latest keyframe with fresh PAT/PMT. Radio starts at any audio packet.
  • Slow viewers are dropped. A viewer that falls behind the oldest chunk is disconnected. Keeping it would make memory unbounded, and it would only see a frozen picture anyway.

HLS built in Go ​

XC_VM uses ffmpeg's tee muxer to write HLS files and a TS pipe at the same time. Bitstream does not. The node cuts HLS segments in memory from the ring buffer, at keyframes. Why:

  • One path for every input. ffmpeg, LLOD and relays all get HLS the same way.
  • The sequence never resets. ffmpeg's HLS muxer starts at 0 each time it restarts, which breaks players. The Go segmenter keeps counting across source switches and restarts and adds #EXT-X-DISCONTINUITY.
  • No silent half failures. With tee, a dead pipe can leave HLS running while TS is gone, and the channel looks healthy.
  • No tmpfs files. Segments live in RAM, and only while someone watched HLS in the last 60 seconds.

The cost: segment length follows the source GOP. A source with a 10 second GOP gives 10 second segments.

Health and fallback ​

The node watches the TS itself, the same way for ffmpeg and LLOD. It uses the same rules as XC_VM. A run fails when no new video arrives for 60 seconds (15 seconds with fast detection), or when ffmpeg stops or the connection fails. That catches a dead source, a frozen picture and a provider sending empty packets. A late, bursty or slow source is never cut: a player waits through it, and so does the node. A slow source shows a warning instead. ffmpeg reconnects by itself after a short network drop.

On failure the next source starts at once. The better sources are probed in the background, and the node goes back to the main one after it stays healthy for 30 seconds. Each source backs off on its own (1 to 60 seconds, with jitter), and each node opens only a few sources at the same time, so a provider outage does not turn into a restart storm. See fallback sources.

Fast starts for on-demand ​

  • The control pre-warms the node at the same moment it redirects the viewer.
  • ffmpeg probes only 1 MB and 0.5 seconds by default. If audio is missing, the node retries once with a big probe and remembers it.
  • The node does not use -fflags nobuffer, a common "low latency" flag. On ffmpeg 5 and later it adds a full GOP of delay with a small probe, because it drops the packets read while probing.

Measured: ffmpeg's default probe costs 5 to 6 seconds on 5.x and later, and XC_VM pays it on every restart. With Bitstream's flags, ffmpeg 5.x and later start in about one GOP (2 seconds for a 2 second GOP), and 4.4 in about 0.5 seconds.

Server tree ​

A child node pulls /relay/<token>/stream.ts from its parent's ring buffer, the same TS any viewer gets, and builds its own HLS. When the parent dies, the child tries the grandparent and so on up to the root, then the sources if it is allowed to. In tests the switch took 0.8 seconds. See server tree.

Known limits ​

  • PAT/PMT tables larger than one TS packet are not parsed. Those channels still play, but new viewers wait for the next PAT/PMT in the stream.
  • LLOD does not set the TS discontinuity flag at a source switch. The players we tested resync on the new PAT/PMT.
  • No timeshift or catch-up, no RTMP output and no transcoding in this version.

Bitstream documentation