Skip to main content

Module json

Module json 

Source
Expand description

JSON track publish/subscribe.

A JSON track is application data a broadcast publishes alongside its media: a chat log, a telemetry feed, a status document. The catalog entry (hang::catalog::JsonConfig) says how to read it, so a consumer needs the track name and nothing else about the application.

The write and read sides are shaped differently on purpose. A publisher knows its mode at compile time, so Snapshot and Stream are distinct types and append-on-a-snapshot can’t be written. A consumer only learns the mode from the catalog, so there is one Consumer that reads either; it exposes mode for a reader that needs to know.

Mint a producer from the catalog:

// A timed track, since every write is stamped. JSON is small and repetitive, so compress it.
let info = moq_net::track::Info::default()
    .with_timescale(moq_net::Timescale::MILLI)
    .with_compression(moq_net::track::Compression::Deflate);
let track = broadcast.create_track("chat", info).ok_or(moq_net::Error::Duplicate)?;
let mut chat = catalog.json_stream::<Message>(track, moq_mux::json::Config::default())?;
chat.append(&Message { text: "hello".to_string() }, catalog.clock().now())?;

The catalog entry is written when the producer is created and removed when it drops, so a track is never advertised without a publisher behind it.

Every write takes a timestamp on the broadcast Clock, the epoch every track in the broadcast shares, so the track must have a timescale. A value is written at that timestamp as given, and the entry advertises how late values reach the transport as its jitter and delay, the way a media rendition does. A capture Instant converts with Clock::capture on the catalog’s clock:

let at = catalog.clock().capture(received)?;
gps.append(&fix, at)?;

A timestamp carried over from elsewhere, such as a source’s own timestamp or the at of a consumed Timed the value was derived from, is published unchanged too. It lines up with media only on a broadcast sharing the source’s clock mapping.

Read one back off the catalog, naming it once:

let config = catalog.json.tracks.get("chat").expect("no chat track");
let entry = moq_mux::catalog::Entry::new("chat", config);
let mut chat = entry.subscribe::<Message>(source).await?;
while let Some(message) = chat.next().await? {
    // ...
}

Structs§

Config
Everything a JSON track declares about itself, beyond its mode and name.
Consumer
Reads a JSON track, in whichever mode its catalog entry declares.
Options
How a Snapshot encodes its frames, beyond what its catalog entry declares.
Snapshot
Publishes a latest-value JSON track, advertised in the catalog for as long as this handle lives.
Stream
Publishes an append-log JSON track, advertised in the catalog for as long as this handle lives.