arcly_stream/bus/mod.rs
1//! The stream bus: the live pub/sub core, plus the trait contracts that
2//! decouple protocol/packager code from any concrete registry.
3//!
4//! `stream-center`'s protocol crates depended directly on the concrete
5//! `ApplicationRegistry`. Here, ingest handlers depend on [`PublishRegistry`],
6//! playback/packaging depends on [`PlaybackRegistry`], and event consumers
7//! depend on [`EventBus`]. The bundled [`Engine`](crate::Engine) is one
8//! implementation; a host can supply its own.
9
10mod application;
11mod events;
12mod handle;
13
14pub use application::Application;
15pub use events::{StreamEvent, StreamEventKind};
16pub(crate) use handle::{mono_ms, now_ms};
17pub use handle::{Qos, StreamHandle, StreamMetadata, StreamState, Subscription};
18
19use crate::{AppName, Result, StreamId, StreamKey};
20use async_trait::async_trait;
21use tokio::sync::broadcast;
22
23/// The publish side of a stream registry: protocol *ingest* handlers depend on
24/// this, never on the concrete [`Engine`](crate::Engine). Implement it to plug
25/// a foreign media bus into existing protocol handlers.
26#[async_trait]
27pub trait PublishRegistry: Send + Sync + 'static {
28 /// Claim a stream for publishing. Returns a handle the ingest loop pushes
29 /// frames into. Fails with [`StreamAlreadyPublishing`] on a live duplicate,
30 /// or [`PublisherLimitReached`] at capacity.
31 ///
32 /// [`StreamAlreadyPublishing`]: crate::StreamError::StreamAlreadyPublishing
33 /// [`PublisherLimitReached`]: crate::StreamError::PublisherLimitReached
34 async fn start_publish(&self, key: &StreamKey) -> Result<StreamHandle>;
35
36 /// Release a publish session. Idempotent.
37 async fn end_publish(&self, key: &StreamKey) -> Result<()>;
38}
39
40/// The playback side: HLS/DASH packagers, WebRTC SFUs, and recorders depend on
41/// this to find a live stream and subscribe to its frames.
42pub trait PlaybackRegistry: Send + Sync + 'static {
43 /// Resolve a live stream handle, or [`StreamNotFound`].
44 ///
45 /// [`StreamNotFound`]: crate::StreamError::StreamNotFound
46 fn get_stream(&self, key: &StreamKey) -> Result<StreamHandle>;
47
48 /// Enumerate live stream IDs within an application.
49 fn list_streams(&self, app: &AppName) -> Result<Vec<StreamId>>;
50}
51
52/// Lifecycle event subscription, decoupled from the concrete registry.
53pub trait EventBus: Send + Sync + 'static {
54 /// Subscribe to the [`StreamEvent`] feed for an application.
55 fn subscribe_events(&self, app: &AppName) -> Result<broadcast::Receiver<StreamEvent>>;
56}