Expand description
Async discovery and receivers for Tokio and smol.
AsyncReceiver wraps the synchronous NDI receiver API for an async
runtime, with capture_video, capture_audio and capture_metadata
methods for owned media and matching capture_*_with scoped callbacks.
AsyncReceiver::capture_any returns whichever owned media item or receiver
event arrives next. The adapter selects the AsyncRuntime whose blocking
thread pool runs the NDI operations:
Use Tokio with the tokio Cargo feature; the tokio submodule names its adapters.
Use Smol with the smol Cargo feature; the smol submodule names its adapters.
This module exists when either feature is enabled, and each runtime only
with its own feature; the crate root does not re-export any of it.
AsyncFinder owns a crate::Finder or crate::SourceCache for async
selection and complete discovery windows. Its owned AsyncSourceWatch
retains a consumer’s generation across awaits without borrowing a finder.
Runtime submodules also provide the AsyncSourceCache alias for cached
discovery. Dropped lookups stop their workers using the same rule as receivers.
The NDI SDK operations are synchronous and blocking. Tokio runs them through
tokio::task::spawn_blocking; smol uses smol::unblock, keeping the work off
the async runtime’s executor threads. Besides capture, AsyncReceiver has async
versions of the receiver’s other waiting calls:
next_event, which returns the status and
source changes that captures pass over and keep,
wait_for_source_name_change,
connect_to,
disconnect and
reconnect. It lends the Receiver itself,
through receiver, for shared controls. See
Receiver’s waiting contract, including native destruction
on the thread dropping the last owner.
For owned/scoped captures, next_event and wait_for_source_name_change, the timeout
budget starts when the future is first polled, rather than when it is
constructed. The same deadline covers blocking-pool queue delay and SDK waits.
This is a wait budget, not a hard completion deadline: scheduling, frame copying,
and other processing can delay the result beyond it.
AsyncReceiver::capture_video_with and AsyncReceiver::capture_any_with run a
synchronous callback over validated borrowed media on the same blocking
worker that captures it, without wrapper payload allocations or copies.
Only its Send + 'static result crosses the task boundary. The callback
holds an SDK buffer and occupies a worker until it returns; keep processing
bounded. Use owned capture or explicitly copy in the callback to keep media
across awaits. Owned capture copies all active pixels/samples and present
metadata into new allocations (33,177,600 pixel bytes for 4K BGRA).
Dropping a future sets a cancellation flag. Work is skipped if the blocking
closure observes that flag at its entry gate. A budgeted wait also checks
it before each subsequent attempt. An attempt already in flight, lasting at
most 100 ms in the SDK, can still consume media and run its scoped callback
after the future was dropped. Undelivered receiver events and source-name
changes are retained for a later wait, including when a completed worker
was never polled again. Source names and unknown events are redelivered only
if no newer report superseded them. Replay takes a short bookkeeping lock,
never a native-query permit. Callback time and side effects are not cancelled
by the wait timeout either. A panic is caught on the worker, where SDK guards
are released, then resumed on the awaiting task with its original payload.
It unwinds through select! or join! as any other task panic does. If the
future was dropped, its panic payload is discarded with the result.
§Features
tokio- Enable Tokio runtime supportsmol- Enable smol runtime support
§Example with Tokio
use grafton_ndi::{Finder, FinderOptions, Ndi, ReceiverOptions,
asynchronous::tokio::{AsyncFinder, AsyncReceiver}};
#[tokio::main]
async fn main() -> Result<(), grafton_ndi::Error> {
let ndi = Ndi::new()?;
let finder = AsyncFinder::new(Finder::new(&ndi, &FinderOptions::new())?);
let Some(source) = finder.find_source_by_name("CAMERA (Program)",
std::time::Duration::from_secs(5)).await? else { return Ok(()); };
let options = ReceiverOptions::snapshot_preset(source);
let receiver = grafton_ndi::Receiver::new(&ndi, &options)?;
let async_receiver = AsyncReceiver::new(receiver);
if let Some(size) = async_receiver.capture_video_with(std::time::Duration::from_secs(5), |frame| {
(frame.width(), frame.height())
}).await? {
println!("Captured dimensions: {size:?}");
}
Ok(())
}See mixed_media_async.rs for a runnable async discovery and capture loop.
Modules§
- smol
AsyncReceiveron the smol runtime: the genericasynchronous::AsyncReceiverwithSmol, which run blocking work withsmol::unblock.- tokio
AsyncReceiveron the Tokio runtime: the genericasynchronous::AsyncReceiverwithTokio, which run blocking work withtokio::task::spawn_blocking.
Structs§
- Async
Finder - Async discovery through one owned
FinderorSourceCache. - Async
Receiver - An async wrapper over a
Receiverthat runs its blocking NDI operations on the blocking thread pool of the runtimeR, so that they do not block the async runtime. - Async
Source Watch - One owned consumer’s cursor over an
AsyncFinder’s change generations.
Enums§
- Smol
- The smol runtime, which runs blocking work with
smol::unblock. - Tokio
- The Tokio runtime, which runs blocking work with
tokio::task::spawn_blocking.
Traits§
- Async
Runtime - An async runtime whose blocking thread pool runs the NDI operations of an
AsyncReceiver,AsyncFinderandAsyncSourceWatch.Tokioselects the Tokio runtime.Smolselects the smol runtime. - Source
Discovery - Source discovery state an
AsyncFindercan own:FinderorSourceCache.