Keyboard shortcuts

Press ← or → to go to the previous or next page

Press S or / to search the docs

Press ? to show this help

Press Esc to hide this help

Internals

Subscribing

moq_ext_player::RemoteBroadcast reads a broadcast’s catalog and holds the subscription. RemoteBroadcast::play starts a Player, which owns its decoders, its playout clock, its rendition choice and its statistics. Two players of one broadcast are two separate playbacks. Decoding is upstream: moq_video::decode::Consumer and moq_audio::decode::Consumer pick a backend from the catalog entry and return frames. This crate adds rendition selection, the decoder swap, and the playout clock. The code is in moq-ext-player/src/remote.rs and moq-ext-player/src/player/.

Opening a broadcast

Every constructor starts reading the catalog and returns at once. catalog() watches an Option<Arc<Catalog>> that reads None until the first catalog arrives. A background task follows the catalog track and publishes each update.

RemoteBroadcast::from_resolved(origin, path, consumer) follows a path in a route table, starting from a consumer the caller already resolved in that table. When a change of route ends the broadcast, it is requested again through the next route, and players see a new generation of the broadcast instead of an end. The node asks its route table again 100 ms after a route goes (see The node and its transport). The broadcast closes once no route has served the path for three seconds (REROUTE_PATIENCE). So closed() resolves about three seconds after a publisher ends its broadcast, or after the open if the publisher was already gone.

RemoteBroadcast::from_local(&broadcast) reads a LocalBroadcast in-process, without a transport.

Catalog is hang’s catalog, re-exported. Its video.ranked() lists the video renditions best first: enabled, then by preference, picture size and bitrate. video_rendition(&catalog, name) looks one up, with an error that lists the renditions the catalog has.

subscription.remote_broadcast(), from iroh_live::media::SubscriptionExt, builds the RemoteBroadcast with from_resolved and attaches the serving link’s measurements, whether the subscription came from Node::subscribe_ticket, a room or Node::subscribe. See Adaptive rendition switching.

Video decoding

A player runs three tasks. The selector (select.rs) turns the rendition mode, the catalog and the network into the rendition that should play. The video supervisor (video_task.rs) keeps one decoder playing and at most one replacement warming up. The audio task (audio.rs) decodes into the output.

Each decoder is read by its own task, which calls decode::Consumer::read() in a plain loop and forwards frames over a bounded channel two frames deep (READ_AHEAD). The supervisor selects over that channel and its control signals. The read cannot be an arm of the supervisor’s select!: dropping a read future poisons the decoder, and a select! drops every arm it does not pick.

An access unit the decoder refuses is skipped. After a skipped group or a truncated access unit, a decoder refuses every picture until the next keyframe. The reader gives up after 300 refusals in a row (MAX_CONSECUTIVE_DECODE_FAILURES), which spans several keyframe intervals, and counts what it skipped in the player’s stats.

Switching renditions

A rendition switch and a decoder change are the same operation. The supervisor opens a replacement decoder beside the incumbent, keeps showing the incumbent’s frames, and hands over once the replacement has caught up. The rules are plain transitions in moq-ext-adaptive/src/switch.rs, so tests drive them without a decoder or a network.

  • There is at most one replacement. A new request replaces it, so a switch to C while B is warming up never lands on B first.
  • A replacement takes over once its playhead is within 100 ms of the incumbent’s (CATCH_UP_SLACK, the slack @moq/watch uses). It waits for that at most one second after its first picture (CATCH_UP_PATIENCE), then takes over where it is.
  • A replacement takes over on a picture it decoded, never on opening alone. With nothing playing, including after the incumbent ended, its first picture is enough.
  • A replacement has 15 s from the request to taking over (SWITCH_DEADLINE). That covers the open, the wait for the track’s next keyframe and the catch-up. Past it the replacement is given up and the incumbent keeps playing.

An automatic step down does not overlap the two decoders. See Adaptive rendition switching.

Player::set_rendition(mode) changes the RenditionMode and returns at once. To wait until a rendition is on screen, wait on Player::status() for its rendition.

Player::status() watches a PlayerStatus: each slot’s state, the mode, the rendition on screen, the one warming up (switching_to), why the last switch or pin failed (switch_error), and the decoder backend running.

Frame delivery

Decoded frames land in a latest-wins slot. Player::video_frames() returns a VideoFrames handle onto it. Every call returns another handle onto the same stream, which survives rendition switches and decoder changes. Each handle keeps its own cursor, so two readers both see the current picture. A reader that falls behind skips to the newest one. try_next() polls without blocking, for a render loop, and next().await waits for a picture newer than the last one the handle read.

Each presented picture’s FrameTiming (arrived, decoded, presented) goes into a log of the last 512, which Player::video_timing(timestamp) searches. FrameTiming::decode_time() is the span from arrived to decoded, and queued() the span the playout clock held the picture. Player::take_latest_video() pairs the two for a consumer that samples at its own rate: the newest picture since its last call, with its timing. Player::video_backlog(capacity) returns a VideoBacklog, whose take() returns every picture presented since the last take, with its timing. arrived comes from the decoder, which reports when the access unit behind each picture landed on this node.

Audio playback

PlayerConfig::audio takes an AudioMode. AudioMode::Play(engine) (the playback feature) plays on a moq_audio::playback::Engine. The application opens the engine with Engine::open(config) and passes a clone to every PlayerConfig that should play there, so several players share one device stream. The engine lists and switches devices. AudioMode::play_or_decode takes an Option<Engine> and returns AudioMode::Play for Some and AudioMode::Decode for None. AudioMode::Decode still decodes, for Player::audio_frames, headless use and tests, but nothing paces the audio. AudioMode::Off, the default, does not subscribe to audio at all.

The audio task plays the first audio rendition. It writes frames straight to its sink and reports how much audio is queued ahead of the speaker to the playout clock on every frame. Player::set_volume sets the level. PlaybackStats::audio carries the buffered duration, the latest peak, and the RMS level of the last 100 ms of decoded audio, which the task measures with or without a speaker. The task reopens when the broadcast moves to a new route. A track that ended or never opened is tried again when the catalog changes, and every two seconds.

Player configuration

PlayerConfig starts from Default, with a with_* builder per field:

let config = PlayerConfig::default().with_audio(AudioMode::Play(engine));

It carries the rendition mode, the latency, the audio mode, the decoder backend, and the adaptation thresholds and timers. latency is moq-net’s subscription Delay { min, max }. The playout clock holds each picture for at least min, and longer when the catalog’s delay or the measured audio jitter asks for it. Upstream drops groups older than max. DEFAULT_LATENCY holds for at least 100 ms and skips past 150 ms.

Player::set_latency and Player::set_decoder change a running player. Each opens a new video decoder behind the picture, which takes over once it has caught up, as in a switch, except that a new latency max alone moves the open decoder’s subscription in place. A backend that fails to open leaves the incumbent playing and says why in PlayerStatus::switch_error. Playout and A/V sync covers the latency in full.

Shutdown

Dropping the Player cancels its tasks and closes its playout clock, which wakes anything waiting for a frame’s playout time. Dropping a reader task drops its decoder. Each player holds a clone of its RemoteBroadcast, so the subscription lasts until the last broadcast handle and the last player are gone. RemoteBroadcast::closed() waits until the broadcast itself has ended.