Skip to main content

PacketReceiver

Struct PacketReceiver 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for PacketReceiver

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.