Internals
Publishing
A LocalBroadcast, from moq-ext-publisher, wraps a moq-net broadcast
producer. It owns the catalog, a media clock, one video slot and one audio
slot. set_video, set_encoded_video and set_audio fill a slot and
replace what it held. clear_video and clear_audio empty it.
A transport creates the broadcast it publishes, and
LocalBroadcast::new_in(producer) fills that one, so readers get it without
a copy. In iroh-live, the layout publishes one at
live/<endpoint id>/<name>:
let broadcast: LocalBroadcast = layout.publish(&node, name)?;
LocalBroadcast::new() creates one of its own, published nowhere, for a
local preview or a test. Closing a LocalBroadcast, or dropping its last
clone, closes the broadcast, which withdraws the publication.
Encoding is upstream. moq_video::encode and moq_audio::encode own the
codec, its thread and the catalog entry it writes. This crate adds a ladder
of renditions over one source, and publishes pre-encoded streams without
re-encoding them.
Sources
A source is already open when you get it. Opening a device is async and fails
if the device does not open, so a missing camera is an error at the call that
asked for it. Each source runs on a thread of its own, which non-Send
platform capture objects require. A source runs while any clone exists,
including the clone a broadcast holds, and any number of broadcasts and
previews can read it.
VideoSource has these constructors:
capture(moq_video::capture::Config)opens a camera, a display or a window, and returns once it produced a frame. It needs thecapturefeature. A camera that produces no frame within 10 seconds fails, and a display or a window within 30 seconds. A camera open first waits up to three seconds for the cameras dropped before it to close their devices, so a switch back to the same camera does not fail as busy (see below).camera(id)andscreen()arecapturewith a camera or the main display at its default mode.screen_source()returns the sourcescreen()opens: the desktop portal on Wayland with thepipewirefeature, the main display elsewhere.test_pattern(size, rate)draws a sweeping bar, a frame counter, a clock, and a marker that flashes in step withAudioSource::test_pattern’s beep.push(format)returns aFrameSenderand the source it feeds, for frames the application makes. The Android demo hands over camera buffers this way.spawn(name, format, run)runsrunon a dedicated thread with a current-thread runtime entered, and hands it the sender. It is for thread-bound platform objects.rpicam(config)reads raw pictures fromrpicam-vidon a Raspberry Pi.
The test pattern changes every frame on purpose. A static image compresses
to almost nothing after the first keyframe, so a test that waits for bytes
would pass on a stalled pipeline. With AudioSource::test_pattern, it shows
smoothness, dropped frames, latency and A/V sync without a camera or
microphone.
FrameSender::push never blocks: a new video frame replaces one not yet taken.
FrameSender::demand() reports whether anything wants the frames: a rendition
that encodes, or a VideoFrames handle from VideoSource::frames(). An
application camera can idle while nothing does. push returns SourceClosed
once the source is gone, and the producing loop should stop then.
switchable(format) returns a VideoSwitch and the source it feeds, for a
camera menu. The broadcast encodes the switchable source once. A forwarding
task per input copies the current input’s frames into the source’s slot,
keeps each frame’s sent_at, and passes the switchable source’s demand back
to the input. The switch has three operations:
replace(source)swaps at once.open(future)opens the next source in the background, and drops one still opening.open_spec(spec, options)opens a source specifier fromspecthat way, and clears the switch fornone.clear()stops feeding.
The previous source is dropped first, so the same camera opens again.
replace and open also drop the previous source’s last frame from the
slot, so no reader gets a frame of the old source after the switch.
status() watches a SlotState: Starting while an open runs, then
Running, Failed or Ended. Unlike a second set_video, a switch neither
probes encoders again nor needs the ladder at hand. The broadcast sees one
source whose timeline breaks, and starts a group there.
moq-video’s capture stream closes its device when it drops. A camera source
drops that stream on its own capture thread, after the last handle is gone. So
the device can still be held when the application opens the next camera. A
dropped camera source therefore leaves a release token in a small
process-wide list, and capture waits on those tokens before it opens a
camera. Some captures get stuck in a read, such as a Media Foundation camera
that delivers neither a frame nor an error. The wait gives up on such a
capture after three seconds. VideoSource::released returns the same token,
for a device that something else opens next.
set_encoded_video takes H.264 that is already encoded: an Annex-B byte
stream such as rpicam_h264(config) returns, or a file’s access units with
their timestamps from MediaFile::encoded_video. On a Raspberry Pi,
rpicam-vid encodes in hardware and no raw picture reaches iroh-live.
set_encoded_audio takes a file’s AAC the same way, and set_encoded_file
sets whichever of the two a MediaFile has.
set_spec sits on top of the setters. It takes a MediaSpec from
moq-ext-sources, a video specifier and an optional audio specifier, opens
every source before it sets any, and then calls set_video or
set_encoded_video, and set_audio or set_encoded_audio. set_spec_with
does the same with VideoOpenOptions for the size, the frame rate and a held
file. A file: video
passes through unless the spec asks to re-encode it, and brings its own
audio unless the spec names another. An audio source of the same file, or
of the file VideoOpenOptions::with_file holds, shares the file’s clock.
AudioSource has these constructors:
microphone(config)opens a microphone from moq-audio’scapture::Config.file(path, at_end)decodes a WAV, MP3 or MP4 file in real time, a WAV or MP3 with symphonia on its own thread and an MP4 throughMediaFile. TheFileEndsays whether it stops or restarts at the end of the file.tone(hz, layout)andtest_pattern(layout)generate sound.push(format)returns aFrameSenderfor PCM the application produces, andspawn(name, format, run)runs a producer on a thread of its own.
For echo cancellation, the config’s aec holds the canceller of the speaker’s
playback::Engine, from Engine::canceller (needs the aec feature). The
microphone attaches to it when its publication opens the device, after a
publication it replaces has let go. A PCM source fans out to every broadcast
through a buffer of 100 frames. A broadcast that falls further behind loses
the oldest frames and counts them in its stats.
Simulcast
set_video(source, encoding) takes a VideoEncoding. VideoEncoding::single
publishes one rendition and VideoEncoding::ladder several.
VideoEncoding::default() is one rendition named video at the source’s
size and rate. A
VideoRendition has these parts:
name, its track name;- an optional
sizeandrate; - moq-video’s own
encode::Configfor the rest: bitrate, keyframe interval (join, two seconds by default), codec, backend (kind) and preset. The config’s size, rate and color are filled in when the rendition opens.
VideoRendition::new(name) encodes at the source’s size. p360 and p720
are 16:9 presets at 500 kbit/s and 1.8 Mbit/s. Both are close to the 0.07
bit per pixel that moq-video derives without a bitrate (484 kbit/s and
1.94 Mbit/s at 30 fps).
set_video fails at once for a ladder it cannot encode: an empty ladder, an
empty or duplicate name, an odd or zero size, a rate above the source’s, a
zero keyframe interval, or H.265 in software. openh264, the one encoder every
build has, encodes only H.264.
The source opens once, and each rendition’s encoder reads its frames through
a latest-wins handle of its own. Frames are Arcs, so the preview and every
rendition share one allocation. A rendition that falls behind drops frames
without stalling the others. A frame of another size than its rendition, such
as a phone camera turned to portrait, is scaled on that rendition’s encoder
thread by moq-video’s encode::Config::resize.
The first frame any rendition encodes anchors the mapping from source timestamps to the broadcast clock. Every rendition carries the same timestamp for the same picture, so a subscriber that switches sees no jump.
A subscriber only ever sees broadcast timestamps. A publisher that measures
a round trip, such as the latency from capture to a result that a subscriber
reports back, needs to map an echoed timestamp to its own frame.
VideoEncoding::with_recorded_timestamps() keeps a small record for that,
and its API docs describe the methods that read it.
Before a rendition is advertised, encode::Config::probe() runs once for it,
so the catalog entry describes exactly what the track will carry. The whole
ladder is probed before any of it reaches the catalog. With the encoder Auto
or Hardware, a hardware encoder that fails to open or fails mid-stream falls
back to software once. Kind::Software asks for software from the start. A
backend named explicitly is never replaced, so a broken driver shows
as a failed rendition.
Audio has no ladder. set_audio(source, encoding) publishes one track, named
after its codec. The encoding is an AudioEncoding: moq-audio’s
encode::Config and the span of each group. The audio_encoding module has
three presets:
voice(): Opus mono at 32 kbit/s.music(): Opus stereo at 128 kbit/s.pcm(format): uncompressed PCM in the given format, usually the source’s (AudioSource::format).
A plain encode::Config converts with the default span.
Each group carries 100 ms of audio by default, moq-audio’s own default, and
with_group_span sets another. A lost packet stalls only the rest of its
group, and a new subscriber joins at a group boundary, so shorter groups lose
less audio on a loss and join a little closer to live. Longer groups mean
fewer streams, which a media relay with many subscribers feels most. 20 ms puts each
Opus frame in a group of its own, as moq-audio did before.
Demand
Each rendition’s encoder waits on producer.demand().used() and opens a
moq_video::encode::Sink only once someone subscribes to that rendition. While
it encodes, it counts as demand on the source, which FrameSender::demand()
reports. When the last viewer leaves, the encoder closes and the producer
records a discontinuity, so the next timestamp does not stretch a frame across
the gap. The track and its catalog entry stay advertised throughout.
A subscriber that joins mid-group reads the newest group from its keyframe.
Each rendition’s track sets track::Info::refresh to 2, as moq-video’s
capture does. Once a group’s deltas outweigh twice its keyframe, a new viewer
asks for a newer group (Demand::wanted). The encoder then makes a keyframe
on its next frame. So the joiner starts near a keyframe instead of
downloading the rest of a long group, and a later joiner benefits too.
moq-video measured the cost at 10 to 13% more bitrate for one join a second, and about 1% for one every ten seconds. A player that switches renditions joins too. Requests that arrive while the keyframe is still in the encoder coalesce, and a fresh encoder’s opening keyframe serves every earlier request, as in moq-video’s own capture.
moq-video advertises the refresh only when every backend it may open honors a keyframe request. iroh-live advertises it unless the rendition names the V4L2 encoder. An encoder that cannot make a keyframe on request answers with its next scheduled keyframe, which is what a joiner gets without a refresh.
The producer of a pushed source can ask for a keyframe at a frame of its
choice with FrameSender::request_keyframe(). The request bumps a counter on the
source’s frame slot, and every frame pushed afterwards carries the count.
Each rendition’s VideoFrames handle reports the first frame it reads past a
new count through keyframe_requested(), so every rendition cuts once, on
the next frame it encodes, even if it skipped the frame the request preceded.
scaled and switchable sources pass the request on. The encoder’s join
stays in force, and a backend that cannot cut ignores the request.
The catalog reservation carries encode::Config::effective_join(), so
moq-mux declares media.join.interval from the encoder’s keyframe interval
rather than measuring it from the first group.
The encoders and the publisher’s frame-rate meter read the source through
VideoSource::observe(), which does not count as demand, so an idle broadcast
does not keep an application camera running. A preview’s handle does count, so
such a camera still feeds the publisher’s own preview while nobody watches.
Capture devices and generators run regardless of demand.
A microphone is not a shared source. The audio slot opens it on a thread of
its own as soon as set_audio runs, as one moq_audio::capture::Producer that
feeds the encoder. A failure, at open or later, shows in status() as a
Failed audio slot, and the slot stays failed until the next set_audio.
LocalBroadcast::status() watches a PublishStatus: each slot’s SlotState
(Off, Starting, Running, Failed, Ended), and per rendition a
RenditionState (Idle, Encoding { encoder }, Failed). stats() returns
a PublishStats snapshot with one entry per rendition.
Preview
VideoSource::frames() returns a VideoFrames handle onto the source’s raw
frames. These are the frames the encoders receive, so a preview costs no
decode. The handle counts as demand until it and its clones are dropped, so a
source that idles until wanted produces frames for it. The preview belongs to
the source, so it works before any broadcast exists. Pre-encoded video has no
preview.
Pre-encoded media
set_encoded_video never opens an encoder. For an Annex-B stream,
moq_mux::codec::h264::Split cuts the bytes into access units, and
moq_mux::codec::h264::Import publishes them as the one rendition video,
filling in the catalog entry from the first SPS. The splitter holds the last
access unit until the next start code, so the end of the stream flushes it
explicitly. A file’s access units go to moq_mux::import::Track with the
file’s avcC instead, each with its presentation time, so B-frames keep their
order. set_encoded_audio publishes AAC frames through the same importer,
one group per frame. A stream that ends before its first frame shows as a
failed slot.
A file’s frames carry the wall-clock instant their timestamp stands for. The broadcast maps a stream’s timestamps with the offset of its first frame, so the audio and video of one file stay in sync whichever starts first.
Files
MediaFile reads the sample index of an MP4 file with mp4-atom, from the
moov sample tables of a plain file or the moof of each fragment, and
reads each sample from the file when it is due. It applies the edit list:
empty edits delay a track, and the media edit trims its start and end. A
track presents from the first keyframe that the edit keeps. Each
sample is due at its decode time, and a decoded picture at its presentation
time. The clock starts with the first sample any stream asks for, and a
stream that starts later joins at the clock’s position, at the next
keyframe. A looping file starts every track again once the longest has
ended, with timestamps that keep rising. A file that falls more than a
second behind, after a stalled disk for example, moves its timeline forward
on every stream at once instead of racing to catch up. The content resumes
where it stopped.
The passthrough trade-offs are in
Publish a file. MediaFile::video
and MediaFile::audio decode with moq-video’s and moq-audio’s decoders into
raw sources, which set_video and set_audio encode like a camera.
Decoded pictures stay in the decoder’s memory, at the file’s size, and the
renditions scale them. The decode path puts pictures back into
presentation order, since moq-video’s VideoToolbox backend returns them in
decode order.
See Publish from a Raspberry Pi for rpicam_h264.
Catalog and clock
The catalog is hang’s, written by moq_mux::catalog::Producer with no
extension, so any hang player reads it. Rooms carry display names in their own
announcements (see Run a room).
A LocalBroadcast holds one moq_mux::Clock. Both slots stamp their media
from it, and the catalog advertises its wall-clock mapping. Each source’s
timestamps are rebased onto it at the first frame, so audio and video share a
timeline even though the devices open at different times.
close() ends the broadcast for every clone. It stops the encoders, gives each
up to two seconds to finish its track, then ends the catalog and the broadcast,
which withdraws it everywhere it was published. closed() waits for that.
Dropping the last clone without close also ends the broadcast, without the
clean finish.
Where in the code
moq-ext-publisher/src/publish/:LocalBroadcast, the slots, the ladder and demand.moq-ext-sources/src/:VideoSource,AudioSource, the switch and the release tokens.VideoEncoding::with_recorded_timestamps,video_source_timestamp,video_captured_atandvideo_encoded_at: the timestamp record, 64 bytes for each of the last 512 pictures.