Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

Video encoder. Build one with Encoder::new, feed it raw RGBA frames via encode_rgba, and publish the resulting packets through a Producer built for the same Codec.

Implementations§

Source§

impl Encoder

Source

pub fn new(config: &Config) -> Result<Self, Error>

Open an encoder for config.

Source

pub fn name(&self) -> &str

The encoder name in use, e.g. "videotoolbox".

Source

pub fn size(&self) -> Size

The resolution this encoder emits, which every frame fed to it must match.

Source

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.

Source

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.

Source

pub fn codec(&self) -> Codec

The codec this encoder emits. A Producer must be built for the same codec to publish its packets.

Source

pub fn encode_rgba( &mut self, rgba: &[u8], size: Size, keyframe: bool, ) -> Result<Vec<Bytes>, Error>

Encode one tightly-packed RGBA frame of size, returning zero or more encoded packets in the codec’s framing. Set keyframe to force an IDR (e.g. on resume so a re-subscribing viewer can start decoding at once).

size must equal the encoder’s size, and rgba must hold exactly width * height * 4 bytes with no row padding.

Source

pub fn encode_i420( &mut self, i420: &[u8], size: Size, keyframe: bool, ) -> Result<Vec<Bytes>, Error>

Encode one tightly-packed I420 frame of size (Y then U then V, no row padding, BT.601 limited range), returning zero or more encoded packets in the codec’s framing. Set keyframe to force an IDR.

size must equal the encoder’s size, and i420 must hold exactly width * height * 3 / 2 bytes.

The bring-your-own-I420 path, which copies the buffer to take ownership of it. A transcoder should prefer encode: it takes a decoded frame directly and keeps a GPU one on the GPU, so it neither copies nor round-trips through system memory.

Source

pub fn encode( &mut self, frame: &Surface, keyframe: bool, ) -> Result<Vec<Bytes>, Error>

Encode a Surface, whether it came from capture or a decoder (the transcode input path).

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 surface must already be at the encoder’s resolution: decode with decode::Config::resize, or scale first with decode::Frame::resize.

Source

pub fn finish(self) -> Result<Vec<Bytes>, Error>

Flush the encoder, returning any buffered packets.

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.

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<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> MaybeSend for T
where T: Send,

Source§

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> 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