pub struct Encoder { /* private fields */ }Expand description
Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn size(&self) -> Size
pub fn size(&self) -> Size
The resolution this encoder emits, which every frame fed to it must match.
Sourcepub fn bitrate(&self) -> u64
pub fn bitrate(&self) -> u64
The current target bitrate in bits per second: what
Config::bitrate resolved to at open, or the last value
set_bitrate accepted.
Sourcepub fn set_bitrate(&mut self, bitrate: u64) -> Result<(), Error>
pub fn set_bitrate(&mut self, bitrate: u64) -> Result<(), Error>
Retune the live encoder to bitrate bits per second, taking effect from
roughly the next frame. No IDR is forced, so this is cheap enough to
drive from a congestion controller: pair it with
rate::Control, which decides when the target
is worth moving.
Setting the rate the encoder is already at does nothing and succeeds.
§Errors
Returns Error::BitrateUnsupported if this backend can’t retune while
running. That’s not fatal: the encoder keeps running at its current rate,
so a caller driving a control loop should stop adapting rather than stop
encoding.
Sourcepub fn codec(&self) -> Codec
pub fn codec(&self) -> Codec
The codec this encoder emits. A Producer must be
built for the same codec to publish its packets.
Sourcepub fn keyframe(&mut self)
pub fn keyframe(&mut self)
Ask for the next frame to be encoded as a keyframe (an IDR), on top of the
ones Config::gop already inserts on its own.
Rarely needed: the encoder keys frames automatically, so reach for this only when something outside the encoder needs a decodable starting point at a specific frame. Opening a new group is the usual reason (a subscriber has to be able to start there); resuming after an idle gap is another.
The request waits for the next encode rather than applying
at once, so it is safe to call before the frame exists. Calling it repeatedly
before a frame arrives asks for one keyframe, not several.
Sourcepub fn encode(&mut self, frame: &Frame) -> Result<Vec<Encoded>, Error>
pub fn encode(&mut self, frame: &Frame) -> Result<Vec<Encoded>, Error>
Encode one raw Frame, whether it came from capture, a decoder (the
transcode input path), or your own pixels via
Surface::rgba.
Returns zero or more encoded access units, each carrying the timestamp of the raw frame it came from: a backend that buffers hands back an earlier frame’s output, so the two don’t always line up.
A GPU surface feeds a hardware encoder on the same device directly
(NVDEC -> NVENC never leaves the GPU, a CVPixelBuffer goes straight to
VideoToolbox); anything else falls back to a CPU I420 upload. The frame must
already be at the encoder’s resolution: decode with
decode::Config::resize, or scale first with
Frame::resize.
Sourcepub fn flush(&mut self) -> Result<Vec<Encoded>, Error>
pub fn flush(&mut self) -> Result<Vec<Encoded>, Error>
Return every access unit the codec is still holding, leaving the encoder ready for the frames that follow. Each keeps the timestamp of the raw frame it was encoded from, so a drained tail stays in step with what came before it.
Reach for this at a boundary the output has to respect, which for a live broadcast is a group: a hardware codec that pipelines holds the last frames of a group past its end, and they would otherwise be published into the next group ahead of its keyframe, where a consumer joining there cannot decode them. Publishing frame-by-frame with no group structure needs none of this.
Not free: emptying the pipeline gives up the overlap between one frame’s encode and the next frame’s submission, so flush at boundaries rather than per frame.
Sourcepub fn finish(self) -> Result<Vec<Encoded>, Error>
pub fn finish(self) -> Result<Vec<Encoded>, Error>
Flush the encoder, returning any buffered frames. Each keeps the timestamp of the raw frame it was encoded from, so a drained tail stays in step with what was published before it.
Consumes the encoder: nothing can be encoded after a flush, so this is the last call rather than one leaving a drained encoder in your hands.