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
impl AvSync
Sourcepub fn new(receiver: Receiver) -> Result<Self, Rejected<Receiver>>
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.
Sourcepub fn receiver(&self) -> &Receiver<AvSyncAttached>
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.
Sourcepub fn controls(&mut self) -> ReceiverControls<'_, AvSyncAttached>
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.
Sourcepub fn split(
&mut self,
) -> (AvSyncCapture<'_>, ReceiverControls<'_, AvSyncAttached>)
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.
Sourcepub fn into_receiver(self) -> Receiver
pub fn into_receiver(self) -> Receiver
Stop synchronizing and return the receiver, with its own audio capture again.
Sourcepub fn synchronize(
&self,
video: AvSyncVideo<'_>,
request: AvSyncRequest,
) -> Result<AvSyncOutcome>
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.
Sourcepub fn synchronize_ref(
&self,
video: AvSyncVideo<'_>,
request: AvSyncRequest,
) -> Result<AvSyncOutcomeRef<'_>>
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.
Sourcepub fn queued_audio(&self, request: AvSyncRequest) -> Result<AvSyncOutcome>
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.
Sourcepub fn queued_audio_ref(
&self,
request: AvSyncRequest,
) -> Result<AvSyncOutcomeRef<'_>>
pub fn queued_audio_ref( &self, request: AvSyncRequest, ) -> Result<AvSyncOutcomeRef<'_>>
Borrow queued audio without copying; see Self::queued_audio for
request semantics and errors.