Skip to main content

Engine

Struct Engine 

Source
pub struct Engine { /* private fields */ }
Expand description

The engine. Many instances may coexist in one process (ideal for tests). Cheap to Arc-share.

Implementations§

Source§

impl Engine

Source

pub async fn serve( self: &Arc<Self>, handlers: Vec<Box<dyn InboundProtocol>>, shutdown: CancellationToken, ) -> Result<()>

Drive the engine: run every protocol handler concurrently until shutdown fires, then let them observe the cancellation and wind down.

Coordinated teardown: when any handler returns — cleanly, with an error, or by panicking — the shared shutdown token is cancelled so the remaining handlers wind down too. A single listener dying never leaves its siblings orphaned.

Returns the first fatal handler error, if any; otherwise Ok(()) once all handlers have returned.

Accepts any InboundProtocol worker — the bundled RtmpHandler, a legacy ProtocolHandler (bridged automatically), or your own custom protocol. Prefer EngineBuilder::protocol + serve_registered for the builder-driven path.

engine.serve(vec![h], CancellationToken::new()).await
Source

pub async fn serve_until_signal( self: &Arc<Self>, handlers: Vec<Box<dyn InboundProtocol>>, ) -> Result<()>

Like serve, but owns the shutdown trigger: it runs until Ctrl-C (SIGINT) or, on Unix, SIGTERM — the two signals an orchestrator (systemd, Kubernetes) uses to stop a process — then cancels the handlers and waits for them to drain.

This is the batteries-included entry point for a binary; use serve when the host already owns a cancellation token (e.g. to compose the engine into a larger shutdown graph).

engine.serve_until_signal(vec![h]).await
Source

pub async fn serve_registered( self: &Arc<Self>, shutdown: CancellationToken, ) -> Result<()>

Run the protocol workers registered on the EngineBuilder via protocol, until shutdown fires.

This is the builder-driven counterpart to serve: register workers fluently, keep the Arc<Engine> for your own use (subscribing, packaging), then drive them. The registered workers are consumed on the first call; a second call serves an empty set.

let engine = Engine::builder()
    .application(AppSpec::new("live"))
    .protocol(RtmpHandler::new("0.0.0.0:1935".parse().unwrap()))
    .build();
engine.serve_registered(CancellationToken::new()).await
Source

pub async fn serve_registered_until_signal(self: &Arc<Self>) -> Result<()>

Like serve_registered but owns the shutdown trigger, running the builder-registered workers until Ctrl-C/SIGTERM.

Source

pub async fn pump_source<S: MediaSource>( self: &Arc<Self>, key: &StreamKey, source: S, shutdown: CancellationToken, ) -> Result<()>

Drive a MediaSource into the bus: claim key, pump every frame the source yields, and end the publish session when the source is exhausted, errors, or shutdown fires.

This is the first-class runner for the MediaSource trait — the in-process counterpart to a socket-driven ProtocolHandler. Useful for file/loopback ingest, test fixtures, and generated streams.

Source

pub async fn reap_idle(self: &Arc<Self>) -> usize

Reap every publishing stream that has not received a frame within the configured idle_timeout. Returns the number of streams reaped. A no-op when no timeout is configured.

Source

pub fn spawn_idle_reaper( self: &Arc<Self>, interval: Duration, shutdown: CancellationToken, )

Spawn a background task that calls reap_idle every interval until shutdown fires. No-op (returns immediately) when no idle timeout is configured.

Source§

impl Engine

Source

pub fn builder() -> EngineBuilder

Start building an engine.

Source

pub fn register_app(&self, spec: AppSpec) -> Result<()>

Register an application after construction (e.g. on config reload).

Rejects a name that is already registered with StreamError::AppAlreadyRegistered rather than silently replacing the live Application — an overwrite would orphan that app’s active streams and leak the engine-wide publisher count that gates StreamError::PublisherLimitReached. To change an app’s settings, drain and remove it first (a future remove_app), then re-register.

Source

pub fn list_apps(&self) -> Vec<AppName>

List registered application names.

Source

pub fn total_stream_count(&self) -> usize

Total active publishers across all applications (single atomic load).

Source

pub async fn start_publish_authorized( &self, key: &StreamKey, creds: &Credentials, ) -> Result<StreamHandle>

Claim a publish slot only if the injected StreamAuthenticator permits creds to publish key. Protocol handlers should call this rather than start_publish directly so the auth policy is enforced uniformly across every transport.

Source

pub async fn open_playback_authorized( &self, key: &StreamKey, creds: &Credentials, ) -> Result<StreamHandle>

Resolve a live stream for playback only if the authenticator permits creds to play key.

Trait Implementations§

Source§

impl Debug for Engine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl EventBus for Engine

Source§

fn subscribe_events(&self, app: &AppName) -> Result<Receiver<StreamEvent>>

Subscribe to the StreamEvent feed for an application.
Source§

impl PlaybackRegistry for Engine

Source§

fn get_stream(&self, key: &StreamKey) -> Result<StreamHandle>

Resolve a live stream handle, or StreamNotFound.
Source§

fn list_streams(&self, app: &AppName) -> Result<Vec<StreamId>>

Enumerate live stream IDs within an application.
Source§

impl PublishRegistry for Engine

Source§

fn start_publish<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 StreamKey, ) -> Pin<Box<dyn Future<Output = Result<StreamHandle>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Claim a stream for publishing. Returns a handle the ingest loop pushes frames into. Fails with StreamAlreadyPublishing on a live duplicate, or PublisherLimitReached at capacity.
Source§

fn end_publish<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 StreamKey, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Release a publish session. Idempotent.

Auto Trait Implementations§

§

impl !Freeze for Engine

§

impl !RefUnwindSafe for Engine

§

impl !UnwindSafe for Engine

§

impl Send for Engine

§

impl Sync for Engine

§

impl Unpin for Engine

§

impl UnsafeUnpin for Engine

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more