Skip to main content

LlHlsClient

Struct LlHlsClient 

Source
pub struct LlHlsClient { /* private fields */ }
Expand description

A driveable, sans-IO Low-Latency HLS (RFC 8216bis) playback client.

LlHlsClient never touches a socket or a clock. The caller drives it:

  1. LlHlsClient::new seeds the first Action::FetchPlaylist; drain it with LlHlsClient::poll and perform the GET.
  2. Feed the response back with LlHlsClient::on_playlist (playlist) or LlHlsClient::on_resource (init/part/segment bytes) — Action::FetchResource’s id correlates the two.
  3. Drain LlHlsClient::poll again for the next round of actions (a new reload, newly discoverable parts, a preload-hint prefetch, …) and LlHlsClient::next_output for newly available Outputs.

§Behaviour

  • Reload scheduling (issue #717 slice 2): once a playlist advertises EXT-X-SERVER-CONTROL/EXT-X-PART-INF and the origin’s CAN-BLOCK-RELOAD attribute is YES (transmux::hls::LowLatencyConfig::can_block_reload is truenot merely transmux::hls::MediaPlaylist::low_latency being Some, since an origin may carry parts/PART-INF while still advertising CAN-BLOCK-RELOAD=NO), every reload is a Blocking Playlist Reload (RFC 8216bis §6.2.5.2) naming the next not-yet-seen Partial Segment’s _HLS_msn/_HLS_part. Otherwise reloads are plain GETs paced by an Action::WaitMs hint derived from #EXT-X-TARGETDURATION. EXT-X-SKIP/CAN-SKIP-UNTIL Playlist Delta Updates (RFC 8216bis §4.4.5.2) are requested once a full-playlist baseline exists, and merged back into a full view before further processing — see merge_delta internally.
  • Fetch pipeline (slice 3): the EXT-X-PRELOAD-HINTed part is fetched ahead of its own appearance as a numbered EXT-X-PART; BYTERANGE parts are supported, including the RFC 8216bis §4.4.4.9 “omitted offset means immediately after the previous sub-range of the same resource” rule (tracked per resource URL). The Media Initialization Section (EXT-X-MAP) is fetched once and reused for every following resource until the map changes.
  • Dedup / coalescing: once any of a segment’s parts have been individually fetched, that segment is never re-fetched whole — when it later closes (#EXTINF+URI), the client only fetches whichever of its parts (if any) are still missing, and marks the segment “delivered” once every part is accounted for (fetched, or GAP=YES). A playlist whose segments carry no parts at all (a non-LL origin) falls back to fetching the whole segment resource — the two paths never overlap for a single segment, so a part’s samples are never double-counted against its parent’s.
  • Output adapter (slice 4): exactly one Output::Init precedes any Output::Samples; parts/segments are demuxed via transmux::Fmp4Demux (by concatenating the cached init bytes with the fetched resource — this crate never re-implements ISOBMFF box parsing, only reuses transmux’s), so Output::Samples carries real access units, not opaque container bytes. #EXT-X-DISCONTINUITY on a segment surfaces as Output::Discontinuity immediately before that segment’s first samples. Known limitation: an in-progress (OpenSegment) segment carries no discontinuity flag of its own (only a closed MediaSegment does) — if every part of a segment was already delivered while it was still open, a discontinuity revealed only once it closes is signalled late (after those parts’ samples, not before). This is a gap in the current wire model (transmux::hls::OpenSegment), not something this crate can fix locally.
  • Classic MPEG-TS-segment HLS (issue #760): a playlist that never advertises an EXT-X-MAP (HLS v3, the dominant legacy/IPTV form — self-contained .ts segments carrying their own PAT/PMT/PES, no separate init resource) routes each fetched Part/Segment through transmux::TsDemux instead, content-sniffed by the MPEG-TS sync byte rather than blocked on an init fetch that will never come. The first successfully demuxed segment’s recovered TrackSpecs synthesize the one Output::Init this crate’s contract requires (via transmux::build_init_segment) so downstream callers (e.g. multimux’s HlsPull, which recovers track specs from Output::Init) need no TS-specific handling of their own. The fMP4/CMAF plus LL (parts/preload-hint) path above is entirely unchanged; the two never overlap for a single playlist.

Implementations§

Source§

impl LlHlsClient

Source

pub fn new(playlist_url: impl Into<String>) -> Self

Create a new client for the Media Playlist at playlist_url, seeding the first Action::FetchPlaylist (a plain, non-blocking GET — the client does not yet know whether the origin supports blocking reload).

Examples found in repository?
examples/client_stepping.rs (line 90)
86fn main() {
87    let playlist = canned_playlist();
88    println!("--- canned media.m3u8 ---\n{playlist}");
89
90    let mut client = LlHlsClient::new(PLAYLIST_URL);
91
92    // The client always seeds a plain (non-blocking) GET first — it hasn't
93    // seen a playlist yet, so it doesn't know the origin supports blocking
94    // reload.
95    match client.poll() {
96        Some(Action::FetchPlaylist {
97            url,
98            blocking,
99            skip,
100        }) => {
101            assert_eq!(url, PLAYLIST_URL);
102            assert!(blocking.is_none());
103            assert!(!skip);
104            println!("action: FetchPlaylist {{ url: {url:?}, blocking: None }}");
105        }
106        other => panic!("expected the seeded FetchPlaylist, got {other:?}"),
107    }
108
109    // Feed the canned playlist in response to that (imagined) GET — no HTTP
110    // client is ever involved.
111    client
112        .on_playlist(playlist.as_bytes())
113        .expect("the canned playlist parses");
114
115    // Drain every action the client now wants performed: the closed
116    // segment's bytes, the open segment's landed part, the init segment
117    // (from `#EXT-X-MAP`), the preload-hinted next part, and finally a
118    // Blocking Playlist Reload naming the next Media Sequence Number/part.
119    let mut saw_blocking_reload = false;
120    while let Some(action) = client.poll() {
121        match &action {
122            Action::FetchResource { id, url, .. } => {
123                println!("action: FetchResource {{ id: {id:?}, url: {url:?} }}");
124            }
125            Action::FetchPlaylist {
126                url,
127                blocking: Some(b),
128                ..
129            } => {
130                println!(
131                    "action: FetchPlaylist {{ url: {url:?}, blocking: {b:?} }}  <- blocking reload"
132                );
133                saw_blocking_reload = true;
134            }
135            Action::FetchPlaylist {
136                url,
137                blocking: None,
138                ..
139            } => {
140                println!("action: FetchPlaylist {{ url: {url:?}, blocking: None }}");
141            }
142            Action::WaitMs(ms) => println!("action: WaitMs({ms})"),
143            // `Action` is `#[non_exhaustive]` — a future variant is simply
144            // not printed by this demo, not a compile break.
145            _ => {}
146        }
147    }
148    assert!(
149        saw_blocking_reload,
150        "this crate's own origin renderer defaults CAN-BLOCK-RELOAD=YES, so the \
151         next reload the client schedules must be a blocking one"
152    );
153}
Source

pub fn playlist_url(&self) -> &str

The Media Playlist URL this client is following.

Source

pub fn poll(&mut self) -> Option<Action>

Drain the next IO Action the caller must perform, if any.

Examples found in repository?
examples/client_stepping.rs (line 95)
86fn main() {
87    let playlist = canned_playlist();
88    println!("--- canned media.m3u8 ---\n{playlist}");
89
90    let mut client = LlHlsClient::new(PLAYLIST_URL);
91
92    // The client always seeds a plain (non-blocking) GET first — it hasn't
93    // seen a playlist yet, so it doesn't know the origin supports blocking
94    // reload.
95    match client.poll() {
96        Some(Action::FetchPlaylist {
97            url,
98            blocking,
99            skip,
100        }) => {
101            assert_eq!(url, PLAYLIST_URL);
102            assert!(blocking.is_none());
103            assert!(!skip);
104            println!("action: FetchPlaylist {{ url: {url:?}, blocking: None }}");
105        }
106        other => panic!("expected the seeded FetchPlaylist, got {other:?}"),
107    }
108
109    // Feed the canned playlist in response to that (imagined) GET — no HTTP
110    // client is ever involved.
111    client
112        .on_playlist(playlist.as_bytes())
113        .expect("the canned playlist parses");
114
115    // Drain every action the client now wants performed: the closed
116    // segment's bytes, the open segment's landed part, the init segment
117    // (from `#EXT-X-MAP`), the preload-hinted next part, and finally a
118    // Blocking Playlist Reload naming the next Media Sequence Number/part.
119    let mut saw_blocking_reload = false;
120    while let Some(action) = client.poll() {
121        match &action {
122            Action::FetchResource { id, url, .. } => {
123                println!("action: FetchResource {{ id: {id:?}, url: {url:?} }}");
124            }
125            Action::FetchPlaylist {
126                url,
127                blocking: Some(b),
128                ..
129            } => {
130                println!(
131                    "action: FetchPlaylist {{ url: {url:?}, blocking: {b:?} }}  <- blocking reload"
132                );
133                saw_blocking_reload = true;
134            }
135            Action::FetchPlaylist {
136                url,
137                blocking: None,
138                ..
139            } => {
140                println!("action: FetchPlaylist {{ url: {url:?}, blocking: None }}");
141            }
142            Action::WaitMs(ms) => println!("action: WaitMs({ms})"),
143            // `Action` is `#[non_exhaustive]` — a future variant is simply
144            // not printed by this demo, not a compile break.
145            _ => {}
146        }
147    }
148    assert!(
149        saw_blocking_reload,
150        "this crate's own origin renderer defaults CAN-BLOCK-RELOAD=YES, so the \
151         next reload the client schedules must be a blocking one"
152    );
153}
Source

pub fn next_output(&mut self) -> Option<Output>

Drain the next Output event, if any.

Source

pub fn on_playlist(&mut self, bytes: &[u8]) -> Result<()>

Feed a freshly fetched Media Playlist response.

§Errors

Error::PlaylistNotUtf8 / Error::PlaylistParse on malformed input.

Examples found in repository?
examples/client_stepping.rs (line 112)
86fn main() {
87    let playlist = canned_playlist();
88    println!("--- canned media.m3u8 ---\n{playlist}");
89
90    let mut client = LlHlsClient::new(PLAYLIST_URL);
91
92    // The client always seeds a plain (non-blocking) GET first — it hasn't
93    // seen a playlist yet, so it doesn't know the origin supports blocking
94    // reload.
95    match client.poll() {
96        Some(Action::FetchPlaylist {
97            url,
98            blocking,
99            skip,
100        }) => {
101            assert_eq!(url, PLAYLIST_URL);
102            assert!(blocking.is_none());
103            assert!(!skip);
104            println!("action: FetchPlaylist {{ url: {url:?}, blocking: None }}");
105        }
106        other => panic!("expected the seeded FetchPlaylist, got {other:?}"),
107    }
108
109    // Feed the canned playlist in response to that (imagined) GET — no HTTP
110    // client is ever involved.
111    client
112        .on_playlist(playlist.as_bytes())
113        .expect("the canned playlist parses");
114
115    // Drain every action the client now wants performed: the closed
116    // segment's bytes, the open segment's landed part, the init segment
117    // (from `#EXT-X-MAP`), the preload-hinted next part, and finally a
118    // Blocking Playlist Reload naming the next Media Sequence Number/part.
119    let mut saw_blocking_reload = false;
120    while let Some(action) = client.poll() {
121        match &action {
122            Action::FetchResource { id, url, .. } => {
123                println!("action: FetchResource {{ id: {id:?}, url: {url:?} }}");
124            }
125            Action::FetchPlaylist {
126                url,
127                blocking: Some(b),
128                ..
129            } => {
130                println!(
131                    "action: FetchPlaylist {{ url: {url:?}, blocking: {b:?} }}  <- blocking reload"
132                );
133                saw_blocking_reload = true;
134            }
135            Action::FetchPlaylist {
136                url,
137                blocking: None,
138                ..
139            } => {
140                println!("action: FetchPlaylist {{ url: {url:?}, blocking: None }}");
141            }
142            Action::WaitMs(ms) => println!("action: WaitMs({ms})"),
143            // `Action` is `#[non_exhaustive]` — a future variant is simply
144            // not printed by this demo, not a compile break.
145            _ => {}
146        }
147    }
148    assert!(
149        saw_blocking_reload,
150        "this crate's own origin renderer defaults CAN-BLOCK-RELOAD=YES, so the \
151         next reload the client schedules must be a blocking one"
152    );
153}
Source

pub fn on_resource(&mut self, id: ResourceId, bytes: &[u8]) -> Result<()>

Feed the bytes fetched for a previously requested ResourceId (init/part/segment). Part/Segment resources delivered before the init segment are buffered internally and demuxed once the init arrives — the caller’s fetches may complete in any order.

§Errors

Error::UnrequestedResource if id was never requested (the requested bookkeeping — or, for Init, init_uri — has no record of it): a caller/driver bug, or a stale/duplicate delivery after the client already moved past this id. Error::Demux if transmux::Fmp4Demux rejects the concatenation of the cached init + bytes.

Source

pub fn on_error(&mut self, id: Option<ResourceId>)

Report that a previously requested ResourceId (or the playlist itself, via None) failed. Clears the id’s “requested” bookkeeping so the next Self::on_playlist call naturally re-requests it (no automatic retry timer — the caller drives retry cadence).

Trait Implementations§

Source§

impl Debug for LlHlsClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more