Skip to main content

Crate o_sfu_router

Crate o_sfu_router 

Source
Expand description

Sans-IO room session placement and RTP capability negotiation.

Alice, Bob and Carol join a call. Callers commit every connection to router placements with Router, which resolves the media worker that runs it. Room state outside this crate records publishers and receivers.

When Alice publishes camera video, the negotiation module derives RTP parameters for her publication, then matches them separately against the receiver capabilities of Bob and Carol. Every successful match returns compatible RTP parameters as MediaStream.

Both APIs are synchronous and perform no I/O. Callers supply identifiers, choose media workers and set up packet forwarding from the returned workers and RTP parameters. Transport cleanup when removing sessions is up to callers too.

    connections and               MediaStream and
    router placements             MediaCapabilities
             |                          |
             v                          v
+--------------------- o-sfu-router ---------------------+
|            Router                 negotiation          |
|       session placement         RTP compatibility      |
+------------|--------------------------|----------------+
             v                          v
       media workers                MediaStream

§Placements

One Router manages room placements across multiple logical RouterIds. RouterPlacement maps logical routers to MediaWorkerId values. Worker selection and load measurements belong to callers. Every admitted user has one current ConnectionId and home placement.

§Use the placement API

Start with Router::with_placements when worker assignments are known. Router::new accepts only one primary router ID and RTP capabilities, so the first session commit must assign that primary router to some worker. Later commits can add placements but cannot change worker assignments of existing routers.

OperationUse
Router::commit_session_placementRegister user connections and placements. Committing another connection for the same user replaces the previous session.
Router::committed_media_worker_idResolve the worker of a connection while it remains current for that user.
Router::retire_committed_placementRemove sessions only while their connection is current. Returns None for obsolete connections.
Router::remove_sessionRemove the current session by user ID.
Router::placement_snapshot, Router::primary_workerRead assigned placements for caller-side worker selection.
Router::rtp_capabilitiesRead the capability baseline supplied at construction for RTP negotiation.

Mutations fail with RouterError for duplicate connections, missing sessions or inconsistent placement assignments. IDs come from callers. Use fresh ConnectionId values for every reconnect.

For delayed disconnect handling, pass the original connection ID to Router::retire_committed_placement. Router::remove_session targets whichever connection is current. These operations do not close transport sessions or return transport cleanup plans, so callers must save what cleanup needs before removing sessions.

§A receiver reconnects

Obsolete connections cannot resolve or retire the replacement session:

use o_sfu_model::UserId;
use o_sfu_router::{
    ConnectionId, MediaWorkerId, Router, RouterError, RouterId,
    rtp::MediaCapabilities,
    topology::{RouterPlacement, RouterPlacements},
};

let source = RouterPlacement {
    router: RouterId(1),
    media_worker: MediaWorkerId::from_raw(0),
};
let destination = RouterPlacement {
    router: RouterId(2),
    media_worker: MediaWorkerId::from_raw(1),
};
let mut router = Router::with_placements(
    RouterPlacements::new(source, vec![destination]),
    MediaCapabilities::default(),
);
let publisher = UserId::from(1_i64);
let receiver = UserId::from(2_i64);
let old_connection = ConnectionId::from_raw(20);
router.commit_session_placement(&publisher, ConnectionId::from_raw(10), source)?;
router.commit_session_placement(&receiver, old_connection, destination)?;
assert_eq!(
    router.committed_media_worker_id(&receiver, old_connection),
    Some(destination.media_worker),
);

let new_connection = ConnectionId::from_raw(21);
router.commit_session_placement(&receiver, new_connection, destination)?;
assert_eq!(router.committed_media_worker_id(&receiver, old_connection), None);
assert_eq!(router.retire_committed_placement(&receiver, old_connection), None);
assert_eq!(
    router.retire_committed_placement(&receiver, new_connection),
    Some(destination.media_worker),
);

The empty capabilities above are enough for placement operations. Media forwarding also requires negotiated RTP parameters and transport setup.

§Negotiate RTP parameters

rtp::MediaCapabilities describes supported codecs and header extensions. rtp::MediaStream describes concrete formats, payload types and stream bindings. Convert transport negotiation results into these types before calling the matching APIs:

  1. derive_consumable_rtp_parameters matches producer formats against router capabilities and maps payload types into the router’s forwarding model. Keep this consumable stream with the publication.
  2. negotiate_consumer_rtp_parameters selects compatible formats and feedback for each receiver. Pass the consumable stream from step 1 and the receiver’s capabilities. The result preserves consumable payload types.

Negotiation fails with RtpNegotiationError. Producer derivation rejects unsupported media codecs and invalid repair associations. Consumer negotiation rejects receivers with no compatible media codec. See negotiation for an example with payload-type remapping and retransmission formats.

Re-exports§

pub use ids::ConnectionId;
pub use ids::MediaWorkerId;
pub use ids::RouterId;

Modules§

ids
Logical router, worker and connection identifiers.
model 🔒
pure session placements plus the RTP models used at negotiation
negotiation
Match producer media to router capabilities, then to receiver capabilities.
rtp
Codec capabilities and concrete media parameters accepted by negotiation.
topology
Worker assignments used by Router.

Structs§

Router
pure room router for session placements

Enums§

MediaKind
Technical media kind shared by RTP, SDP, and signaling metadata.
RouterError
router mutation errors