Skip to main content

Module task

Module task 

Source
Expand description

A PeerDriver as a task of its own, driven through channels (feature peer): what an application with its own threads or tasks uses instead of stepping the driver itself.

task splits a driver into three parts:

  • PeerTask, the loop: PeerTask::spawn runs it on the driver’s runtime, or PeerTask::run is awaited where the application wants it (Runtime::block_on on an OS thread of its own, as reachmydevice’s transport thread does);
  • PeerHandle (cloneable), the commands: PeerHandle::call runs a closure on the driver inside the loop and hands back its result (signalling, data channels, media: the whole RTCPeerConnection API through PeerDriver::pc_mut), and PeerHandle::shutdown ends it in order (PeerDriver::shutdown). Each has a _blocking form for a thread outside any runtime;
  • PeerEvents, what the loop reports: the connection’s events, the driver’s own, and what the connection reads (RTP, RTCP, data channel messages), in order (PeerTaskEvent), with recv (async) and recv_blocking.

The channels are async-channel’s: executor-agnostic, usable from tokio, smol, or a plain thread. Events are unbounded (the loop never waits for the application); an application that stops reading lets them pile up.

let (task, handle, events) = rtci_stream_driver::task::task(driver);
let _join = task.spawn();
let offer = handle.call(|d| {
    let offer = d.pc_mut().create_offer(None)?;
    let now = d.runtime().now();
    d.pc_mut().set_local_description(now, offer.clone())?;
    Ok::<_, rtci::shared::error::Error>(offer)
}).await??;
while let Some(event) = events.recv().await { /* PeerTaskEvent::Connection(...), ... */ }

Structs§

PeerEvents
What a driver run as a task reports, in order.
PeerHandle
The commands of a driver run as a task: cloneable, usable from any thread.
PeerTask
The loop of a driver run as a task (see the module documentation).
TaskEnded
The loop ended: the command was not carried out.

Enums§

PeerTaskEvent
Something the loop reports (PeerEvents).

Functions§

task
Splits driver into its loop, its commands and its events (see the module documentation). The loop waits at most 20 ms per step when nothing happens (PeerTask::with_step_wait).