Skip to main content

VideoRef

Struct VideoRef 

Source
pub struct VideoRef<'a, S: FrameFree<RawFrame = NDIlib_video_frame_v2_t>> { /* private fields */ }
Expand description

A zero-copy borrowed video frame, generic over its free strategy S.

This type wraps an RAII Guard that owns the NDI frame buffer lifetime, exposing a safe, zero-copy view of the video data. The frame is automatically freed when dropped, via whichever NDIlib_*_free_video* call the strategy S encodes.

The two public spellings are type aliases over this one core, so every accessor and the Debug impl are written exactly once:

Key characteristics:

  • Zero allocations: References NDI SDK buffers directly
  • Zero copies: No memcpy of pixel data
  • RAII lifetime: Exactly one free per frame, enforced at compile time
  • Receiver refs are Send + Sync; FrameSync refs stay on their capture thread
  • Immutable plane/row views support scoped worker reads from either family

§Lifetime

The lifetime parameter 'a ties this frame to the owner (a Receiver or a FrameSync) that created it. The borrow checker ensures the owner cannot be dropped while any frame references are alive, preventing use-after-free at compile time with zero runtime cost. The underlying NDI buffer is freed when the frame ref is dropped.

§Performance

For a 1920×1080 BGRA frame, this eliminates ~8.3 MB of memcpy compared to the owned VideoFrame. At 60 fps, this saves ~475 MB/s of memory bandwidth.

Implementations§

Source§

impl<'a, S: FrameFree<RawFrame = NDIlib_video_frame_v2_t>> VideoRef<'a, S>

Source

pub fn width(&self) -> i32

Get the frame width in pixels.

Source

pub fn height(&self) -> i32

Get the frame height in pixels.

Source

pub fn pixel_format(&self) -> PixelFormat

Get the pixel format (FourCC code).

This is guaranteed to be a valid, supported format since it’s validated during construction.

Source

pub fn frame_rate_n(&self) -> i32

Get the frame rate numerator.

Source

pub fn frame_rate_d(&self) -> i32

Get the frame rate denominator.

Source

pub fn picture_aspect_ratio(&self) -> f32

Get the picture aspect ratio.

Source

pub fn scan_type(&self) -> ScanType

Get the scan type (progressive, interlaced, etc.).

This is guaranteed to be valid since it is checked during construction.

Source

pub fn timecode(&self) -> i64

Get the timecode in 100 ns units.

Source

pub fn timestamp(&self) -> i64

Get the receive timestamp in 100 ns units since the Unix epoch.

Source

pub fn line_stride_or_size(&self) -> LineStrideOrSize

Get the line stride or data size.

This returns the cached, validated value computed at construction time.

Source

pub fn metadata(&self) -> Option<&str>

Get frame metadata as UTF-8 text, if present.

Source

pub fn data(&self) -> Option<&[u8]>

Borrow all active pixels when contiguous, in SDK plane order.

Returns None if padding separates active rows or planes. Use Self::planes / Self::plane for zero-copy padded access, or Self::to_owned for a tightly packed owned buffer. A single packed row can be borrowed even when its stride includes trailing padding.

Source

pub fn plane(&self, index: usize) -> Option<VideoPlane<'_>>

Get one plane in SDK memory order, or None for an invalid index.

Source

pub fn planes(&self) -> VideoPlanes<'_> ⓘ

Iterate planes in SDK memory order without allocating or copying.

Source

pub fn encode_png(&self) -> Result<Vec<u8>>

Encode PNG directly from the borrowed SDK pixels, without first copying the frame into an owned VideoFrame. Uses the same supported formats and padding handling as VideoFrame::encode_png.

Source

pub fn encode_jpeg(&self, quality: u8) -> Result<Vec<u8>>

Encode JPEG directly from borrowed SDK pixels. Quality must be in 1..=100; see VideoFrame::encode_jpeg for format restrictions.

Source

pub fn encode_data_url(&self, format: ImageFormat) -> Result<String>

Encode borrowed SDK pixels as a PNG or JPEG base64 data URL without an intermediate owned frame. See VideoFrame::encode_data_url.

Source

pub fn to_owned(&self) -> Result<VideoFrame>

Convert this borrowed frame to an owned VideoFrame.

Copies only active rows into one tightly packed pixel allocation and copies metadata separately. The owned stride can be smaller than the SDK stride. Preserves format, timing, aspect ratio, and scan type.

Trait Implementations§

Source§

impl<'a, S: FrameFree<RawFrame = NDIlib_video_frame_v2_t>> Debug for VideoRef<'a, S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, S> !Send for VideoRef<'a, S>

§

impl<'a, S> !Sync for VideoRef<'a, S>

§

impl<'a, S> Freeze for VideoRef<'a, S>
where <S as FrameFree>::Instance: Freeze,

§

impl<'a, S> RefUnwindSafe for VideoRef<'a, S>
where <S as FrameFree>::Instance: RefUnwindSafe,

§

impl<'a, S> Unpin for VideoRef<'a, S>
where <S as FrameFree>::Instance: Unpin,

§

impl<'a, S> UnsafeUnpin for VideoRef<'a, S>
where <S as FrameFree>::Instance: UnsafeUnpin,

§

impl<'a, S> UnwindSafe for VideoRef<'a, S>
where <S as FrameFree>::Instance: UnwindSafe,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.