pub struct Export<S: Stream> { /* private fields */ }Expand description
Subscribe to a moq broadcast and produce a single fMP4 / CMAF byte stream.
Built from a Source, Export subscribes to the hang catalog,
(un)subscribes per-rendition tracks as the catalog changes, decodes both Legacy and
CMAF tracks via a per-track source, and re-encodes everything as a merged init
segment + moof+mdat fragments, each track in presentation-timestamp order (see
poll_next for how far that holds across tracks). This is what an fMP4
player (e.g. ffplay, MSE) expects.
Use poll_next to pull byte chunks: the first call returns the merged
init segment (ftyp + multi-track moov), subsequent calls return moof+mdat
fragments. By default a fragment is a group, emitted when the group closes. A video
keyframe also rolls the fragment within a group; audio follows publisher cuts. with_fragment_duration caps the
fragment duration on top of that, for downstream consumers that throttle by
fragment rate. Returns None when the broadcast ends.
The init segment declares every enabled rendition in the catalog once each can be
described, and fragments cut while it waits are written right after it. Each track holds
at most its consumer’s max buffer meanwhile, and the export fails with
TrackUndescribed once the init has waited 30 s on the local
clock. Annex-B H.264 and H.265 whose catalog codec string and dimensions determine the
sample entry are described at the first catalog, as avc3 or hev1 with parameter sets in
the samples. From then on the track set is fixed: a rendition that leaves the catalog, as
a disabled one (enabled: false) does, is unsubscribed and idles. On its return it is resubscribed after
the last group read, under the track it was declared as, when its sample entry is
unchanged. That comparison covers the catalog-derived record, so an Annex-B encoder that
restarts with a new parameter set can return. A rendition the catalog moves to another
track (a new track or broadcast) continues under its declared track the same way, from
the new track’s first sync sample past what was read. A new rendition, a changed sample
entry, or a return that replays media already written fails the export.
A rendition whose track fails ends alone while another track still reads, and a new rendition of its medium continues it the same way, under its declared track: a publisher that loses a source, such as a microphone, lists a replacement under a new name. A new rendition that joins while a track of its medium still reads waits to continue that one should it fail, and fails the export once no track of its medium could.
poll_next_chunk returns the same bytes as a Chunk, which
separates the init segment from a Fragment carrying whether it begins at a
sync sample and how long it lasts. A segmenting consumer (e.g. an HLS/LL-HLS
packager) needs that to map fragments onto segments and parts; narrow the catalog
to a single rendition with Stream::select so the
fragments belong to one track.
Implementations§
Source§impl<S: Stream> Export<S>
impl<S: Stream> Export<S>
Sourcepub fn new(source: Source, catalog: S) -> Self
pub fn new(source: Source, catalog: S) -> Self
Subscribe to source and produce fMP4 byte chunks, driving track
(un)subscription from catalog.
catalog is any Stream of catalog snapshots, typically a
catalog::Consumer directly, or narrowed to
one rendition set via Stream::select.
Sourcepub fn with_delay(self, delay: Delay) -> Self
pub fn with_delay(self, delay: Delay) -> Self
Set how far behind the live edge each per-track source plays: Delay::min is where it
starts, and Delay::max how late a group may fall before it is skipped.
See Consumer for the per-track start and skip behavior.
Defaults to zero for both (start at the live edge, skip aggressively).
Sourcepub fn with_fragment_duration(
self,
duration: impl Into<Option<Duration>>,
) -> Self
pub fn with_fragment_duration( self, duration: impl Into<Option<Duration>>, ) -> Self
Cap the fragment (moof+mdat) duration.
By default a fragment is a group: one per GOP for video, and for audio one per
group the publisher cut, which is a packet for a live encoder and a segment for
an fMP4 import. Setting this caps each fragment to roughly duration of frames,
useful for downstream consumers that throttle by fragment rate, or to bound an
audio track whose publisher never cuts it. Duration::ZERO emits one fragment
per frame. Video with unknown duration waits for the next timestamp or endpoint
marker; audio and samples with explicit durations remain immediate. Otherwise
the cap applies in addition to the group boundary.
A fragment cut inside a GOP continues the decode timeline where the previous one
ended, so B-frame video keeps increasing decode times. Such a fragment cannot see
the reordering around it, so unless the catalog declares reorder: 0 (or the codec
never reorders), a video sample with unknown duration lasts no longer than the
catalog frame rate’s cadence.
Accepts either Duration or Option<Duration> (where None restores
the per-group default).
Sourcepub fn poll_next(&mut self, waiter: &Waiter) -> Poll<Result<Option<Bytes>>>
pub fn poll_next(&mut self, waiter: &Waiter) -> Poll<Result<Option<Bytes>>>
Get the next byte chunk.
The first call returns the merged init segment (ftyp + multi-track moov); each
subsequent call returns one moof+mdat fragment. Returns None when the catalog
and every track have ended.
A track’s own fragments always ascend in timestamp. Across tracks the order is
only approximate: a fragment leaves when its group closes or a frame rolls it, so a
video GOP follows the audio fragments that overlap it, and two video renditions
rolling on keyframes that do not coincide interleave by up to a GOP. Holding
fragments back to sort them would cost the length of the longest GOP in added
latency, which a live export should not pay for an ordering no fMP4 consumer
needs: a demuxer indexes by track and reads tfdt. Narrow the catalog to a
single rendition with Stream::select where
the order matters.
Sourcepub async fn next(&mut self) -> Result<Option<Bytes>>
pub async fn next(&mut self) -> Result<Option<Bytes>>
Get the next byte chunk.
The first call returns the merged init segment (ftyp + multi-track moov); each
subsequent call returns one moof+mdat fragment. Returns None when the catalog
and every track have ended.
A track’s own fragments always ascend in timestamp. Across tracks the order is
only approximate: a fragment leaves when its group closes or a frame rolls it, so a
video GOP follows the audio fragments that overlap it, and two video renditions
rolling on keyframes that do not coincide interleave by up to a GOP. Holding
fragments back to sort them would cost the length of the longest GOP in added
latency, which a live export should not pay for an ordering no fMP4 consumer
needs: a demuxer indexes by track and reads tfdt. Narrow the catalog to a
single rendition with Stream::select where
the order matters.
Awaits poll_next.
Sourcepub async fn next_chunk(&mut self) -> Result<Option<Chunk>>
pub async fn next_chunk(&mut self) -> Result<Option<Chunk>>
Like poll_next but gets a Chunk, which tells the init segment
apart from a Fragment and carries that fragment’s segmenting metadata.
Awaits poll_next_chunk.