Skip to main content

Encoder

Struct Encoder 

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

Video encoder. Build one with Encoder::new, submit raw Frames via encode, receive the resulting Encoded access units until it returns None, and publish them through a Publisher built for the same Codec.

An encoder is bound to the thread that opens it. Use Sink when the owner can move between threads.

ⓘ
fn move_to_another_thread(encoder: moq_video::encode::Encoder) {
    std::thread::spawn(move || drop(encoder));
}

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 applied(&self) -> Applied

The latency and compression controls the backend applied for Config::preset, and whether it cuts, fixed for the session’s lifetime.

Source

pub fn size(&self) -> Size

The resolution this encoder emits; external Vulkan inputs may be larger.

Source

pub fn bitrate(&self) -> Rate

The current target bitrate: what Config::bitrate resolved to at open, or the last value set_bitrate accepted.

Source

pub fn set_bitrate(&mut self, bitrate: Rate) -> Result<(), Error>

Retune the live encoder to bitrate, 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 moq_mux::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 keeps encoding, and may try again later.

Source

pub fn codec(&self) -> Codec

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

Source

pub fn cut(&mut self) -> Result<(), Error>

Cut a new group at the next frame, on top of the boundaries Config::join places on its own. Today that means encoding it as a keyframe (an IDR), so a subscriber can start decoding there.

Rarely needed: the encoder opens groups automatically, so reach for this only when something outside the encoder needs a boundary at a specific frame. A source group boundary (transcode mirrors the source’s groups) is the usual reason; a scene change, a source switch, or a subscriber joining over track::Info::refresh are others. Capture cuts for joiners itself.

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 cuts once, not several times.

§Errors

Returns Error::CutUnsupported when this backend cannot force a group boundary (a V4L2 driver without the force-keyframe control), in which case nothing is queued: groups keep falling every Config::join’s worth of frames at Config::framerate, and the caller decides whether that layout is acceptable rather than finding out from the stream.

Source

pub fn encode(&mut self, frame: &Frame) -> Result<(), Error>

Submit one raw Frame, whether it came from capture, a decoder (the transcode input path), or your own pixels via Surface::rgba.

Its access units come out of receive, each carrying the timestamp of the raw frame it came from: a backend that buffers readies 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, except external Vulkan inputs which the backend scales on the GPU. Config::resize scales the rest here; without it, scale them with Frame::resize, which decode::Config::scale_hint lets a hardware decoder make a no-op. An unscaled frame has no time and fails with Error::TimeOverflow.

Source

pub fn receive(&mut self) -> Result<Option<Encoded>, Error>

Pop the oldest ready access unit, or None while the codec holds the rest.

Call until it returns None after each encode and flush.

Source

pub fn flush(&mut self) -> Result<(), Error>

Mark the end of input, so receive returns every access unit the codec is still holding. The encoder stays 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.

Call it before dropping the encoder, or its last frames are lost.

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.

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