Skip to main content

AvSync

Struct AvSync 

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

Audio matched to the timestamps of a receiver’s video.

The synchronizer takes over the receiver’s audio, so it owns the receiver: capture video through receiver, then ask synchronize for the audio that belongs to each frame. It matches audio to the frame’s timestamp, so the frame is given as an AvSyncVideo, which only a frame with a timestamp can be; the frame is read as it was captured, without copying it. into_receiver returns the receiver.

The receiver is lent in the AvSyncAttached mode, with its video and metadata capture and every receiver control. Its audio capture and event polling are unavailable; use the capability, source-name and connection queries described on AvSyncAttached. AvSyncOutcome distinguishes no audio stream, no samples matching a video’s time, a changed format and successful output. These outcomes do not replace monitoring progress of the media the application expects; see the reception monitoring guide.

§Threading

An AvSync is Send but not Sync. It can be created on one thread and synchronize and be dropped on another, but it synchronizes one call at a time: the SDK does not document two synchronizations of one synchronizer running at once. The receiver it lends is Sync, so scoped threads can capture video or metadata or reconnect through it while the thread that holds the AvSync synchronizes.

§Examples

use std::time::Duration;
use grafton_ndi::{
    advanced::{AvSync, AvSyncOutcome, AvSyncRequest, AvSyncVideo},
    Receiver, ReceiverOptions, Ndi,
};

let ndi = Ndi::new()?;
let receiver = Receiver::new(&ndi, &ReceiverOptions::disconnected())?;
let sync = AvSync::new(receiver)?;

// The frame stays in the SDK's buffer: nothing is copied.
if let Some(captured) = sync.receiver().capture_video_ref(Duration::from_secs(1))? {
    // The SDK may supply a timestamp for a captured frame.
    if let Some(video) = AvSyncVideo::new(&captured) {
        if let AvSyncOutcome::Audio { frame, .. } =
            sync.synchronize(video, AvSyncRequest::automatic())?
        {
            println!("{} samples belong to this frame", frame.num_samples());
        }
    }
}

Implementations§

Source§

impl AvSync

Source

pub fn new(receiver: Receiver) -> Result<Self, Rejected<Receiver>>

Start synchronizing the audio of receiver.

§Errors

Returns the receiver, untouched, with Error::UnsupportedCapability without an Advanced runtime (or in a build without the Advanced SDK), or Error::NativeOperationFailed when the SDK creates no synchronizer.

Source

pub fn receiver(&self) -> &Receiver<AvSyncAttached>

The receiver this holds, in the AvSyncAttached mode: its video and metadata capture and the controls every receiver has: tally, PTZ, statistics, upstream metadata, keyframe requests, delays, connecting, reconnecting and source-name changes.

Event polling is unavailable because native synchronization can consume status notifications. Query ptz().is_supported(), web_control_url() or Advanced KVM’s is_supported() for capabilities, use wait_for_source_name_change() for the connected source, and is_connected() or connection_stats() for connection state.

A video frame captured through it can be handed to synchronize as it is, through AvSyncVideo. Its own audio capture does not exist in this mode, because the synchronizer takes the audio:

ⓘ
use grafton_ndi::advanced::AvSync;
fn capture(sync: &AvSync) {
    let _ = sync.receiver().capture_audio(std::time::Duration::ZERO);
}

The synchronizer retains this receiver’s native core through its own destruction. controls lends exclusive stream controls and KVM; split keeps synchronization usable while a KVM session holds the receiver borrow.

Source

pub fn controls(&mut self) -> ReceiverControls<'_, AvSyncAttached>

Borrow exclusive receiver controls without permitting receiver replacement. This allocates nothing and makes no SDK call. Use Self::split to synchronize while a KVM session retains the receiver borrow.

Source

pub fn split( &mut self, ) -> (AvSyncCapture<'_>, ReceiverControls<'_, AvSyncAttached>)

Lend synchronization and exclusive receiver controls independently.

Both views borrow this owner without allocating or making SDK calls. A session from the controls lends the receiver for video/metadata capture while the synchronization view continues matching audio. Release held inputs before changing source, and drop both views before recovering the receiver.

Source

pub fn into_receiver(self) -> Receiver

Stop synchronizing and return the receiver, with its own audio capture again.

Source

pub fn synchronize( &self, video: AvSyncVideo<'_>, request: AvSyncRequest, ) -> Result<AvSyncOutcome>

Fetch the audio that belongs to video.

The audio is matched to the frame’s timestamp, which AvSyncVideo guarantees the frame has; queued_audio takes the queued audio without a frame. The frame is read in place: a VideoFrameRef captured through receiver is handed to the SDK as the SDK delivered it, and an owned copy of one as it is, so no pixels are copied. One call makes one native request: a changed format or missing audio is reported, never retried or converted. Returning samples allocates their owned buffer, plus metadata storage when present. Self::synchronize_ref borrows those samples directly and offers allocation-free copying into reusable storage.

Matching uses the video’s timestamp and geometry, independently of whether this runtime can send its pixel format. Owned and borrowed planar 4:4:4 frames therefore follow the same rule on older runtimes.

One call synchronizes at a time, on the thread that holds this AvSync, which is not Sync. Other threads may meanwhile capture video or metadata or use the controls through the receiver it lends. Connection changes may overlap synchronization, as the native overlap tests also exercise for FrameSync.

§Errors

Error::InvalidFrame when the audio the SDK returned fails validation, Error::NativeOperationFailed when the SDK reports an internal error, and Error::UnexpectedSdkResult for an unknown status or a format change to a sample rate or channel count that is not positive.

Source

pub fn synchronize_ref( &self, video: AvSyncVideo<'_>, request: AvSyncRequest, ) -> Result<AvSyncOutcomeRef<'_>>

Borrow matched audio without allocating or copying samples. Timing, statuses and errors are as for Self::synchronize. The returned frame keeps this synchronizer borrowed until it is released.

Source

pub fn queued_audio(&self, request: AvSyncRequest) -> Result<AvSyncOutcome>

Fetch whatever audio is currently queued, matched to no video frame.

With AvSyncRequest::automatic this takes all the queued audio; with a sample count, it requests that many samples. Inspect the returned frame’s sample count: a depleted queue can return a shorter tail even when the SDK does not report samples_adjusted. Otherwise as for synchronize.

§Errors

As for synchronize.

Source

pub fn queued_audio_ref( &self, request: AvSyncRequest, ) -> Result<AvSyncOutcomeRef<'_>>

Borrow queued audio without copying; see Self::queued_audio for request semantics and errors.

Trait Implementations§

Source§

impl Debug for AvSync

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.