arcly_stream/lib.rs
1//! **arcly-stream** — an open-extensible, high-performance live-media streaming
2//! kernel.
3//!
4//! The pub/sub core of a streaming server: lock-free, zero-copy frame fan-out to
5//! an arbitrary number of late-joining subscribers, with instant-start GOP
6//! replay, publish/play authorization, live QoS, a pluggable **multi-protocol
7//! ingestion layer** ([`InboundProtocol`]), and a feature-gated pure-Rust media
8//! plane — all free of any config schema, metrics singleton, or HTTP runtime.
9//! You bring the wire protocols and telemetry by implementing small traits; the
10//! kernel owns the hard, hot-path part.
11//!
12//! ```
13//! use arcly_stream::prelude::*;
14//!
15//! let engine = Engine::builder()
16//! .max_publishers(1000)
17//! .application(AppSpec::new("live").gop_cache(120)) // instant-start
18//! .build();
19//! assert_eq!(engine.list_apps().len(), 1);
20//! ```
21//!
22//! # Architecture
23//!
24//! The crate is layered as a small always-on **kernel**, a feature-gated
25//! **media plane**, and **deferred wire protocols** that exist only as traits:
26//!
27//! ```text
28//! ┌─────────────────────── Core kernel (always on) ───────────────────────┐
29//! │ Engine · StreamHandle (broadcast fan-out, GOP cache, live QoS) │
30//! │ PublishRegistry / PlaybackRegistry / EventBus · Observer · auth · │
31//! │ audit · health · MediaFrame / CodecId · StreamError │
32//! └───────────────────────────────────────────────────────────────────────┘
33//! ▲ implements ▲ feature-gated, pure Rust
34//! │ │
35//! ┌─────┴── Wire protocols & muxers ──┐ ┌───────┴── Media plane ──────────┐
36//! │ rtmp RTMP publish/play (ready) │ │ codec H.264/H.265/AV1/VP9/VVC │
37//! │ mpegts MPEG-TS muxer (ready) │ │ hls Packager + segmenter │
38//! │ ProtocolHandler (SRT/WHIP/RTSP) │ │ record RecordingSink │
39//! │ Transcoder/ClusterRelay (traits) │ │ ingest TCP accept loop │
40//! └───────────────────────────────────┘ └─────────────────────────────────┘
41//! ```
42//!
43//! Two wire-plane components ship operational: the [`rtmp`](crate::protocol::rtmp)
44//! handler (publish + play over real RTMP) and the
45//! [`MpegTsMuxer`](crate::packager::MpegTsMuxer) (player-compatible `.ts` output).
46//! Remaining protocols (SRT/WHIP/RTSP) and ABR/cluster backends are still trait
47//! contracts a host implements.
48//!
49//! ## Design invariants
50//!
51//! - **Zero-copy fan-out** — frames are `bytes::Bytes`; one publish clones an
52//! `Arc<MediaFrame>` pointer to every subscriber, never the payload.
53//! - **No locks on the hot path** — the frame bus is a `tokio::broadcast`
54//! channel; cached CONFIG frames and the GOP head use `ArcSwap` / atomics.
55//! - **No global state** — telemetry ([`Observer`]), authorization
56//! ([`StreamAuthenticator`]), and audit ([`AuditSink`]) are *injected* traits
57//! with no-op/permit-all defaults, never singletons. The engine is
58//! `Send + Sync` and can be instantiated many times per process.
59//! - **Contracts are traits** — protocols depend on [`PublishRegistry`] /
60//! [`PlaybackRegistry`], not on the concrete [`Engine`], so a host can swap in
61//! its own bus.
62//! - **Pluggable ingest** — a new wire protocol is one [`InboundProtocol`]
63//! ([`inbound`]) implementation, registered with
64//! [`EngineBuilder::protocol`](crate::EngineBuilder::protocol). The engine runs
65//! any worker that satisfies the trait; the bundled `RtmpHandler` is just the
66//! first reference implementation.
67//! - **`#![forbid(unsafe_code)]`** — the kernel contains no `unsafe`, enforced
68//! by the compiler. Documentation completeness (`#![deny(missing_docs)]`) and
69//! intra-doc link integrity are compiler-enforced too.
70//!
71//! ## Module layout
72//!
73//! | Module | Responsibility |
74//! |---------------|---------------------------------------------------------------|
75//! | [`frame`] | `MediaFrame`, `CodecId`, `FrameType`, `FrameFlags` |
76//! | [`identity`] | `AppName`, `StreamId`, `StreamKey` (cheap `Arc<str>` ids) |
77//! | [`error`] | `StreamError` and the crate [`Result`] alias |
78//! | [`traits`] | `MediaSource`, `MediaSink`, `ProtocolHandler`, `StorageBackend`, `HwAccelBackend` |
79//! | [`bus`] | `StreamHandle` fan-out (GOP cache, live QoS) + registry contracts |
80//! | [`inbound`] | `InboundProtocol` trait + `IngestContext` / `PublishSession` — the multi-protocol ingestion seam |
81//! | [`engine`] | `Engine`, `EngineBuilder`, `AppSpec` — composition, run loop, source pump, idle reaper |
82//! | [`auth`] | `StreamAuthenticator` publish/play authorization (permit-all default) |
83//! | [`observe`] | `Observer` telemetry hook + `NoopObserver` |
84//! | [`audit`] | `AuditSink` + `AuditPipeline` lifecycle audit trail |
85//! | [`health`] | `HealthCheck` / `HealthRegistry` readiness aggregation |
86//! | [`transcode`] | `Transcoder` / `RenditionSpec` ABR contracts |
87//! | [`cluster`] | `ClusterRelay` origin/edge federation contracts |
88//! | [`testing`] | In-memory helpers for downstream tests |
89//!
90//! ## Cargo features
91//!
92//! | Feature | Effect |
93//! |---|---|
94//! | *(none)* | Pure engine: frame model, bus, traits, auth/audit/health/cluster contracts |
95//! | `codec` | Baseline H.264 NAL/SPS + AAC ADTS parsers ([`codec`]) |
96//! | `codec-h265` / `codec-av1` / `codec-vp9` / `codec-vvc` | Next-gen codec parsers (opt-in, pure Rust) |
97//! | `codecs-all` | Every codec parser at once |
98//! | `storage-fs` | Filesystem [`StorageBackend`] adapter |
99//! | `ingest` | Shared TCP accept loop, keyframe gate, rate limiter, NAL scan |
100//! | `hls` | HLS/LL-HLS packager: `Muxer`/`Packager`, segmenter, playlists ([`packager`]) |
101//! | `mpegts` | Native MPEG-TS `Muxer` (`MpegTsMuxer`) — playable `.ts` segments |
102//! | `fmp4` | Native fragmented-MP4/CMAF `Muxer` ([`Fmp4Muxer`](packager::Fmp4Muxer)) — `ftyp`+`moov` init & `moof`+`mdat` fragments for LL-HLS |
103//! | `rtmp` | Working RTMP publish/play [`ProtocolHandler`](protocol::rtmp::RtmpHandler) |
104//! | `record` | Segment/recording sink over a [`StorageBackend`] ([`record`]) |
105//! | `hls` / `record` | Shared keyframe-boundary [`segment`] timing |
106//! | `metrics` | Prometheus [`Observer`] implementation |
107//! | `auth-token` | Production signed-token [`StreamAuthenticator`] ([`TokenAuthenticator`]) — HMAC-SHA-256, dependency-free |
108//! | `macros` | `#[derive(MediaSink)]`, `#[protocol]` ergonomics |
109//! | `full` | everything |
110//!
111//! Ready-to-use reference implementations ship for the operational traits too:
112//! [`FileAuditSink`] ([`AuditSink`] → file),
113//! [`StandardTelemetry`] (structured-logging [`Observer`]), and the
114//! `metrics`-gated [`PrometheusObserver`](observability::PrometheusObserver).
115
116#![cfg_attr(docsrs, feature(doc_cfg))]
117// The engine contains no `unsafe` — make that a compiler-enforced guarantee so
118// a future change can't quietly introduce it.
119#![forbid(unsafe_code)]
120// Documentation completeness is enforced, not aspirational: every public item
121// carries docs, and intra-doc links must resolve.
122#![deny(missing_docs)]
123#![deny(rustdoc::broken_intra_doc_links)]
124
125pub mod audit;
126pub mod auth;
127pub mod bus;
128pub mod cluster;
129pub mod engine;
130pub mod error;
131pub mod frame;
132pub mod health;
133pub mod identity;
134pub mod inbound;
135pub mod observe;
136pub mod testing;
137pub mod traits;
138pub mod transcode;
139
140// Canonical Annex-B start-code scanner, shared by the codec NAL utilities and
141// the ingest packetizers. Internal; compiled whenever either layer is present.
142#[cfg(any(feature = "_nal", feature = "ingest"))]
143mod bytescan;
144
145// Dependency-free SHA-256 / HMAC-SHA-256 backing the signed-token authenticator.
146#[cfg(feature = "auth-token")]
147mod crypto;
148
149#[cfg(feature = "_codec")]
150#[cfg_attr(docsrs, doc(cfg(feature = "codec")))]
151pub mod codec;
152
153#[cfg(feature = "storage-fs")]
154#[cfg_attr(docsrs, doc(cfg(feature = "storage-fs")))]
155pub mod storage;
156
157#[cfg(feature = "ingest")]
158#[cfg_attr(docsrs, doc(cfg(feature = "ingest")))]
159pub mod protocol;
160
161#[cfg(feature = "hls")]
162#[cfg_attr(docsrs, doc(cfg(feature = "hls")))]
163pub mod packager;
164
165#[cfg(feature = "record")]
166#[cfg_attr(docsrs, doc(cfg(feature = "record")))]
167pub mod record;
168
169#[cfg(any(feature = "hls", feature = "record"))]
170#[cfg_attr(docsrs, doc(cfg(any(feature = "hls", feature = "record"))))]
171pub mod segment;
172
173#[cfg(feature = "metrics")]
174#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
175pub mod observability;
176
177// ── Flat re-exports (match sc-core's surface so migration is mechanical) ────
178pub use audit::{AuditAction, AuditPipeline, AuditRecord, AuditSink, FileAuditSink};
179#[cfg(feature = "auth-token")]
180#[cfg_attr(docsrs, doc(cfg(feature = "auth-token")))]
181pub use auth::TokenAuthenticator;
182pub use auth::{AllowAll, Credentials, StreamAuthenticator};
183pub use bus::{
184 EventBus, PlaybackRegistry, PublishRegistry, Qos, StreamEvent, StreamEventKind, StreamHandle,
185 StreamMetadata, StreamState, Subscription,
186};
187pub use cluster::{ClusterRelay, NodeAddr};
188pub use engine::{AppSpec, Engine, EngineBuilder, EngineConfig};
189pub use error::{ProtocolErrorKind, StreamError};
190pub use frame::{AudioFrame, CodecId, FrameFlags, FrameType, MediaFrame, VideoFrame};
191pub use health::{HealthCheck, HealthRegistry, HealthReport, HealthStatus};
192pub use identity::{AppName, StreamId, StreamKey};
193pub use inbound::{InboundProtocol, IngestContext, PublishSession};
194pub use observe::{NoopObserver, Observer, StandardTelemetry};
195pub use traits::{HwAccelBackend, MediaSink, MediaSource, ProtocolHandler, StorageBackend};
196
197// Re-export `bytes` so downstream crates (and doctests) can name `Bytes` — the
198// payload type of every `MediaFrame` — without pinning a separate version.
199pub use async_trait;
200pub use bytes;
201pub use transcode::{RenditionSpec, Transcoder};
202
203/// The crate-wide fallible result. Mirrors `sc-core::Result`.
204pub type Result<T> = std::result::Result<T, StreamError>;
205
206/// Re-exports the derive macros expand against. Stable surface for codegen so
207/// internal files can move without touching the macro crate.
208#[cfg(feature = "macros")]
209#[doc(hidden)]
210pub mod __macro_support {
211 pub use crate::bus::PublishRegistry;
212 pub use crate::traits::{MediaSink, ProtocolHandler};
213 pub use crate::{MediaFrame, Result, StreamError, StreamKey};
214 pub use async_trait::async_trait;
215 pub use std::sync::Arc;
216 pub use tokio_util::sync::CancellationToken;
217}
218
219/// `use arcly_stream::prelude::*;` — everything a downstream user needs.
220pub mod prelude {
221 pub use crate::audit::{AuditPipeline, AuditRecord, AuditSink, FileAuditSink};
222 #[cfg(feature = "auth-token")]
223 pub use crate::auth::TokenAuthenticator;
224 pub use crate::auth::{AllowAll, Credentials, StreamAuthenticator};
225 pub use crate::bus::{
226 EventBus, PlaybackRegistry, PublishRegistry, Qos, StreamEvent, StreamEventKind,
227 StreamHandle, StreamState, Subscription,
228 };
229 pub use crate::cluster::{ClusterRelay, NodeAddr};
230 pub use crate::engine::{AppSpec, Engine, EngineBuilder, EngineConfig};
231 pub use crate::error::{ProtocolErrorKind, StreamError};
232 pub use crate::frame::{CodecId, FrameFlags, FrameType, MediaFrame};
233 pub use crate::health::{HealthCheck, HealthRegistry, HealthStatus};
234 pub use crate::identity::{AppName, StreamId, StreamKey};
235 pub use crate::inbound::{InboundProtocol, IngestContext, PublishSession};
236 pub use crate::observe::{NoopObserver, Observer, StandardTelemetry};
237 pub use crate::traits::{
238 HwAccelBackend, MediaSink, MediaSource, ProtocolHandler, StorageBackend,
239 };
240 pub use crate::transcode::{RenditionSpec, Transcoder};
241 pub use crate::Result;
242 pub use async_trait::async_trait;
243 pub use tokio_util::sync::CancellationToken;
244
245 #[cfg(feature = "macros")]
246 pub use arcly_stream_macros::{protocol, MediaSink as MediaSinkDerive};
247}