Skip to content

AVIOReader falls back to streaming mode when the origin rejects HEAD, breaking seekable playback #8

Closed

Description

@Delarkz

Summary

When the source URL is fronted by an origin that rejects HEAD, AVIOReader.probeFileSize() logs:

[AVIOReader] HEAD failed (HTTP 405) → streaming mode

…and falls back to streaming mode (sequential GET, no Range). In streaming mode AVIOReader.seek returns -1 for SEEK_SET / SEEK_END whenever fileSize < 0, which means:

  • Containers whose demuxer seeks on open fail to load. MKV / WebM place Cues (seek index) at the end; non-faststart MP4 / MOV place moov at the end; AVI places its index at the end. FFmpeg's demuxer issues avio_seek(SEEK_END) inside avformat_open_input for these, so the open fails (or degrades).
  • Even containers that open successfully (fragmented MP4 with moov at front, HLS-style sources, MPEG-TS) become unscrubbable — SEEK_SET returns -1, so any user-initiated timeline seek is rejected.

So streaming mode is a strict regression for any source that's actually seekable on the server — which the HEAD-rejecting origins observed here are.

Reproduction

Observed with an AIOStreams URL that 307-redirects to a tb-cdn.st origin. The origin rejects HEAD but accepts Range GET:

$ curl -I '<aiostreams URL>'
HTTP/2 307                              # redirects to origin

$ curl -r 0-0 -I '<redirected origin URL>'
HTTP/1.1 405 Method Not Allowed         # origin rejects HEAD

$ curl -L -r 0-0 -s -o /dev/null -D - '<aiostreams URL>'
HTTP/1.1 206 Partial Content            # but accepts Range GET
Content-Range: bytes 0-0/<TOTAL>

Likely affects any other source that proxies to the same origin family. Progressive / fragmented MP4 sources (YouTube etc.) still open and play, but the user loses the ability to scrub.

Fix

Replace the HEAD probe with a Range: bytes=0-0 GET that cancels in didReceive response (so the body never streams). Fall back to HEAD if the Range probe doesn't yield a size. Three details matter, all of which were necessary to make this work end-to-end against a real Cloudflare-fronted CDN:

  1. Explicit redirect handler that preserves the Range header. URLSession's default willPerformHTTPRedirection behavior drops custom request headers on cross-host redirects. Without an explicit handler the redirected GET arrives at the origin without Range, and the origin (here AIOStreams' Express proxy) returns 400 Bad Request. Implement the delegate method and copy task.originalRequest?.value(forHTTPHeaderField: "Range") onto the new request.

  2. Per-task delegate on the existing URLSession, not a fresh URLSession per probe. Creating an ephemeral session per probe forces a new TLS handshake every call and varies the TLS fingerprint, which makes Cloudflare-fronted origins intermittently slow or bot-flagged on repeat probes. iOS 15+ allows task.delegate = ... for per-task delegate callbacks while reusing the session's connection pool.

  3. Generous timeout. The redirect chain (proxy → CDN) plus a cold TLS handshake can take 5–10 s under load. A 5 s request.timeoutInterval rejects healthy responses; 20 s leaves comfortable headroom.

This is also how AVPlayer / QuickTime probe file size, and matches the existing StreamingDelegate pattern in the same file (stateless let-only delegate, satisfies strict concurrency).

Optional follow-up

AetherEngine.load(url:) opens a probe Demuxer and an HLSVideoEngine Demuxer against the same URL, so probeFileSize() runs twice per load. Caching the result keyed by URL would halve the probe traffic and reduce the chance of hitting upstream rate-limits on retried playback attempts.

Happy to open a PR.

Activity

  1. superuser404notfound commented on May 22, 2026

    @superuser404notfound
    Owner

    Fixed in 83a6d21, shipped to Sodalite pin in 5d36129f. Implemented the spec end-to-end:

    • Range bytes=0-0 GET cancelled in didReceive response so the body never streams. Parses total size from Content-Range: bytes 0-0/<TOTAL> on 206, also accepts plain 2xx with positive Content-Length for origins that ignore Range entirely.
    • Explicit willPerformHTTPRedirection handler on the task delegate that re-sets Range and the caller-supplied extra headers on the redirected request. Without this URLSession's default strips them on cross-host redirect.
    • Long-lived static probeSession (separate from the per-request session pattern used in syncRequest for chunk-fetch leak control). AetherEngine.load() probes twice per call (probe Demuxer + session Demuxer); the shared session keeps the TLS connection warm + fingerprint stable across the pair, which avoids the bot-flag scenario you described.
    • 20 s timeout on the Range probe, 25 s on the semaphore wait, falls back to the original HEAD probe if Range doesn't yield a size (kept for live-transcode endpoints that aren't ready to answer either probe immediately).

    The follow-up about double-probing per load: still on my list. I want to cache the size keyed by URL + extra-headers hash inside AetherEngine so the second Demuxer.open(url:) skips the probe entirely. Probably ships in a separate commit after I review the lifecycle of extraHeaders (they can change mid-session for auth-token refresh on long-form Jellyfin sessions, and the cache key needs to invalidate cleanly when they do).

    Thanks for the detailed writeup, the three-part requirement set saved a lot of debugging the Cloudflare interactions from scratch.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions