#[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
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.width: u32Encoded width in pixels.
height: u32Encoded height in pixels.
framerate: FrameRateEncoded 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: DurationThe 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: CodecOutput codec. Defaults to Codec::H264.
backend: BackendWhich encoder implementation to use. Defaults to Backend::Auto.
preset: PresetHow 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: DeviceThe 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
impl Config
Sourcepub fn new(width: u32, height: u32, framerate: FrameRate) -> Self
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.
Sourcepub fn effective_join(&self) -> Duration
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.
Sourcepub async fn probe(&self) -> Result<VideoConfig, Error>
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.
Sourcepub fn resolved_bitrate(&self) -> Rate
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.