pub struct Import<E: Catalog = ()> { /* private fields */ }Expand description
Demuxes an MPEG-TS byte stream into a MoQ broadcast.
Decodes H.264 (stream type 0x1B), H.265 (0x24), ADTS AAC (0x0F), MP2 (0x03/0x04), AC-3 (0x81), and E-AC-3 (0x87). Each codec stream is fed to its importer, which manages the track, catalog config, and keyframe-based group boundaries. MPEG-1/2 video (0x01/0x02) is not decoded but still paces the program: its PTS anchors the timebase while its PES ride whole as below.
Video access units can span PES packets, and one PES can contain several. Its PTS
stamps the AU whose required start-code prefix begins there, once. Later AUs need
anchored POC and proportional timing: AVC’s TS timing descriptor plus fixed-rate
SPS VUI, or HEVC’s poc_proportional_to_timing_flag. Missing timing ends the import;
it is not recoverable packet damage. Raw elementary-stream import keeps its
caller-supplied timestamp mode.
Partial header contents hold at most 264 bytes per PID. PES payloads, retained video AUs, and sections awaiting a timestamp share a 16 MiB budget, and at most 4096 units are buffered, the raw TS accumulator’s group roll triggers. Exceeding either limit refuses the input instead of retaining more bytes.
Elementary streams we don’t decode are carried verbatim, one MoQ track per
PID, when the catalog E carries the mpegts section: section-framed
streams (private sections 0x05, the ISO 13818-6 DSM-CC types 0x0A to 0x0D, and SCTE-35 0x86
marked by a program-level ‘CUEI’ registration descriptor) are reassembled as sections, and
any other stream travels as whole PES packets (PesPacket), LATM/LOAS
AAC (0x11) included. A 0x86 stream without ‘CUEI’, which may be DTS audio or SCTE-35, is not
carried. With a base Catalog<()> no undecoded stream is carried.
Every PID is accounted for. A listed stream that is not carried counts once in
stats::Stream::refused with a warning, and so does each TS-scrambled packet on a carried
one and each PES-scrambled PES on a decoded one. Once the PMT is in, a packet on a PID no table claims (the null PID aside) counts in its
own row: see stats::Snapshot::streams. That holds when the PAT lists only the imported
program; a with_program import of a multiplex cannot tell another
program’s PIDs from unclaimed ones, so it leaves them to Programs.
A whole PES or section is timestamped with its placement: the program clock at its first byte,
interpolated between the PCRs either side of it (ISO 13818-1 2.4.2.2) and shifted onto the
catalog clock as the media is. It waits for the PCR after it, and for the first media PTS
to anchor the timebase, or the first PCR when the PMT declares no media stream, within the
staging bound below; one that starts past the last PCR before a
clock break or the end of input is extrapolated from the last two. One before the first PCR,
or with no interval to extrapolate from, is dropped and counted in
stats::Stream::unplaced. A section’s SCTE-35 splice times absorb the media’s offset in
pts_adjustment; a whole PES’s PTS and DTS move by that offset, and every other byte is kept.
A stream whose PES carry a clock only the PTS and DTS of are rebased (ESCR, a
pack_header_field, a TREF), PES-level scrambling, an unbounded non-video PES, or a header
export would refuse is refused rather than altered. A program with no PCR refuses both
whole PES and section streams: stats::Stream::refused. A PMT that moves the PCR PID ends the
placement timebase, as a clock break does; one that drops the PCR refuses those streams, and
one that names it again carries them.
The PAT and every PMT are reassembled the same way, so a table that spans packets or
sits behind a nonzero pointer_field is read whole. A PAT or PMT section whose CRC-32
fails is dropped and counted in stats::Snapshot::crc_error; the last good table stays
in force.
With the mpegts catalog extension, the catalog goes out with the first media and gains
mpegts.muxRate as an update once a two-second PCR window measures a constant rate, which
a variable-rate source never does. An export that pads waits for that update. A restarted
import’s catalog carries its predecessor’s measurement of the same multiplex
(Republish), which the measurement then refines as usual.
The selected container applies only to decoded media renditions. Verbatim tracks in the
mpegts catalog section continue to use the legacy Hang container.
PTS values follow the program’s timestamp history across their 33-bit wrap, including the PCR history before a data-only program gains media.
Implementations§
Source§impl<E: Catalog> Import<E>
impl<E: Catalog> Import<E>
Sourcepub fn new(broadcast: Producer, reserved: Reserved<E>) -> Self
pub fn new(broadcast: Producer, reserved: Reserved<E>) -> Self
Import a byte stream into the broadcast using its reserved catalog producer.
Sourcepub fn with_timebase(self, timebase: Timebase<E>) -> Self
pub fn with_timebase(self, timebase: Timebase<E>) -> Self
Shift this program’s timestamps through timebase, a timestamp base of the catalog reserved
belongs to, instead of a fresh one: importers of one source share its offset.
Set it before the first decode, which may anchor the timebase.
Sourcepub fn with_program(self, program: u16) -> Self
pub fn with_program(self, program: u16) -> Self
Import only the program numbered program in the PAT, ignoring the rest of the multiplex.
Without this, a PAT that lists more than one program fails the import with
MultiplePrograms rather than merging them onto one clock. A PAT that does not
list program fails it too. The SI describes only this program’s service: other
services’ EIT actual is dropped and the SDT actual lists this service alone.
Sourcepub fn with_container(self, container: Container) -> Self
pub fn with_container(self, container: Container) -> Self
Select the container this importer wraps decoded media renditions in.
Legacy unless selected. It applies to every rendition
this input demuxes, since the tracks are discovered rather than named by the caller.
Sourcepub fn decode(&mut self, data: &[u8]) -> Result<(), Error>
pub fn decode(&mut self, data: &[u8]) -> Result<(), Error>
Append buf to the internal scratch and demux every whole TS packet it
now completes. The buffer is fully consumed; a trailing partial packet
(< 188 bytes) is retained for the next call.
A step of the source’s clock onto another timeline ends the identity, failing with
Restarted: the program clock and every timed stream stepping
together without discontinuity_indicator, forward by more than 10 s or back, or the
streams stepping back after a flagged break. Until the streams have spoken, which a
cadenced one does within 0.7 s, the input after a suspected step is held rather than
published, up to half a group’s bytes. A PAT naming another transport_stream_id or
program number restarts too, at that PAT. Every later call fails the same way;
finish ends the identity before the step.
A PAT listing several programs fails with MultiplePrograms
unless with_program picked one. Input after
finish is refused with Finished, or with the
cause of a finish that failed.
Republish carries on past each restart into a new identity, on a
path of an origin.
Sourcepub fn finish(self) -> Result<Finished<E>, Error>
pub fn finish(self) -> Result<Finished<E>, Error>
Flush any buffered PES, finish every track, and return the Finished import.
Input held while a step was judged is read first, failing with
Restarted if it reaches one, since this import would drop the
broadcast after it. A step decode already reported ends the identity
there instead. A finish that fails aborts every track with the cause, so it can’t be
retried.
Sourcepub fn stats(&self) -> Snapshot
pub fn stats(&self) -> Snapshot
Snapshot what every elementary stream has delivered, the audio frame sync lost on the way, damaged units refused, and the PAT and PMT sections dropped for a bad CRC.
Cheap: it reads counters the demuxer already keeps, so a caller can poll it per chunk and report the delta.
Sourcepub fn abort(self, err: Error)
pub fn abort(self, err: Error)
Abort every track with err instead of finishing, so subscribers see the
real cause rather than [moq_net::Error::Dropped]. Buffered PES is discarded.
Consumes the importer.
Unlike a drop, this leaves the tracks’ HLS records to the catalog, whose abort fails them rather than ending them as a complete recording.