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:
VideoFrameRef<'rx>— frames captured from aReceiver(freed viaNDIlib_recv_free_video_v2).FrameSyncVideoRef<'fs>— frames captured from aFrameSync(freed viaNDIlib_framesync_free_video).
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>
impl<'a, S: FrameFree<RawFrame = NDIlib_video_frame_v2_t>> VideoRef<'a, S>
Sourcepub fn pixel_format(&self) -> PixelFormat
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.
Sourcepub fn frame_rate_n(&self) -> i32
pub fn frame_rate_n(&self) -> i32
Get the frame rate numerator.
Sourcepub fn frame_rate_d(&self) -> i32
pub fn frame_rate_d(&self) -> i32
Get the frame rate denominator.
Sourcepub fn picture_aspect_ratio(&self) -> f32
pub fn picture_aspect_ratio(&self) -> f32
Get the picture aspect ratio.
Sourcepub fn scan_type(&self) -> ScanType
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.
Sourcepub fn line_stride_or_size(&self) -> LineStrideOrSize
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.
Sourcepub fn data(&self) -> Option<&[u8]>
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.
Sourcepub fn plane(&self, index: usize) -> Option<VideoPlane<'_>>
pub fn plane(&self, index: usize) -> Option<VideoPlane<'_>>
Get one plane in SDK memory order, or None for an invalid index.
Sourcepub fn planes(&self) -> VideoPlanes<'_> ⓘ
pub fn planes(&self) -> VideoPlanes<'_> ⓘ
Iterate planes in SDK memory order without allocating or copying.
Sourcepub fn encode_png(&self) -> Result<Vec<u8>>
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.
Sourcepub fn encode_jpeg(&self, quality: u8) -> Result<Vec<u8>>
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.
Sourcepub fn encode_data_url(&self, format: ImageFormat) -> Result<String>
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.
Sourcepub fn to_owned(&self) -> Result<VideoFrame>
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.