Skip to main content

hls_runtime/client/
engine.rs

1//! [`HlsClient`] — the sans-IO caller-driven engine.
2
3use alloc::collections::{BTreeMap, BTreeSet, VecDeque};
4use alloc::string::{String, ToString};
5use alloc::vec::Vec;
6
7use broadcast_common::Unpackage;
8use broadcast_hls::{ByteRange, MapTag, MediaPlaylist, MediaSegment, OpenSegment, PreloadHintType};
9use transmux::{Fmp4Demux, TrackSpec, TsDemux};
10
11use super::action::{Action, BlockingReload, ResourceId};
12use super::error::{Error, Result};
13use super::output::Output;
14use super::url;
15
16/// First byte of every MPEG-2 TS packet (ITU-T H.222.0 / ISO/IEC 13818-1
17/// §2.4.3.2 `sync_byte`). Classic MPEG-TS-segment HLS (HLS v3, RFC 8216 —
18/// the dominant legacy/IPTV form) has no `EXT-X-MAP`/init segment at all:
19/// each `.ts` segment is a self-contained PAT/PMT/PES stream, so this byte
20/// is the only available signal to distinguish one from an fMP4/CMAF
21/// segment (which starts with an ISOBMFF box: `ftyp`/`styp`/`moof`) once the
22/// playlist itself has never advertised a Media Initialization Section.
23const TS_SYNC_BYTE: u8 = 0x47;
24
25/// A driveable, sans-IO Low-Latency HLS (RFC 8216bis) playback client.
26///
27/// `HlsClient` never touches a socket or a clock. The caller drives it:
28///
29/// 1. [`HlsClient::new`] seeds the first [`Action::FetchPlaylist`]; drain it
30///    with [`HlsClient::poll`] and perform the GET.
31/// 2. Feed the response back with [`HlsClient::on_playlist`] (playlist) or
32///    [`HlsClient::on_resource`] (init/part/segment bytes) —
33///    [`Action::FetchResource`]'s `id` correlates the two.
34/// 3. Drain [`HlsClient::poll`] again for the next round of actions (a new
35///    reload, newly discoverable parts, a preload-hint prefetch, ...) and
36///    [`HlsClient::next_output`] for newly available [`Output`]s.
37///
38/// # Behaviour
39///
40/// - **Reload scheduling** (issue #717 slice 2): once a playlist advertises
41///   `EXT-X-SERVER-CONTROL`/`EXT-X-PART-INF` **and** the origin's
42///   `CAN-BLOCK-RELOAD` attribute is `YES`
43///   ([`broadcast_hls::LowLatencyConfig::can_block_reload`] is `true` —
44///   *not* merely [`broadcast_hls::MediaPlaylist::low_latency`] being
45///   `Some`, since an origin may carry parts/PART-INF while still
46///   advertising `CAN-BLOCK-RELOAD=NO`), every reload is a Blocking
47///   Playlist Reload (RFC 8216bis §6.2.5.2) naming the next not-yet-seen
48///   Partial Segment's `_HLS_msn`/`_HLS_part`. Otherwise reloads are plain GETs
49///   paced by an [`Action::WaitMs`] hint derived from `#EXT-X-TARGETDURATION`.
50///   `EXT-X-SKIP`/`CAN-SKIP-UNTIL` Playlist Delta Updates (RFC 8216bis §4.4.5.2)
51///   are requested once a full-playlist baseline exists, and merged back into
52///   a full view before further processing — see `merge_delta` internally.
53/// - **Fetch pipeline** (slice 3): the `EXT-X-PRELOAD-HINT`ed part is fetched
54///   ahead of its own appearance as a numbered `EXT-X-PART`; `BYTERANGE`
55///   parts are supported, including the RFC 8216bis §4.4.4.9 "omitted offset
56///   means immediately after the previous sub-range of the same resource"
57///   rule (tracked per resource URL). The Media Initialization Section
58///   (`EXT-X-MAP`) is fetched once and reused for every following resource
59///   until the map changes.
60/// - **Dedup / coalescing**: once *any* of a segment's parts have been
61///   individually fetched, that segment is never re-fetched whole — when it
62///   later closes (`#EXTINF`+URI), the client only fetches whichever of its
63///   parts (if any) are still missing, and marks the segment "delivered" once
64///   every part is accounted for (fetched, or `GAP=YES`). A playlist whose
65///   segments carry **no** parts at all (a non-LL origin) falls back to
66///   fetching the whole segment resource — the two paths never overlap for a
67///   single segment, so a part's samples are never double-counted against its
68///   parent's.
69/// - **Output adapter** (slice 4): exactly one [`Output::Init`] precedes any
70///   [`Output::Samples`]; parts/segments are demuxed via
71///   [`transmux::Fmp4Demux`] (by concatenating the cached init bytes with the
72///   fetched resource — this crate never re-implements ISOBMFF box parsing,
73///   only reuses transmux's), so `Output::Samples` carries real access units,
74///   not opaque container bytes. `#EXT-X-DISCONTINUITY` on a segment surfaces
75///   as [`Output::Discontinuity`] immediately before that segment's first
76///   samples. **Known limitation**: an in-progress ([`OpenSegment`]) segment
77///   carries no discontinuity flag of its own (only a *closed*
78///   [`MediaSegment`] does) — if every part of a segment was already
79///   delivered while it was still open, a discontinuity revealed only once it
80///   closes is signalled late (after those parts' samples, not before). This
81///   is a gap in the current wire model ([`broadcast_hls::OpenSegment`]), not
82///   something this crate can fix locally.
83/// - **Classic MPEG-TS-segment HLS** (issue #760): a playlist that never
84///   advertises an `EXT-X-MAP` (HLS v3, the dominant legacy/IPTV form —
85///   self-contained `.ts` segments carrying their own PAT/PMT/PES, no
86///   separate init resource) routes each fetched Part/Segment through
87///   [`transmux::TsDemux`] instead, content-sniffed by the MPEG-TS sync byte
88///   rather than blocked on an init fetch that will never come. The first
89///   successfully demuxed segment's recovered
90///   [`TrackSpec`]s synthesize the one [`Output::Init`] this crate's contract
91///   requires (via [`transmux::build_init_segment`]) so downstream callers
92///   (e.g. `multimux`'s `HlsPull`, which recovers track specs from
93///   `Output::Init`) need no TS-specific handling of their own. The
94///   fMP4/CMAF plus LL (parts/preload-hint) path above is entirely
95///   unchanged; the two never overlap for a single playlist.
96#[derive(Debug)]
97pub struct HlsClient {
98    playlist_url: String,
99
100    pending_actions: VecDeque<Action>,
101    pending_outputs: VecDeque<Output>,
102
103    init_uri: Option<String>,
104    init_bytes: Option<Vec<u8>>,
105    init_emitted: bool,
106    /// Part/Segment resources delivered before the init segment arrived —
107    /// buffered (in arrival order) and replayed once [`Self::init_bytes`] is
108    /// set, so the caller's fetch/response IO can complete in any order
109    /// (a real HTTP client has no reason to serialize on init-first).
110    pending_demux: VecDeque<(ResourceId, Vec<u8>)>,
111
112    requested: BTreeSet<ResourceId>,
113    delivered_parts: BTreeSet<(u64, u64)>,
114    delivered_segments: BTreeSet<u64>,
115    discontinuous_msns: BTreeSet<u64>,
116    discontinuity_emitted: BTreeSet<u64>,
117    byte_range_cursor: BTreeMap<String, u64>,
118
119    outstanding_fetches: u64,
120    saw_endlist: bool,
121    end_emitted: bool,
122    last_full_playlist: Option<MediaPlaylist>,
123}
124
125impl HlsClient {
126    /// Create a new client for the Media Playlist at `playlist_url`, seeding
127    /// the first [`Action::FetchPlaylist`] (a plain, non-blocking GET — the
128    /// client does not yet know whether the origin supports blocking reload).
129    pub fn new(playlist_url: impl Into<String>) -> Self {
130        let playlist_url = playlist_url.into();
131        let mut pending_actions = VecDeque::new();
132        pending_actions.push_back(Action::FetchPlaylist {
133            url: playlist_url.clone(),
134            blocking: None,
135            skip: false,
136        });
137        Self {
138            playlist_url,
139            pending_actions,
140            pending_outputs: VecDeque::new(),
141            init_uri: None,
142            init_bytes: None,
143            init_emitted: false,
144            pending_demux: VecDeque::new(),
145            requested: BTreeSet::new(),
146            delivered_parts: BTreeSet::new(),
147            delivered_segments: BTreeSet::new(),
148            discontinuous_msns: BTreeSet::new(),
149            discontinuity_emitted: BTreeSet::new(),
150            byte_range_cursor: BTreeMap::new(),
151            outstanding_fetches: 0,
152            saw_endlist: false,
153            end_emitted: false,
154            last_full_playlist: None,
155        }
156    }
157
158    /// The Media Playlist URL this client is following.
159    pub fn playlist_url(&self) -> &str {
160        &self.playlist_url
161    }
162
163    /// Drain the next IO [`Action`] the caller must perform, if any.
164    pub fn poll(&mut self) -> Option<Action> {
165        self.pending_actions.pop_front()
166    }
167
168    /// Drain the next [`Output`] event, if any.
169    pub fn next_output(&mut self) -> Option<Output> {
170        self.pending_outputs.pop_front()
171    }
172
173    /// Feed a freshly fetched Media Playlist response.
174    ///
175    /// # Errors
176    /// [`Error::PlaylistNotUtf8`] / [`Error::PlaylistParse`] on malformed
177    /// input.
178    pub fn on_playlist(&mut self, bytes: &[u8]) -> Result<()> {
179        let text = core::str::from_utf8(bytes)?;
180        let playlist = MediaPlaylist::parse(text)?;
181        let playlist = self.merge_delta(playlist);
182
183        for (i, seg) in playlist.segments.iter().enumerate() {
184            let msn = playlist.media_sequence + i as u64;
185            self.process_closed_segment(msn, seg)?;
186        }
187
188        let next_msn = playlist.media_sequence + playlist.segments.len() as u64;
189        if let Some(open) = &playlist.open_segment {
190            self.process_open_segment(next_msn, open)?;
191        }
192
193        // Prefer the *open* segment's map when present: it's the most
194        // recent (`#EXT-X-MAP` carries forward, so the open segment's view
195        // is never older than the last closed segment's) and, crucially, is
196        // the only way to learn the init segment's URI at all when NO
197        // segment has closed yet (issue #717 slice 5 fix — previously this
198        // only ever looked at the last *closed* segment's map, so a client
199        // tuning into a stream mid-segment couldn't fetch the init segment,
200        // and therefore couldn't demux any of that segment's parts, until
201        // it closed — needlessly inflating glass-to-glass latency by up to
202        // a full segment duration on every fresh connection).
203        let map = playlist
204            .open_segment
205            .as_ref()
206            .and_then(|o| o.map.as_ref())
207            .or_else(|| playlist.segments.last().and_then(|s| s.map.as_ref()));
208        if let Some(map) = map {
209            self.ensure_init_requested(map)?;
210        }
211
212        if let Some(ll) = &playlist.low_latency {
213            if let Some(hint_uri) = &ll.preload_hint_part {
214                match ll.preload_hint_type {
215                    PreloadHintType::Part => {
216                        let part_idx = playlist
217                            .open_segment
218                            .as_ref()
219                            .map(|o| o.parts.len() as u64)
220                            .unwrap_or(0);
221                        let id = ResourceId::Part {
222                            msn: next_msn,
223                            part: part_idx,
224                        };
225                        let url = url::resolve(&self.playlist_url, hint_uri);
226                        let byte_range = self.resolve_hint_byte_range(&url, ll);
227                        self.request_resource(id, url, byte_range);
228                    }
229                    PreloadHintType::Map => {
230                        let map = MapTag {
231                            uri: hint_uri.clone(),
232                            byte_range: ll.preload_hint_byte_range_length.map(|length| ByteRange {
233                                length,
234                                offset: ll.preload_hint_byte_range_start,
235                            }),
236                            extra_attrs: Vec::new(),
237                        };
238                        self.ensure_init_requested(&map)?;
239                    }
240                    _ => {
241                        // RFC 8216bis §4.4.5.3 defines only PART/MAP today; a
242                        // future hint type from a newer transmux is simply not
243                        // prefetched rather than treated as an error
244                        // (`PreloadHintType` is `#[non_exhaustive]`).
245                    }
246                }
247            }
248        }
249
250        if playlist.endlist {
251            self.saw_endlist = true;
252        } else {
253            // Issue #717 slice 1 fix: block only when the origin actually
254            // advertises `CAN-BLOCK-RELOAD=YES` — `low_latency.is_some()`
255            // alone is not enough (an origin sending `CAN-BLOCK-RELOAD=NO`
256            // still carries parts/PART-INF, e.g. while ramping up support).
257            let blocking = playlist
258                .low_latency
259                .as_ref()
260                .filter(|ll| ll.can_block_reload)
261                .map(|_| {
262                    let part = playlist
263                        .open_segment
264                        .as_ref()
265                        .map(|o| o.parts.len() as u64)
266                        .unwrap_or(0);
267                    BlockingReload {
268                        msn: next_msn,
269                        part: Some(part),
270                    }
271                });
272            let can_skip = playlist
273                .low_latency
274                .as_ref()
275                .and_then(|ll| ll.can_skip_until)
276                .is_some();
277            let skip = can_skip && self.last_full_playlist.is_some();
278            self.pending_actions.push_back(Action::FetchPlaylist {
279                url: self.playlist_url.clone(),
280                blocking,
281                skip,
282            });
283            if blocking.is_none() {
284                // RFC 8216 §4.3.3.1: a client SHOULD NOT reload more
285                // frequently than once per Target Duration; half that as a
286                // reasonable non-blocking poll cadence.
287                let wait_ms = (u64::from(playlist.target_duration.max(1)) * 1000) / 2;
288                self.pending_actions.push_back(Action::WaitMs(wait_ms));
289            }
290        }
291
292        if playlist.skip.is_none() {
293            self.last_full_playlist = Some(playlist);
294        }
295
296        self.maybe_emit_end_of_stream();
297        Ok(())
298    }
299
300    /// Feed the bytes fetched for a previously requested [`ResourceId`]
301    /// (`init`/part/segment). Part/Segment resources delivered before the
302    /// init segment are buffered internally and demuxed once the init
303    /// arrives — the caller's fetches may complete in any order.
304    ///
305    /// # Errors
306    /// [`Error::UnrequestedResource`] if `id` was never requested (the
307    /// `requested` bookkeeping — or, for `Init`, `init_uri` — has no record
308    /// of it): a caller/driver bug, or a stale/duplicate delivery after the
309    /// client already moved past this id.
310    /// [`Error::Demux`] if `transmux::Fmp4Demux` rejects the concatenation of
311    /// the cached init + `bytes`.
312    pub fn on_resource(&mut self, id: ResourceId, bytes: &[u8]) -> Result<()> {
313        let was_requested = match id {
314            ResourceId::Init => self.init_uri.is_some(),
315            ResourceId::Part { .. } | ResourceId::Segment { .. } => self.requested.contains(&id),
316        };
317        if !was_requested {
318            return Err(Error::UnrequestedResource { id });
319        }
320        self.outstanding_fetches = self.outstanding_fetches.saturating_sub(1);
321        match id {
322            ResourceId::Init => {
323                self.init_bytes = Some(bytes.to_vec());
324                if !self.init_emitted {
325                    self.pending_outputs.push_back(Output::Init(bytes.to_vec()));
326                    self.init_emitted = true;
327                }
328                let buffered: Vec<_> = self.pending_demux.drain(..).collect();
329                for (bid, bbytes) in buffered {
330                    self.finish_media_resource(bid, &bbytes)?;
331                }
332            }
333            ResourceId::Part { .. } | ResourceId::Segment { .. } => {
334                if self.is_ts_segment(bytes) {
335                    // Classic MPEG-TS-segment HLS (issue #760): no init
336                    // resource will ever arrive for this playlist, so demux
337                    // this self-contained TS segment straight away rather
338                    // than buffering it forever waiting for one.
339                    self.finish_ts_resource(id, bytes)?;
340                } else if self.init_bytes.is_none() {
341                    self.pending_demux.push_back((id, bytes.to_vec()));
342                } else {
343                    self.finish_media_resource(id, bytes)?;
344                }
345            }
346        }
347        self.maybe_emit_end_of_stream();
348        Ok(())
349    }
350
351    /// Demux + emit + mark-delivered for a Part/Segment resource, once the
352    /// init segment is known to be available.
353    fn finish_media_resource(&mut self, id: ResourceId, bytes: &[u8]) -> Result<()> {
354        match id {
355            ResourceId::Part { msn, part } => {
356                self.emit_discontinuity_if_needed(msn);
357                self.demux_and_emit(id, bytes)?;
358                self.delivered_parts.insert((msn, part));
359            }
360            ResourceId::Segment { msn } => {
361                self.emit_discontinuity_if_needed(msn);
362                self.demux_and_emit(id, bytes)?;
363                self.delivered_segments.insert(msn);
364            }
365            ResourceId::Init => {}
366        }
367        Ok(())
368    }
369
370    /// The classic-TS-HLS counterpart to [`Self::finish_media_resource`]:
371    /// demux + emit + mark-delivered for a self-contained MPEG-TS Part/
372    /// Segment resource — never buffered pending an init fetch, since
373    /// [`Self::is_ts_segment`] only routes here once this playlist is known
374    /// to advertise no `EXT-X-MAP` at all.
375    fn finish_ts_resource(&mut self, id: ResourceId, bytes: &[u8]) -> Result<()> {
376        match id {
377            ResourceId::Part { msn, part } => {
378                self.emit_discontinuity_if_needed(msn);
379                self.demux_and_emit_ts(id, bytes)?;
380                self.delivered_parts.insert((msn, part));
381            }
382            ResourceId::Segment { msn } => {
383                self.emit_discontinuity_if_needed(msn);
384                self.demux_and_emit_ts(id, bytes)?;
385                self.delivered_segments.insert(msn);
386            }
387            ResourceId::Init => {}
388        }
389        Ok(())
390    }
391
392    /// `true` when `bytes` should be routed to [`Self::finish_ts_resource`]
393    /// (classic MPEG-TS-segment HLS, issue #760) rather than the fMP4/CMAF
394    /// path: this playlist has never advertised an `EXT-X-MAP` (no init
395    /// fetch is outstanding or cached — [`Self::init_uri`] is `None`; by the
396    /// time any Part/Segment fetch response reaches [`Self::on_resource`],
397    /// [`Self::on_playlist`] has already fully processed the playlist that
398    /// requested it, including any map it carries, so this check is never
399    /// stale) **and** `bytes` starts with the MPEG-TS sync byte — an
400    /// fMP4/CMAF resource always starts with an ISOBMFF box
401    /// (`ftyp`/`styp`/`moof`), never [`TS_SYNC_BYTE`].
402    fn is_ts_segment(&self, bytes: &[u8]) -> bool {
403        self.init_uri.is_none() && bytes.first() == Some(&TS_SYNC_BYTE)
404    }
405
406    /// Report that a previously requested [`ResourceId`] (or the playlist
407    /// itself, via [`None`]) failed. Clears the id's "requested" bookkeeping
408    /// so the next [`Self::on_playlist`] call naturally re-requests it (no
409    /// automatic retry timer — the caller drives retry cadence).
410    pub fn on_error(&mut self, id: Option<ResourceId>) {
411        if let Some(id) = id {
412            self.outstanding_fetches = self.outstanding_fetches.saturating_sub(1);
413            match id {
414                ResourceId::Init => self.init_uri = None,
415                other => {
416                    self.requested.remove(&other);
417                }
418            }
419        }
420        self.maybe_emit_end_of_stream();
421    }
422
423    // -- internals ------------------------------------------------------
424
425    /// Reconstruct a full playlist view from an `EXT-X-SKIP` delta update
426    /// (RFC 8216bis §4.4.5.2), by splicing the skipped prefix back in from
427    /// the last full playlist this client observed. Best-effort: if there is
428    /// no cached baseline, or it doesn't cover the skipped range, the delta
429    /// is returned as-is (never an error — "at least don't break").
430    fn merge_delta(&self, playlist: MediaPlaylist) -> MediaPlaylist {
431        let Some(skip) = &playlist.skip else {
432            return playlist;
433        };
434        if skip.skipped_segments == 0 {
435            return playlist;
436        }
437        let Some(prev) = &self.last_full_playlist else {
438            return playlist;
439        };
440        if playlist.media_sequence < prev.media_sequence {
441            return playlist;
442        }
443        let prefix_start = (playlist.media_sequence - prev.media_sequence) as usize;
444        let prefix_end = prefix_start + skip.skipped_segments as usize;
445        let Some(prefix) = prev.segments.get(prefix_start..prefix_end) else {
446            return playlist;
447        };
448        let mut merged = playlist;
449        let mut segments = prefix.to_vec();
450        segments.extend(merged.segments);
451        merged.segments = segments;
452        merged
453    }
454
455    fn process_closed_segment(&mut self, msn: u64, seg: &MediaSegment) -> Result<()> {
456        if seg.discontinuous {
457            self.discontinuous_msns.insert(msn);
458        }
459        if self.delivered_segments.contains(&msn) {
460            return Ok(());
461        }
462        if seg.parts.is_empty() {
463            // Either a genuinely non-LL segment (never had parts), OR an LL
464            // segment whose parts were already fetched individually while it
465            // was still open and whose *closed* rendering simply omits them
466            // — RFC 8216bis does not require a closed segment to keep
467            // listing `#EXT-X-PART` lines, and real origins commonly don't
468            // (e.g. `multimux`'s: `MediaSegment.parts` is always empty for a
469            // closed segment; only the still-open segment carries parts).
470            // Detect the latter via `delivered_parts`: if any part for this
471            // `msn` was ever delivered, every one of its non-`GAP` parts was
472            // already requested while it was open (`process_open_segment`
473            // requests every known part each time it's polled, so by the
474            // time the segment closes none can have been missed) — fetching
475            // the whole segment *as well* would demux and emit its samples a
476            // second time. Caught by `hls-runtime/tests/glass_to_glass.rs`
477            // (issue #717 slice 5): every sample was double-delivered for
478            // the first two segments of a real, live-paced run.
479            let already_have_parts = self
480                .delivered_parts
481                .range((msn, 0)..(msn + 1, 0))
482                .next()
483                .is_some();
484            if already_have_parts {
485                self.delivered_segments.insert(msn);
486                return Ok(());
487            }
488            let id = ResourceId::Segment { msn };
489            if !self.requested.contains(&id) {
490                let url = url::resolve(&self.playlist_url, &seg.uri);
491                let byte_range = self.resolve_byte_range(&url, &seg.byte_range);
492                self.request_resource(id, url, byte_range);
493            }
494            return Ok(());
495        }
496
497        let mut fully_accounted = true;
498        for (i, part) in seg.parts.iter().enumerate() {
499            let i = i as u64;
500            if part.gap || self.delivered_parts.contains(&(msn, i)) {
501                continue;
502            }
503            fully_accounted = false;
504            let id = ResourceId::Part { msn, part: i };
505            if !self.requested.contains(&id) {
506                let url = url::resolve(&self.playlist_url, &part.uri);
507                let byte_range = self.resolve_byte_range(&url, &part.byte_range);
508                self.request_resource(id, url, byte_range);
509            }
510        }
511        if fully_accounted {
512            self.delivered_segments.insert(msn);
513        }
514        Ok(())
515    }
516
517    fn process_open_segment(&mut self, msn: u64, open: &OpenSegment) -> Result<()> {
518        for (i, part) in open.parts.iter().enumerate() {
519            let i = i as u64;
520            if part.gap || self.delivered_parts.contains(&(msn, i)) {
521                continue;
522            }
523            let id = ResourceId::Part { msn, part: i };
524            if !self.requested.contains(&id) {
525                let url = url::resolve(&self.playlist_url, &part.uri);
526                let byte_range = self.resolve_byte_range(&url, &part.byte_range);
527                self.request_resource(id, url, byte_range);
528            }
529        }
530        Ok(())
531    }
532
533    fn ensure_init_requested(&mut self, map: &MapTag) -> Result<()> {
534        let url = url::resolve(&self.playlist_url, &map.uri);
535        if self.init_uri.as_deref() == Some(url.as_str()) {
536            return Ok(());
537        }
538        self.init_uri = Some(url.clone());
539        self.init_bytes = None;
540        self.init_emitted = false;
541        let byte_range = self.resolve_byte_range(&url, &map.byte_range);
542        self.pending_actions.push_back(Action::FetchResource {
543            id: ResourceId::Init,
544            url,
545            byte_range,
546        });
547        self.outstanding_fetches += 1;
548        Ok(())
549    }
550
551    fn request_resource(&mut self, id: ResourceId, url: String, byte_range: Option<(u64, u64)>) {
552        self.requested.insert(id);
553        self.outstanding_fetches += 1;
554        self.pending_actions.push_back(Action::FetchResource {
555            id,
556            url,
557            byte_range,
558        });
559    }
560
561    /// Resolve a `PartSpec`/`MediaSegment`/`MapTag` `BYTERANGE` into an
562    /// absolute `(offset, length)`, honouring the "omitted offset continues
563    /// the previous sub-range of the same resource" rule (tracked per
564    /// resolved URL).
565    fn resolve_byte_range(&mut self, url: &str, br: &Option<ByteRange>) -> Option<(u64, u64)> {
566        let br = br.as_ref()?;
567        let offset = br
568            .offset
569            .unwrap_or_else(|| *self.byte_range_cursor.get(url).unwrap_or(&0));
570        self.byte_range_cursor
571            .insert(url.to_string(), offset + br.length);
572        Some((offset, br.length))
573    }
574
575    fn resolve_hint_byte_range(
576        &mut self,
577        url: &str,
578        ll: &broadcast_hls::LowLatencyConfig,
579    ) -> Option<(u64, u64)> {
580        let length = ll.preload_hint_byte_range_length?;
581        let br = ByteRange {
582            length,
583            offset: ll.preload_hint_byte_range_start,
584        };
585        self.resolve_byte_range(url, &Some(br))
586    }
587
588    fn demux_and_emit(&mut self, id: ResourceId, bytes: &[u8]) -> Result<()> {
589        let init = self
590            .init_bytes
591            .as_ref()
592            .ok_or(Error::InitNotYetAvailable { id })?;
593        let mut combined = Vec::with_capacity(init.len() + bytes.len());
594        combined.extend_from_slice(init);
595        combined.extend_from_slice(bytes);
596        let mut demux = Fmp4Demux::new();
597        let media = demux
598            .unpackage(combined.as_slice())
599            .map_err(|source| Error::Demux { id, source })?;
600        for track in media.tracks {
601            if !track.samples.is_empty() {
602                self.pending_outputs.push_back(Output::Samples {
603                    track_id: track.spec.track_id,
604                    samples: track.samples,
605                });
606            }
607        }
608        Ok(())
609    }
610
611    /// The classic-TS-HLS counterpart to [`Self::demux_and_emit`]: demux a
612    /// self-contained MPEG-TS Part/Segment resource via [`TsDemux`] directly
613    /// (no init bytes to concatenate — each `.ts` segment carries its own
614    /// PAT/PMT/PES). On the very first such resource this client demuxes,
615    /// also synthesizes the one [`Output::Init`] the crate's output contract
616    /// requires ("exactly one `Init` precedes any `Samples`") from the
617    /// recovered [`TrackSpec`]s via [`transmux::build_init_segment`] — a real
618    /// `ftyp`+fragmented-`moov`, byte-for-byte demuxable by
619    /// `transmux::Fmp4Demux` like any other init segment, so callers built
620    /// against the fMP4 path (e.g. `multimux`'s `HlsPull`, which recovers
621    /// track specs from `Output::Init`) need no TS-specific handling.
622    fn demux_and_emit_ts(&mut self, id: ResourceId, bytes: &[u8]) -> Result<()> {
623        let mut demux = TsDemux::new();
624        let media = demux
625            .demux(bytes)
626            .map_err(|source| Error::Demux { id, source })?;
627        if !self.init_emitted {
628            let specs: Vec<TrackSpec> = media.tracks.iter().map(|t| t.spec.clone()).collect();
629            let init_bytes = transmux::build_init_segment(&specs, media.movie_timescale)
630                .map_err(|source| Error::Demux { id, source })?;
631            self.pending_outputs.push_back(Output::Init(init_bytes));
632            self.init_emitted = true;
633        }
634        for track in media.tracks {
635            if !track.samples.is_empty() {
636                self.pending_outputs.push_back(Output::Samples {
637                    track_id: track.spec.track_id,
638                    samples: track.samples,
639                });
640            }
641        }
642        Ok(())
643    }
644
645    fn emit_discontinuity_if_needed(&mut self, msn: u64) {
646        if self.discontinuous_msns.contains(&msn) && !self.discontinuity_emitted.contains(&msn) {
647            self.pending_outputs.push_back(Output::Discontinuity);
648            self.discontinuity_emitted.insert(msn);
649        }
650    }
651
652    fn maybe_emit_end_of_stream(&mut self) {
653        if self.saw_endlist && !self.end_emitted && self.outstanding_fetches == 0 {
654            self.pending_outputs.push_back(Output::EndOfStream);
655            self.end_emitted = true;
656        }
657    }
658}
659
660#[cfg(test)]
661mod tests {
662    use super::*;
663
664    // Regression: `on_resource` documents (see `Error::UnrequestedResource`)
665    // that it rejects a `ResourceId` the client never requested, but
666    // previously never actually checked — any bytes for any id (a
667    // caller/driver bug, or a stale/duplicate delivery) were silently
668    // accepted. Must FAIL if that check is ever removed.
669    #[test]
670    fn on_resource_rejects_a_never_requested_id() {
671        let mut client = HlsClient::new("http://example.com/playlist.m3u8");
672        let id = ResourceId::Segment { msn: 0 };
673
674        let err = client
675            .on_resource(id, b"some bytes")
676            .expect_err("an id the client never requested must be rejected");
677        assert!(
678            matches!(err, Error::UnrequestedResource { id: got } if got == id),
679            "wrong error variant: {err:?}"
680        );
681
682        // Init is checked too (tracked via `init_uri` rather than
683        // `requested`, since it's never inserted into that set).
684        let err = client
685            .on_resource(ResourceId::Init, b"init bytes")
686            .expect_err("an unrequested Init must be rejected");
687        assert!(
688            matches!(
689                err,
690                Error::UnrequestedResource {
691                    id: ResourceId::Init
692                }
693            ),
694            "wrong error variant: {err:?}"
695        );
696    }
697
698    // The flip side of the regression above: a `ResourceId` the client
699    // actually asked for (via its own internal `request_resource`
700    // bookkeeping, mirroring what a real `poll()`-driven fetch populates)
701    // must still be accepted, not spuriously rejected.
702    #[test]
703    fn on_resource_accepts_a_previously_requested_id() {
704        let mut client = HlsClient::new("http://example.com/playlist.m3u8");
705        let id = ResourceId::Segment { msn: 0 };
706        client.request_resource(id, "http://example.com/seg0.m4s".to_string(), None);
707
708        // No init segment cached yet, so this is buffered rather than
709        // demuxed — the point here is only that it is *not* rejected as
710        // unrequested.
711        let result = client.on_resource(id, b"some bytes");
712        assert!(
713            result.is_ok(),
714            "a requested id must be accepted: {result:?}"
715        );
716        assert!(
717            client.pending_demux.iter().any(|(bid, _)| *bid == id),
718            "expected the resource to be buffered pending the init segment"
719        );
720    }
721
722    // Issue #760: classic MPEG-TS-segment HLS routing. `is_ts_segment` must
723    // say yes to a genuine TS resource (sync byte, no map ever seen)...
724    #[test]
725    fn is_ts_segment_true_when_no_map_seen_and_sync_byte_present() {
726        let client = HlsClient::new("http://example.com/playlist.m3u8");
727        assert!(client.is_ts_segment(&[TS_SYNC_BYTE, 0x40, 0x11, 0x00]));
728    }
729
730    // ...but say no to an ISOBMFF (fMP4/CMAF) resource even when no map has
731    // been seen yet — the content itself is never TS, so it must fall
732    // through to the ordinary init-buffering path rather than being
733    // misrouted into `TsDemux` (which would reject it as malformed TS).
734    #[test]
735    fn is_ts_segment_false_for_an_isobmff_resource_with_no_map_seen() {
736        let client = HlsClient::new("http://example.com/playlist.m3u8");
737        let ftyp_box = b"\x00\x00\x00\x18ftypiso5\x00\x00\x02\x00iso5iso6mp41";
738        assert!(!client.is_ts_segment(ftyp_box));
739    }
740
741    // The playlist signal takes precedence over content-sniffing: once this
742    // playlist is known to advertise an `EXT-X-MAP` (an init fetch has been
743    // requested/cached), even a resource whose first byte happens to be
744    // `0x47` must NOT be misrouted through `TsDemux` -- it is that
745    // playlist's own fMP4/CMAF init + part/segment concatenation the
746    // fetched bytes belong with.
747    #[test]
748    fn is_ts_segment_false_once_a_map_has_been_requested() {
749        let mut client = HlsClient::new("http://example.com/playlist.m3u8");
750        client
751            .ensure_init_requested(&MapTag {
752                uri: "init.mp4".to_string(),
753                byte_range: None,
754                extra_attrs: Vec::new(),
755            })
756            .expect("ensure_init_requested succeeds");
757        assert!(!client.is_ts_segment(&[TS_SYNC_BYTE, 0x40, 0x11, 0x00]));
758    }
759}