Skip to content

Open the source demuxer once and reuse that preopenedDemuxer across the pre-playback stages #72

Closed

Description

What should the engine do?

Open the source demuxer once and reuse that preopenedDemuxer across the pre-playback stages (audio pre-probe → engine.load probe → session start) instead of opening it 2–3 times. Each open re-runs avformat_open_input + avformat_find_stream_info + the size probe, so a fresh open per stage multiplies the whole pre-first-frame cost (latency and request volume) against a remote source.

Related: #70 and #68 (this multiplies their per-open cost); reducing the number of opens also lowers baseline request volume, contributing toward the #69 reconnect-storm/429 pressure.

Motivating media or use case

CDN-hosted remote VOD (MP4 / MKV over HTTP(S)), especially with "prefer English/compatible audio" enabled — which adds the audio pre-probe open on top of the load + session opens. On a high-latency or rate-limited origin the repeated opens add several seconds and extra round-trips before playback; on a throttling origin they also raise the baseline request rate. The reuse path already exists for some stages (preopenedDemuxer is threaded into HLSVideoEngine.start() / the software host); the gap is that the audio pre-probe demuxer is not carried forward, so the work is redone.

Important safety caveat (please bake into the design): reuse is only safe when the context can be repositioned to the start/resume point. By the pre-probe/load stage the AVFormatContext has consumed packets and its AVIOReader is parked mid-file (the moov/Cues region), so the reusing stage must seek it back to byte 0 (or the resume position) and start from a clean AVIO state. For forward-only sources (size-unknown streaming / live / .strm) that can't seek back, reuse should be skipped and a fresh open used. So: reuse when seekable, fall back to a fresh open otherwise.

Area

Performance

Host app / integration context

Custom / my own integration (remote Jellyfin-style CDN playback)

Would you be willing to open a PR?

No

Activity

  1. ghost added
    enhancement
    on Jun 26, 2026
  2. superuser404notfound commented on Jun 26, 2026

    @superuser404notfound
    Owner

    Shipped in 4.3.0, taking the angle of removing the reason for the extra open rather than threading a parked demuxer.

    The engine already opens the source once and reuses that probe demuxer as the session demuxer (the load-probe to session-start stage you mentioned). The remaining open was the audio pre-probe, whose job is to pick the audio track by language. New LoadOptions.preferredAudioLanguages lets the engine do that selection from its single internal probe, so the pre-probe is no longer needed:

    try await engine.load(
        url: url,
        options: LoadOptions(preferredAudioLanguages: ["en", "de"])
    )

    Resolution order is explicit audioSourceStreamIndex (still wins) > first track matching a preference in order (case-insensitive, ISO 639-1 / 639-2 B+T and English-name synonyms) > container default. The resolved index drives the played audio on the first frame on both the native and software paths. It is a behavioral no-op when the list is empty, and a probe-failed source still honors an explicit override verbatim.

    That collapses the prefer-a-language flow to a single avformat_open_input + find_stream_info + size probe, which is the latency and request-volume win you were after (and one fewer open against a rate-limited origin, alongside #69/#70).

    I went with this over carrying the parked demuxer forward because reuse there has the seek-back-to-byte-0 / clean-AVIO-state hazard you flagged, and folding the selection into the existing single open sidesteps it entirely. If your pre-probe does work beyond audio selection (for example building a track-picker UI before committing to load), the probe(url:) API already returns the full SourceProbe from one open, and an explicit demuxer-handoff (load(preopenedDemuxer:)) is still on the table as a follow-up. If that is your case, let me know what the pre-probe inspects and I will wire it.

    Sodalite now uses this on both its VOD and live paths, which also retired a post-load reselect reload that misfired on single-track live channels.

  3. superuser404notfound commented on Jun 26, 2026

    @superuser404notfound
    Owner

    Resolved in 4.3.0 (see above): rather than threading a parked demuxer through the pre-playback stages, the redundant pre-opens were removed at the source, so the demuxer opens once. Closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions