rtciv0.5.0

Using rtci-host

rtci-host 0.2.0 gives each Peer (one RTCPeerConnection) its sockets, timers, STUN/TURN clients, ICE-TCP, mDNS and media, and a queue of W3C events, behind a permission gate and a socket factory. It is the layer a front end (a JavaScript runtime's WebRTC, rtci-ffi) builds on.

Install

cargo add [email protected]

rtci-host depends on rtci 0.4 (default features off, crypto-aws-lc-rs on), not on rtci 0.5.0: rtci-host 0.2.0 has no release on rtci 0.5 yet, so it builds the engine at 0.4.x. Its README says: pre-1.0 and not externally audited; tested with rtci 0.4.

The gate and the socket factory

Two traits stand between the engine and the network:

  • NetGate: every address the engine sends to, every IP it binds on and the machine's interface list are asked of it first. There is no allowing default: DenyAllGate refuses everything, and every Peer, Network and IceTransport takes a gate.
  • NetIo: the only place sockets are created and names resolved. PlainNetIo uses the operating system's.
use std::rc::Rc;
use rtci_host::{NetGate, Peer, PeerOptions, PlainNetIo, Reach, Verdict};

struct AllowAll;
impl NetGate for AllowAll {
  fn decide(&self, _: &Reach) -> Option<Verdict> { Some(Verdict::Allow) }
  fn interfaces(&self) -> Option<Verdict> { Some(Verdict::Allow) }
}

let peer = Peer::new(PeerOptions::default(), Rc::new(AllowAll), Rc::new(PlainNetIo))?;
let offer = peer.create_offer(false)?;
while let Some(events) = peer.next_events().await {
  // hand `events` to the application
}

From crates/rtci-host/README.md, “The gate and the socket factory” at v0.2.0.

Writing a gate

The engine asks decide(&reach) first (and interfaces() for the interface list). A Reach says why (kind: IceServer, IceServerResolved, RemoteCandidate, SendTo, TcpDial, Bind, Mdns), the direction (Connect or Listen), the transport, the host, the port and, for IceServerResolved, the name it was resolved from.

  • A synchronous gate (a runtime's permission set) always returns Some(verdict); nothing waits, and set_remote_description / add_ice_candidate finish before they return.
  • An asynchronous gate returns None when it does not know yet. The engine then calls ask(reach) once per question per connection and waits, holding back whatever needed the answer: no socket is bound, no name resolved and no datagram sent before an Allow. An answer that does not come within ask_timeout() (10 s by default), a gate that panics, and a future that ends without an answer all count as refusals.

AskGate is a ready-made asynchronous gate: questions become GateRequests, answered by ticket.

let gate = rtci_host::AskGate::new();
gate.set_notify(|| wake_my_event_loop());
gate.attach();
let peer = Peer::new(PeerOptions::default(), Rc::new(gate.clone()), Rc::new(PlainNetIo))?;
// later, on the engine's thread:
for request in gate.take_requests() {
  let verdict = if allowed(&request) { Verdict::Allow } else { Verdict::Deny("denied".into()) };
  gate.answer(request.ticket, verdict);
}

From crates/rtci-host/README.md, “AskGate: questions as events, answers by ticket” at v0.2.0.

A request serializes as the gaterequest event:

{"type":"gaterequest","ticket":7,"kind":"remoteCandidate","dir":"connect",
 "proto":"udp","host":"192.0.2.1","port":5000,"resolvedFrom":null}

From crates/rtci-host/README.md, “AskGate: questions as events, answers by ticket” at v0.2.0.

Two-phase operations

Peer::set_remote_description and Peer::add_ice_candidate return an operation id; their Err is only an argument error. The outcome is the opdone event with that id, queued in order after the gate has answered:

{"type":"opdone","op":3}
{"type":"opdone","op":4,"error":{"kind":"NotCapable","message":"..."}}

From crates/rtci-host/README.md, “Two-phase operations: opdone” at v0.2.0.

With a synchronous gate the opdone is queued before the call returns, so a front end has one code path for both kinds of gate.

End of candidates, and one difference from rtci's default

A connection ends its own gathering. The peer's end of candidates is the front end's to pass on (add_ice_candidate with an empty candidate, or a=end-of-candidates). The host also reaches failed when the peer's end-of-candidates never arrives, once the engine's RFC 8863 peer-reflexive wait (39.5 s by default) runs out (with_ice_failed_without_end_of_candidates(true)): that is what Chrome, Safari and Firefox do, and on rtci's default such a connection would stay disconnected.

Threading

The engine is single-threaded (Rc / RefCell) and spawns !Send tasks on the current thread. Run it on a current-thread tokio runtime (as Deno does), or inside a tokio::task::LocalSet.

Errors

Calls fail with rtci_host::Error { kind, message }; ErrorKind names the W3C exception (TypeError, InvalidStateError, ..., NotCapable for a gate refusal). Each front end maps it to its own error classes.

Bundled media

src/fake_media/*.bin (about 234 KB) are the clips the fake capture devices send, generated from FFmpeg's synthetic sources (a test card and sine tones) with no third-party footage or audio. The crate only replays these short bitstreams to a peer in tests and fake capture; it contains no codec implementation, and no patent licence is granted for decoding or encoding H.264 or H.265.

Reference

The whole README, and every public item: rtci-host API. The changelog: rtci-host releases.