pub struct VideoFrameRef<'rx> { /* private fields */ }Expand description
A zero-copy video frame captured from a Receiver.
Borrows its receiver and frees the SDK buffer when dropped. It can be moved
or shared with scoped worker threads (Send + Sync). Converting with
to_owned copies the active pixels into an owned frame. No pixels are
copied by capture itself. data() returns None when padding separates
active rows or planes; trailing padding after a single active row does not
prevent a contiguous view. Use planes() and each plane’s rows for padded layouts.
See crate::FrameSyncVideoRef for clock-driven capture.
Implementations§
Source§impl<'a> VideoFrameRef<'a>
impl<'a> VideoFrameRef<'a>
Sourcepub fn pixel_format(&self) -> PixelFormat
pub fn pixel_format(&self) -> PixelFormat
Get the pixel format.
It is a supported uncompressed format, validated when the frame was constructed.
Sourcepub fn frame_rate(&self) -> FrameRate
pub fn frame_rate(&self) -> FrameRate
Get the frame rate.
Sourcepub fn picture_aspect_ratio(&self) -> PictureAspectRatio
pub fn picture_aspect_ratio(&self) -> PictureAspectRatio
Get the picture aspect ratio, width over height as displayed; 0.0
means square pixels.
Sourcepub fn scan_type(&self) -> ScanType
pub fn scan_type(&self) -> ScanType
Get the scan type: progressive, interlaced, or a single field.
Sourcepub fn line_stride_bytes(&self) -> usize
pub fn line_stride_bytes(&self) -> usize
Get the validated first-plane line stride in bytes: the distance between the starts of consecutive rows of the first plane.
It is the SDK’s stride, which can include row padding, cached and validated when the frame was captured.
Sourcepub fn metadata(&self) -> Option<&str>
pub fn metadata(&self) -> Option<&str>
Get frame metadata as UTF-8 text, if present.
Empty metadata, Some(""), is distinct from none, None.
Unreadable received text returns None; use
Self::metadata_status to distinguish it from absent text.
Sourcepub fn metadata_status(&self) -> FrameMetadataStatus
pub fn metadata_status(&self) -> FrameMetadataStatus
The metadata’s presence or the reason unreadable received text was withheld. Querying the cached status allocates nothing.
Sourcepub fn color_info(&self) -> Result<Option<ColorInfo>>
pub fn color_info(&self) -> Result<Option<ColorInfo>>
Read the color description in the frame’s ndi_color_info
metadata.
Returns Ok(None) when the frame has no metadata or no such
element, which means BT.709 SDR with unknown primaries. See
ColorInfo for what the element holds and
how the SDK treats it. Image encoding reads the same
description, so a program can check a frame, for an HDR
transfer for example, before it encodes it.
§Errors
Error::InvalidFrame, naming the
attribute and its value, when the element is malformed or
truncated, gives a value outside the SDK’s list, or appears more
than once with different values. Also returned when received
metadata was withheld as FrameMetadataStatus::NotUtf8
or FrameMetadataStatus::Unterminated,
including on an owned copy of that frame.
§Example
match frame.color_info()? {
Some(color) if color.is_hdr() => println!("HDR: {:?}", color.transfer()),
Some(color) => println!("SDR, matrix {:?}", color.matrix()),
None => println!("BT.709 SDR"),
}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.
Plane order follows the pixel format, as for
VideoPlane, whose rows exclude any row
padding.
Sourcepub fn planes(&self) -> VideoPlanes<'_> ⓘ
pub fn planes(&self) -> VideoPlanes<'_> ⓘ
Iterate planes in SDK memory order without allocating or copying.
Sourcepub fn timestamp(&self) -> Option<ReceivedTimestamp>
pub fn timestamp(&self) -> Option<ReceivedTimestamp>
The source-dependent timestamp of the received frame.
It is None when the SDK does not know it.
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 to_owned(&self) -> VideoFrame
pub fn to_owned(&self) -> VideoFrame
Copy this borrowed frame into 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.
Withheld metadata keeps its status without retaining its text.
Self::copy_into makes the same copy into an existing frame,
reusing its allocations.
The copy cannot fail: capture validated everything it relies on, and the packed layout of a valid frame is valid.
Sourcepub fn copy_into(&self, destination: &mut VideoFrame)
pub fn copy_into(&self, destination: &mut VideoFrame)
Copy this borrowed frame into destination, reusing its allocations.
destination ends up exactly as Self::to_owned would have
returned it, whatever it held before: tightly packed active rows,
format, dimensions, frame rate, aspect ratio, scan type, timecode,
timestamp and metadata.
Its pixel buffer is reused when its capacity holds the packed frame and its scan-dependent room, and otherwise replaced by one allocation of exactly that size. Its metadata string is reused when its capacity holds the new text and its terminator; otherwise it grows once. With sufficient pixel and metadata capacity, subsequent copies allocate nothing, including when sources alternate between absent and present metadata. A copy without metadata retains the destination’s text allocation.
The copy makes no SDK call. It runs wherever this reference may be used: a receiver frame can be copied on a scoped worker thread, while a synchronizer frame stays on its capture thread. The destination is an ordinary owned frame either way.
Sourcepub fn sends_in_place(&self) -> bool
pub fn sends_in_place(&self) -> bool
Whether synchronous sending reads this frame directly without a wrapper pixel copy. This cached-layout query allocates nothing and makes no SDK call. Captured padding is never assumed initialized; captures needing row padding or extra SDK tail room use reusable staging.
Sourcepub fn encode_image(&self, options: &ImageOptions) -> Result<Vec<u8>>
pub fn encode_image(&self, options: &ImageOptions) -> Result<Vec<u8>>
Encode the frame as an image, as options describe.
Every uncompressed pixel format encodes, as PNG, JPEG or
lossless or lossy WebP. By default, YUV uses limited range,
repeated chroma and the matrix in readable ndi_color_info,
or BT.709 when untagged. RGB-received frames already hold the
SDK’s BT.709 limited-range conversion, which ignores the tag;
encoding cannot apply its matrix afterward. Tags still decide
HDR refusal and the PNG cICP chunk. Receive YUV when the
tagged matrix or original HDR code values matter. See
ImageOptions for formats, color, HDR,
bit depth, alpha, reduction, cost and memory.
It encodes directly from the borrowed SDK pixels, without first
copying the frame into an owned VideoFrame.
Encoding is CPU work, tens to hundreds of milliseconds for a 1080p frame: async code runs it on a blocking thread with an owned frame.
§Errors
Returns Error::InvalidInput
for unsuitable options, Error::InvalidFrame
for frame or codec refusals, or Error::AllocationFailed for
conversion scratch storage. The complete conditions are in the
ImageOptions error table.
§Example
use grafton_ndi::{image_encoding::{Downscale, ImageFormat, ImageOptions, PngCompression}};
let options = ImageOptions::new(ImageFormat::Png)
.png_compression(PngCompression::Fast)
.downscale(Downscale::Half);
let png = frame.encode_image(&options)?;Sourcepub fn encode_image_to<W: Write>(
&self,
options: &ImageOptions,
out: W,
) -> Result<()>where
Self: Sized,
pub fn encode_image_to<W: Write>(
&self,
options: &ImageOptions,
out: W,
) -> Result<()>where
Self: Sized,
Encode the frame as an image, as options describe, writing it
into out.
It encodes as encode_image does, into
any writer, so a program that takes snapshots on a timer can
reuse one output buffer. A file or socket should be wrapped in
a BufWriter: the codecs write in small
pieces. Both WebP formats buffer their complete encoded image
before writing it; PNG and JPEG write as they encode. Success
means every byte was written and out was flushed.
§Errors
As for encode_image, and
Error::Io when out fails. Every check
of the options and the frame happens before anything is
written; after a later error, out may hold part of an image,
which the caller discards.
§Example
use grafton_ndi::{image_encoding::{ImageFormat, ImageOptions}};
let options = ImageOptions::new(ImageFormat::Jpeg(85));
let mut jpeg = Vec::new();
for _ in 0..3 {
jpeg.clear();
frame.encode_image_to(&options, &mut jpeg)?;
// Publish `jpeg`.
}Sourcepub fn encode_image_data_url(&self, options: &ImageOptions) -> Result<String>
pub fn encode_image_data_url(&self, options: &ImageOptions) -> Result<String>
Encode the frame as an image, as options describe, in a
base64 data URL for HTML or JSON: data:image/png;base64,,
data:image/jpeg;base64, or data:image/webp;base64, and the
image.
The string is allocated once, at its final length.
§Errors
As for encode_image.
§Example
use grafton_ndi::{image_encoding::{Downscale, ImageFormat, ImageOptions}};
let options = ImageOptions::new(ImageFormat::Jpeg(80)).downscale(Downscale::Quarter);
let url = frame.encode_image_data_url(&options)?;
println!("<img src=\"{url}\">");Trait Implementations§
impl AvSyncFrame for VideoFrameRef<'_>
Source§impl<'a> Debug for VideoFrameRef<'a>
impl<'a> Debug for VideoFrameRef<'a>
Source§impl<'a> VideoSource for VideoFrameRef<'a>
impl<'a> VideoSource for VideoFrameRef<'a>
Source§fn pixel_format(&self) -> PixelFormat
fn pixel_format(&self) -> PixelFormat
Source§fn frame_rate(&self) -> FrameRate
fn frame_rate(&self) -> FrameRate
Source§fn picture_aspect_ratio(&self) -> PictureAspectRatio
fn picture_aspect_ratio(&self) -> PictureAspectRatio
Source§fn line_stride_bytes(&self) -> usize
fn line_stride_bytes(&self) -> usize
Source§fn metadata(&self) -> Option<&str>
fn metadata(&self) -> Option<&str>
Self::metadata_status.Source§fn metadata_status(&self) -> FrameMetadataStatus
fn metadata_status(&self) -> FrameMetadataStatus
Source§fn color_info(&self) -> Result<Option<ColorInfo>>
fn color_info(&self) -> Result<Option<ColorInfo>>
ndi_color_info description without copying metadata. Read moreSource§fn plane(&self, index: usize) -> Option<VideoPlane<'_>>
fn plane(&self, index: usize) -> Option<VideoPlane<'_>>
Source§fn planes(&self) -> VideoPlanes<'_> ⓘ
fn planes(&self) -> VideoPlanes<'_> ⓘ
Source§fn timestamp(&self) -> Option<ReceivedTimestamp>
fn timestamp(&self) -> Option<ReceivedTimestamp>
Source§fn data(&self) -> Option<&[u8]>
fn data(&self) -> Option<&[u8]>
None when row/plane padding separates them.
Padding and SDK trailing read room are never exposed through this slice.Source§fn to_owned(&self) -> VideoFrame
fn to_owned(&self) -> VideoFrame
Source§fn copy_into(&self, destination: &mut VideoFrame)
fn copy_into(&self, destination: &mut VideoFrame)
copy_into. A copy
without metadata retains the destination’s text allocation.