Skip to main content

QueuedSender

Struct QueuedSender 

Source
pub struct QueuedSender { /* private fields */ }
Expand description

A sender that queues video frames and reports when each has been sent.

An ordinary asynchronous send must wait for the previous frame before it can accept the next. This sender instead keeps every submitted frame until the SDK reports it complete, so accepting another frame does not require waiting for an earlier completion, up to the limits in QueueLimits. With SenderOptions::clock_video enabled, the default, the SDK still paces video inside each submission call: the first can return immediately, while later calls wait for the next frame time. Set clock_video(false) and pace submissions in the application, or use super::Genlock, to avoid that frame-rate wait. Native back-pressure can also block a call. No application code runs on an SDK thread: completed frames are collected with try_next_completed or next_completed and can be reused.

It is Send and Sync: it can be moved to, and its read-only methods called from, any thread, while submitting and collecting frames takes it mutably. close destroys the native sender and returns every retained frame with its submission id. Dropping it destroys the sender and frees those frames instead. Both wait, without a timeout, for the SDK to finish with every frame still queued, so run teardown on a blocking thread when using an async executor.

sender lends the native sender in the QueueAttached mode, which has the operations every sender shares (the source, metadata in both directions, tally, connections, failover, and the Advanced controls) and send_audio, but none of the video sends, and which cannot be registered with a SendAdvertiser.

§Examples

use std::time::Duration;
use grafton_ndi::{
    advanced::{QueueLimits, QueuedSender},
    SenderOptions, VideoFrame, Ndi,
};

let ndi = Ndi::new()?;
let mut sender = QueuedSender::new(
    &ndi,
    // Completion-based sends are paced by the application.
    &SenderOptions::new("Queued").clock_video(false),
    &QueueLimits::default().max_frames(4),
)?;
let id = sender.try_submit(VideoFrame::builder().resolution(1920, 1080).build()?)?;
if let Some(done) = sender.next_completed(Duration::from_secs(1))? {
    assert_eq!(done.id(), id);
}

Implementations§

Source§

impl QueuedSender

Source

pub fn new( ndi: &Ndi, options: &SenderOptions, limits: &QueueLimits, ) -> Result<Self>

Create a sender, with the options’ Advanced SDK configuration when they carry one.

§Errors

Error::UnsupportedCapability without an Advanced runtime (or in a build without the Advanced SDK), Error::InvalidInput for limits outside their range, Error::AllocationFailed when completion storage cannot be reserved, and the errors of Sender::new.

Source

pub fn from_sender( sender: Sender, limits: &QueueLimits, ) -> Result<(Self, Option<VideoFrame>), Rejected<Sender>>

Turn an existing sender into a queued one.

The sender’s own asynchronous video send is finished first: the SDK is drained, and the frame the sender was still holding, if any, is returned beside the queue. The conversion is one-way.

§Errors

Returns the sender, unchanged, with Error::UnsupportedCapability without an Advanced runtime, or Error::InvalidInput for limits outside their range or a sender that is registered with an advertiser (a registration keeps the native sender alive after this value is dropped, when the completion state no longer exists). Error::AllocationFailed when completion storage cannot be reserved.

Source

pub fn sender(&self) -> &Sender<QueueAttached>

The sender this queue sends through, for the operations that do not send video: source, send_audio, send_compressed_audio, send_scatter_audio, send_metadata and capture_metadata, tally, connection_count, connection metadata, failover, and the Advanced controls.

Its mode has none of the video sends, which would bypass the queue, and it cannot be registered with an advertiser. Use split to send audio or metadata through it while another thread submits video.

Source

pub fn split(&mut self) -> (QueuedVideo<'_>, &Sender<QueueAttached>)

Lend video submission and completion collection separately from uncompressed or compressed audio, metadata and controls. The halves can be moved to separate scoped threads; dropping them restores access to this owner.

Only the video half submits video, and it is neither Sync nor Clone.

Source

pub fn try_submit( &mut self, frame: impl Into<QueuedVideoFrame>, ) -> Result<SubmissionId, Rejected<QueuedVideoFrame>>

Queue a frame without waiting for an earlier submission to complete.

With SenderOptions::clock_video enabled, the default, the SDK paces video to its frame rate inside this call. The first submission can return immediately; subsequent calls wait for the next frame time. Disable video clocking and pace the application, or use super::Genlock, to avoid this wait. SDK back-pressure can still block.

The queue owns the frame until it is collected: the SDK writes to a compressed frame’s bytes while it sends them. A collected frame can be submitted again. Descriptor storage is reserved at construction, so accepted submission allocates nothing. Pixels, blocks and metadata move without copies or growth; owned uncompressed frames already hold all SDK tail room their layout and scan type need.

§Errors

Returns the frame with Error::InvalidInput when its retained allocation capacity alone exceeds QueueLimits::max_bytes, or with Error::QueueFull when the frames already retained leave no room within QueueLimits (QueueLimit::Frames or QueueLimit::Bytes; collect completed frames and submit it again), with Error::UnexpectedSdkResult when the queue has failed (see completion_protocol_failed), with Error::UnsupportedCapability when the runtime lacks scatter sending or cannot send an uncompressed frame’s pixel format (as Sender::send_video checks).

Source

pub fn try_next_completed(&mut self) -> Option<CompletedVideo>

Take one completed frame, if there is one.

Source

pub fn next_completed( &mut self, timeout: Duration, ) -> Result<Option<CompletedVideo>>

Wait up to timeout for a completed frame. None means none completed in time; the queue is unchanged and can still be used.

§Errors

Error::InvalidInput when timeout exceeds crate::MAX_TIMEOUT, and Error::UnexpectedSdkResult when the queue has failed (see completion_protocol_failed).

Source

pub fn retained_frames(&self) -> usize

The number of frames held: those still being sent and those completed but not yet collected.

Source

pub fn completion_protocol_failed(&self) -> bool

Whether the SDK has reported a completion that matches no retained submission, such as a frame it was never given, or one completed twice before it was submitted again.

The queue identifies a completion by the frame’s data allocation and relies on the SDK reporting exactly one completion per submission, after its last access to the frame. A repeated completion after that allocation is submitted again cannot be distinguished from its new submission’s completion.

The queue then no longer knows which frames the SDK still reads, so it accepts no more submissions; frames already completed can still be collected. Use close to destroy the native sender and take back every frame still retained, including unmatched submissions.

Source

pub fn close(self) -> Vec<CompletedVideo>

Destroy the sender and take back every frame it still holds.

The returned count equals retained_frames immediately before this call. Frames reported complete, including by callbacks during destruction, come first in completion order. Any unmatched frames follow in submission-id order. Each frame keeps its original allocations and submission id, even after a completion-protocol failure.

Native destruction waits without a timeout for the SDK to finish with every pending frame. Run this on a blocking thread when using an async executor. Only the returned collection is allocated.

Trait Implementations§

Source§

impl Debug for QueuedSender

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.