Skip to main content

CompressedSender

Struct CompressedSender 

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

A sender of compressed video and audio.

Each stream keeps the frame of its last asynchronous send until the next asynchronous send on that stream returns it, or reclaim_video or close reclaims it. The SDK has one asynchronous send outstanding per sender, so a send on either stream may wait for the SDK to finish with the other stream’s frame. The crate still returns each frame through its own stream. Which streams a codec needs, and how they must relate, is defined by the NDI Advanced SDK documentation.

The native sender is owned exclusively by this value. sender lends it in the CompressedAttached mode, which has the operations every sender shares (the source, metadata in both directions, tally, connections, failover, and the Advanced encoder guidance, keyframe and tally controls) but none of the uncompressed sends, and which cannot be registered with a SendAdvertiser.

§Threading

split lends independent video and audio views to scoped threads without an external lock. The exclusive video view is Send but neither Sync nor Clone, so video submissions remain serialized. The shared Send + Sync sender supports compressed audio, metadata and controls while video is being submitted.

§Sending lends the frame

The SDK writes to the frames it sends: it stamps the reserved field of their packet envelope. A synchronous send therefore borrows its frame exclusively and an asynchronous one takes it, so no reference to a frame’s bytes exists while the SDK may change them, and a frame can be sent again afterwards. reclaim_video waits for pending video and returns both streams’ frames while keeping the source available.

ⓘ
use grafton_ndi::advanced::{CompressedSender, CompressedFrame, VideoPacketFormat};

fn send(sender: &mut CompressedSender, frame: &mut CompressedFrame<VideoPacketFormat>) {
    let bytes = frame.data();
    let _ = sender.send_video(frame);
    // The SDK may have written to what `bytes` refers to.
    let _ = bytes.len();
}

§What a receiver gets

The SDK reports nothing about a frame it does not deliver. The frames it would discard for what they are (see VideoCodec and AudioCodec) are refused when they are built or converted. Contiguous sends are infallible; scatter sends can fail only when scatter sending is unavailable. Every other frame is delivered, subject to two rules about the stream it belongs to:

  • A program-tier frame reaches the receivers that asked for the full-quality stream, and a preview-tier frame those that asked for the low-bandwidth one. The SDK documentation asks a source to send both.
  • An H.264 or HEVC stream starts with a keyframe. Until a receiver has had one, the frames that depend on it are not delivered; keyframe_required on sender says when a receiver is waiting.

§Examples

use grafton_ndi::{
    advanced::{
        CompressedFrame, CompressedSender, EnvelopeCodec, PacketEnvelope, VideoCodec,
        VideoPacketFormat,
    },
    FrameRate, Ndi, SenderOptions,
};

let ndi = Ndi::new()?;
let mut sender = CompressedSender::new(&ndi, &SenderOptions::new("Encoder"))?;
let format = VideoPacketFormat::new(VideoCodec::H264, 1920, 1080)?
    .with_frame_rate(FrameRate::new(60, 1)?);

let (payload, parameter_sets) = encoded_access_unit();
let packet =
    PacketEnvelope::encode(EnvelopeCodec::H264, 0, 0, true, &payload, &parameter_sets)?;
let mut frame = CompressedFrame::<VideoPacketFormat>::new(format, packet)?;
sender.send_video(&mut frame);

Implementations§

Source§

impl CompressedSender

Source

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

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

§Errors

As for Sender::new, and Error::UnsupportedCapability without an Advanced runtime (or in a build without the Advanced SDK).

Source

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

The sender this value sends through, for compressed audio and the operations that do not send video: source, send_compressed_audio, send_scatter_audio, send_metadata and capture_metadata, tally, connection_count, connection metadata, failover, and the Advanced controls such as keyframe_required and set_external_tally.

Its mode has none of the uncompressed sends, and it cannot be registered with an advertiser. Use split to reach these controls through the audio half while video is being sent.

Source

pub fn split(&mut self) -> (CompressedVideo<'_>, &Sender<CompressedAttached>)

Lend video and audio operations to separate scoped threads.

Video keeps exclusive access to the pending streams and is Send but neither Sync nor Clone. The shared sender supports compressed audio, metadata and controls and is Send + Sync. Both halves borrow this owner.

Source

pub fn send_video(&mut self, frame: &mut CompressedFrame<VideoPacketFormat>)

Send a video frame and return when the SDK has finished with it.

The frame is borrowed exclusively because the SDK writes to its bytes.

Source

pub fn submit_video( &mut self, frame: CompressedFrame<VideoPacketFormat>, ) -> Option<SubmittedVideo>

Submit a video frame without waiting for it to be sent.

The sender keeps the frame until the next asynchronous send on the same stream, and returns the frame that this one replaces. The call can still block: with SenderOptions::clock_video enabled, the default, the SDK paces video inside the call. The first can return immediately; subsequent calls wait for the next frame time. Set clock_video(false) and pace sends in the application, or use super::Genlock, to avoid this frame-rate wait. SDK back-pressure can also block a call.

Source

pub fn reclaim_video(&mut self) -> ReclaimedVideo

Drain asynchronous video and return both streams’ final frames.

If the SDK still owns a loan, one null asynchronous flush waits for it without a timeout. A preceding synchronous video send already ends both loans and needs no flush. The source stays available and the next asynchronous send on either drained stream returns None. Reclamation does not report whether receivers received the frames.

Source

pub fn send_scatter_video( &mut self, frame: &mut ScatterFrame<VideoPacketFormat>, ) -> Result<()>

Send a video frame held in several blocks and return when the SDK has finished with it.

The frame is borrowed exclusively because the SDK writes to its blocks.

§Errors

Error::UnsupportedCapability when the runtime has no scatter sending.

Source

pub fn submit_scatter_video( &mut self, frame: ScatterFrame<VideoPacketFormat>, ) -> Result<Option<SubmittedVideo>, Rejected<ScatterFrame<VideoPacketFormat>>>

Submit a video frame held in several blocks without waiting for it to be sent; see submit_video.

§Errors

Returns the frame when the runtime has no scatter sending.

Source

pub fn close(self) -> ReclaimedVideo

Destroy the sender and take back the frames it still held.

Destroying the native sender waits for the SDK to finish with every pending frame, without a timeout.

Trait Implementations§

Source§

impl Debug for CompressedSender

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.