Skip to main content

Config

Struct Config 

Source
#[non_exhaustive]
pub struct Config {
Show 13 fields pub width: u32, pub height: u32, pub framerate: FrameRate, pub bitrate: Option<Rate>, pub join: Duration, pub codec: Codec, pub backend: Backend, pub preset: Preset, pub qp_floor: Option<u8>, pub color: Option<Color>, pub resize: Option<Config>, pub input: Option<Device>, pub device: Device,
}
Expand description

Encoder configuration. width / height / framerate are the encoded output; external Vulkan frames are scaled by the backend on their GPU.

#[non_exhaustive]: build via Config::new and set the optional fields, so future knobs don’t break callers.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§width: u32

Encoded width in pixels.

§height: u32

Encoded height in pixels.

§framerate: FrameRate

Encoded frame rate.

§bitrate: Option<Rate>

The rendition’s cap: the highest bitrate the encoder is configured for. None derives resolved_bitrate’s default from the size, frame rate, and codec.

§join: Duration

The time between join points: a keyframe (an IDR with its parameter sets in band) opens a group this often, so a subscriber joining mid-stream waits at most this long for a picture it can decode. Encoder::cut opens one sooner. Rounded to whole frames at framerate, never below one; see effective_join. Measured by the frames’ timestamps on a backend that can cut, so a source running slower than framerate still opens a group this often, overshooting by less than one of its frames. Encoder::new refuses zero. Defaults to two seconds.

§codec: Codec

Output codec. Defaults to Codec::H264.

§backend: Backend

Which encoder implementation to use. Defaults to Backend::Auto.

§preset: Preset

How the encoder trades latency for compression. Defaults to Preset::LowLatency.

§qp_floor: Option<u8>

The finest quantizer (0-51, lower is finer) the rate control may pick. None (the default) keeps the backend’s own floor, which keeps quality constant under the cap, so a calm scene spends less than it rather than buying detail no one sees: 22 for openh264, 25 (H.264) or 23 (H.265) for VideoToolbox.

Lower it to let the rate control code a picture finer when the cap allows, or set Some(0) for no floor beyond the codec’s own (openh264 never codes finer than 12). What that costs depends on the content: a detailed calm scene spends more of the cap, while very simple content can come out smaller and cheaper to encode and decode. Values above 51 act as 51. Applied’s controls name the floor that took effect.

openh264 and VideoToolbox read it; VideoToolbox before macOS 13 and iOS 16 has no floor at all. The other backends keep their own quality controls, tuned per device.

§color: Option<Color>

The color space of the input frames, written into the bitstream’s VUI so a decoder doesn’t have to guess. None uses Color::infer, which is both what the crate’s own RGB conversions produce and what a player falls back to, so leaving it unset keeps the pixels and the label in agreement.

Set it to the first frame’s Surface::color when that is known: a decoded or resized frame keeps its source’s space, which need not be the one this size infers. Encoder::encode refuses a frame labelled differently with Error::ColorMismatch rather than mislabel it.

§resize: Option<Config>

Scale frames that arrive at another size to the encoded size, rather than refusing them. None (the default) refuses them.

The scale runs wherever the encoder does, which for a Sink is its own thread: a caller fanning one decoded picture out to several encoders (a transcode ladder) hands each the native frame and keeps the per-encoder pixel work off its own threads. On Apple platforms a Sink encodes inline, so the scale runs on the caller there. External Vulkan inputs the backend scales on the GPU are left to it.

A scaled frame keeps its source’s space, and an unlabelled one is labelled with what Color::infer guesses for its original size. Set color to match, or a frame scaled across 576 lines is refused with Error::ColorMismatch.

§input: Option<Device>

External Vulkan input device, required before opening a GPU-only encoder. None retains selection for CPU and native decoder/capture surfaces.

§device: Device

The hardware the encoder opens. Device::Auto (the default) picks one: VA-API the first render node that encodes, decodes, and scales, so a DMA-BUF moves between them without a copy; NVENC the first CUDA device, or the one behind input; V4L2 the first node that encodes H.264, which a driver whose format enumeration is wrong can defeat.

A render node an NVIDIA driver runs that is not the device behind input is refused.

Implementations§

Source§

impl Config

Source

pub fn new(width: u32, height: u32, framerate: FrameRate) -> Self

A config encoding width x height at framerate, with the default codec, join, and bitrate.

Source

pub fn size(&self) -> Size

The encoded resolution.

Source

pub fn effective_join(&self) -> Duration

The time between join points the encoder holds to: join rounded to whole frames at framerate, the value a catalog declares.

Source

pub async fn probe(&self) -> Result<VideoConfig, Error>

The catalog rendition an encoder for this config produces.

Resolved by opening a throwaway encoder and encoding one frame, so the profile, level, and constraints are the ones this machine writes into the bitstream rather than a guess. The backend picks all three itself (VideoToolbox asks for High, openh264 leaves it at Baseline, VAAPI writes Main) and its choice depends on the config it opened with, so reading them back is the only way to know.

This is what lets a track be advertised before it carries anything: a subscriber can only discover a track the catalog names, and an on-demand encoder only encodes once a subscriber arrives. Publishing the probed rendition breaks that cycle without publishing a claim that has to be corrected later, since it already says what the first keyframe will say.

Costs an encoder open and a single frame, so call it once per track. Call it before opening the encoder you will actually use, so the two never hold a codec session at once.

§Errors

Fails when this machine cannot encode the config at all, which makes it a fail-fast check: better here than on the first frame of a track that is already advertised.

Source

pub fn resolved_bitrate(&self) -> Rate

The rendition’s cap: bitrate, or the default for its size, frame rate, and codec.

The default is 7.2 Mbps x Mpx / (1 + Mpx / 1.38), which levels off toward 10 Mbps, for H.264 at 30 fps: 720p30 is 4 Mbps and 1080p30 is 6 Mbps. Frame rate scales it by (30 + (fps - 30) / 2) / 30, since delta frames make it less than linear, and the codec by its efficiency against H.264 (H.265 is 0.7). The JS publisher derives the same default.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more