pub struct Encoder<T> { /* private fields */ }Expand description
Encodes a JSON value into frame payloads, choosing snapshots and deltas automatically.
The track-free core of Producer: it decides what bytes go in a frame and
where the group boundaries fall, and leaves writing them to the caller. Reach for it when
something else already owns the track, for example a
moq_mux::container::Producer
that is also managing a timeline and a catalog estimate:
if let Some(frame) = encoder.update(&value)? {
container.write(moq_mux::container::Frame {
timestamp,
duration: None,
payload: frame.payload.clone(),
keyframe: frame.keyframe,
})?; // an early return here drops `frame`, resetting the encoder
frame.commit();
}Frames must reach the wire in the order they were encoded, and a frame with
keyframe set must open a new group: both the merge patches and the
group-scoped DEFLATE window depend on it. update hands back a Pending
rather than a bare Encoded so a frame that never reaches the wire can’t silently desync the
encoder: dropping it uncommitted resets, and the next value is encoded as a
fresh snapshot. Committing a frame you failed to write is the one way to corrupt the stream.
If the caller cuts a group for its own reasons (a cut, seek, or discontinuity), call
reset directly so the next value opens the new group with a snapshot.
Implementations§
Source§impl<T> Encoder<T>
impl<T> Encoder<T>
Sourcepub fn new(config: ProducerConfig) -> Self
pub fn new(config: ProducerConfig) -> Self
Create an encoder with a cold baseline, so the first update is a snapshot.
Sourcepub fn value(&self) -> Option<&Value>
pub fn value(&self) -> Option<&Value>
The last encoded value, or None before the first snapshot.
This is the baseline the next delta is diffed against, which is what a caller editing the value in place needs to start from.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Force the next update to emit a full snapshot, even for an unchanged value.
Call this whenever the caller closes the current group behind the encoder’s back (a
cut, a seek, a discontinuity). Without it the next value may be encoded as a delta
against a DEFLATE window and a baseline that the new group doesn’t carry.
value survives: the snapshot republishes it in full anyway, and it is what a
caller editing in place starts from.