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,
decode: None,
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: the merge patches 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. To open
one right away, such as when a joiner wants a fresh group (see
Demand::wanted), call snapshot.
Implementations§
Source§impl<T> Encoder<T>
impl<T> Encoder<T>
Sourcepub fn new(config: Config) -> Self
pub fn new(config: Config) -> 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 committed value, or None before the first snapshot commits.
This is the baseline the next delta is diffed against, which is what a caller editing the value in place needs to start from. A frame dropped uncommitted never reached the wire, so its value never lands here.
With deltas disabled the baseline is held as the encoded bytes, so the first call parses them; the result is cached, and callers that never read the value never pay for it.
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 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.
Sourcepub fn snapshot(&mut self) -> Result<Option<Pending<'_, T>>>
pub fn snapshot(&mut self) -> Result<Option<Pending<'_, T>>>
Restate the last committed value as a full snapshot, which must open a new group.
For a caller that opens groups itself: call it when a joiner wants a fresh group, such as when
Demand::wanted passes the largest group written. The new
group restates the value without the deltas before it, though that joiner still reads the
cached group first. Returns None before the first value,
when there is nothing to restate. The frame is a Pending like any other.