pub struct PacketReceiver { /* private fields */ }Expand description
A receiver that delivers compressed packets where it can.
One capture call returns whichever frame or event arrives first, as a
PacketItem from capture_any, or a borrowed
PacketItemRef from capture_any_ref. It cannot feed a
FrameSync, which needs decoded frames.
receiver and controls reach
the Receiver it holds, in the PacketAttached mode, for the common
controls: tally, PTZ, statistics, upstream metadata, keyframe requests,
stream selection, reconnecting and KVM. Its own frame and event capture is
unavailable, because packet capture takes them.
§Threading
A packet receiver is Send and Sync, as the Receiver it holds
is: it can be created on one thread, capture on another and be dropped on a
third. Both capture methods take a shared reference, so packet references
coexist with shared receiver controls and connection changes. The receiver
binds each native capture to one instance, runtime and bounded attempt.
§Examples
use std::time::Duration;
use grafton_ndi::{
advanced::{PacketItem, PacketReceiver, PassthroughPreset, ReceivedVideo},
ReceiverOptions, Source, Ndi, Tally,
};
let ndi = Ndi::new()?;
let mut receiver =
PacketReceiver::new(&ndi, &ReceiverOptions::new(source), PassthroughPreset::Hevc)?;
receiver.receiver().set_tally(Tally::new(true, false));
if let Some(PacketItem::Video(ReceivedVideo::Compressed(packet))) =
receiver.capture_any(Duration::from_secs(1))?
{
println!("{:?}: {} bytes", packet.format().codec(), packet.data().len());
}Implementations§
Source§impl PacketReceiver
impl PacketReceiver
Sourcepub fn new(
ndi: &Ndi,
options: &ReceiverOptions,
preset: PassthroughPreset,
) -> Result<Self>
pub fn new( ndi: &Ndi, options: &ReceiverOptions, preset: PassthroughPreset, ) -> Result<Self>
Create a receiver that passes through the formats preset names,
with the options’ Advanced SDK
configuration, or the SDK’s default one when
they carry none.
The options’ color format is replaced by the preset, and is neither used nor checked against the runtime; fielded video is always allowed, because a compressed stream cannot be deinterlaced.
§Errors
As for Receiver::new, with Error::UnsupportedCapability
without an Advanced runtime (or in a build without the Advanced SDK),
and reported for RuntimeFeature::Passthrough with this preset.
Sourcepub fn receiver(&self) -> &Receiver<PacketAttached>
pub fn receiver(&self) -> &Receiver<PacketAttached>
The receiver this holds, for the common controls: tally, PTZ, statistics, upstream metadata, keyframe requests, delays, reconnecting and source-name changes.
Its own frame and event capture is unavailable in the
PacketAttached mode; capture_any delivers both.
Sourcepub fn controls(&mut self) -> ReceiverControls<'_, PacketAttached>
pub fn controls(&mut self) -> ReceiverControls<'_, PacketAttached>
Borrow exclusive stream controls or start KVM, without permitting
receiver replacement. This allocates nothing and makes no SDK call.
During a KVM session, capture packets through the session’s
receiver() view, with capture_any or
capture_any_ref.
Sourcepub fn capture_any(&self, timeout: Duration) -> Result<Option<PacketItem>>
pub fn capture_any(&self, timeout: Duration) -> Result<Option<PacketItem>>
Wait up to timeout for the next frame or event, or return None
when none arrived in time.
A delivered packet may have been queued; a timeout alone does not
establish disconnection. Use the attached receiver’s per-media
counters as described in the
reception monitoring guide.
Notifications are coalesced ReceiverEvent invalidations, consumed
once. Status changes include the current connection count, as in raw
mixed capture; pending notifications are delivered before waiting.
The timeout is spent as Receiver::capture_any
spends it. The SDK returns early, with neither a frame nor an event,
while a new connection synchronizes; this captures again until
something arrives, and returns None only once the whole timeout is
spent. Duration::ZERO makes one attempt without waiting. Each native
attempt waits at most 100 ms, and connection changes may run concurrently.
§Errors
Error::InvalidInput when timeout exceeds
crate::MAX_TIMEOUT, Error::ConnectionLost when the SDK reports a
lost connection, Error::InvalidFrame when a delivered frame fails
validation, and Error::InvalidUtf8 for standalone metadata that
is not valid UTF-8.
crate::Error::UnexpectedSdkResult reports a negative status
connection count once per publication. Its event stays pending for
recovery while other events and media remain available.
Sourcepub fn capture_any_ref(
&self,
timeout: Duration,
) -> Result<Option<PacketItemRef<'_>>>
pub fn capture_any_ref( &self, timeout: Duration, ) -> Result<Option<PacketItemRef<'_>>>
Borrow the next packet or decoded frame without copying its payload or metadata. The item keeps its native buffer until dropped, including application memory charged to a custom allocator’s limits.
Timeout, event and reconnect semantics are those of Self::capture_any.
Shared receiver controls remain available while an item is borrowed.
§Errors
As for Self::capture_any, including crate::Error::UnexpectedSdkResult
once per failed status publication, whose event remains pending.