Skip to content

[v1] Automatic stored-variant selection for playback #637

Open

Description

@tomislav

Summary

When a title has several stored versions of the same edition, Silo should pick the one that gives the best playback route for the current device, output, bandwidth, quality preference, and track choice, before Playback Protocol v3 decides between direct play, remux, and transcode.

A library may hold, for example:

  • an 80 Mbps 2160p HDR Blu-ray remux with TrueHD;
  • a 20 Mbps 2160p HDR WEB-DL with E-AC3;
  • an 8 Mbps 1080p SDR WEB-DL with AC3.

Automatic selection compares complete executable routes, not files. Manual version selection stays exact.

Related: #697 asks for the same policy to move out of the clients. This issue is the server-side answer to it.

Why v1

Today the client picks a file_id before the v3 planner sees bandwidth and capabilities. The planner adapts only that file. Another stored version is tried only after certain terminal failures, including bitrate_policy_unavailable since #1322.

With the three files above:

  • a fast HDR display with a capable AVR should get the best 4K route that fits;
  • a bandwidth-limited HDR TV should get the 4K WEB-DL;
  • a 12 Mbps SDR TV should get the stored 1080p SDR WEB-DL, not a tone-mapped or transcoded 4K source;
  • TV speakers should not make lossless passthrough preferable, but a materially better video source should not lose just because its audio needs conversion.

Playback v3 is meant to make route and quality decisions on the server. Locking the 1.0 contract with the current boundary keeps avoidable transcodes and over-budget delivery, and leaves each client ranking versions on its own (Apple 741f15a, Silo-Server/silo-android#239).

Status

The dependencies named in the earlier version of this issue have landed:

#657 closed without merging. #659 is still open and touches resume behavior next to this work, not version ranking.

Initial delivery is server and web. Apple and Android adoption are separate follow-up issues.

API contract

This targets /api/v2 only. /api/v1 is frozen, and its start handler already strips allow_alternate_versions, so v1 keeps today's behavior.

No new request field. The existing allow_alternate_versions on POST /api/v2/playback/start already expresses the pinning half of this feature, and the web client and Watch Together already send it. Automatic selection adds a feature flag on top of it:

  • Server: add automatic_source_selection_v1 to ServerFeaturesV3(), so it appears in the capability response features and the decision server_features.
  • Client: opts in by sending automatic_source_selection_v1 in client_features on start and replan.

Resulting behavior:

client_features has automatic_source_selection_v1 allow_alternate_versions Behavior
no omitted or true Today's behavior, including the terminal alternate fallback.
no or yes false Pinned to file_id through start and every replan. Remux and transcode of that file still allowed.
yes omitted or true Proactively evaluate eligible stored versions and choose the best complete route.

file_id stays required as the semantic anchor. requested_media_file_id keeps the client's anchor, and effective_media_file_id reports the stored file actually used. Both already exist in the v2 response.

Replan

With automatic selection on, it runs for failure_recovery, quality_change, and output_change. track_change, seek_reanchor, and seek_failure_recovery stay on the mounted effective file. With allow_alternate_versions: false, every replan stays on the requested file.

A quality or output replan may change effective_media_file_id, but the current effective file wins an otherwise exact tie. A switch happens only when a candidate is better on a higher-priority ranking dimension.

Eligibility

The requested file is the authorization and semantic anchor. An alternate is eligible only if all of these hold:

  • same content or episode identity;
  • same edition;
  • same presentation kind, presentation group, and presentation part index;
  • visible through the active profile's library access;
  • present and successfully probed;
  • known duration within max(2 seconds, 0.1%) of the requested file;
  • compatible with explicit audio and subtitle intent.

Never cross editions, multipart boundaries, or materially different timelines.

Discovery is capped at 32 files. If discovery fails or exceeds the cap, plan the requested or current file and log a structured warning. Playback must not fail only because the optimization was unavailable.

Architecture

Run every eligible candidate through the planner and compare the results. Do not score files.

  1. The handler resolves and authorizes the requested file.
  2. With automatic selection on, it lists eligible siblings.
  3. Each candidate is preflighted, probed, and mapped to the viewer's track intent.
  4. Each candidate goes through the existing PlanPlaybackV3 with the same request time, settings, transformation registry, node capabilities, bitrate policy, and attempted-route state.
  5. A pure selector compares the resulting routes with a deterministic lexicographic rank.
  6. The existing transaction, transport startup, attempt persistence, session replacement, and rollback apply only to the winner.

Suggested ownership:

  • internal/playback/source_selection_v3.go: candidate and result types, route measurements, ranking.
  • internal/api/handlers/playback_source_selection_v3.go: sibling discovery, authorization, duration checks, probe repair, track mapping, candidate planning, fallback.
  • internal/api/handlers/playback_v3.go: call the helper from start and replan, then continue through the existing transport and persistence flow. The terminal alternate fallback stays for clients that do not opt in.

Candidate planning has no session, transport, reservation, or persistence side effects.

Ranking

Compare candidate routes in this order:

  1. playable route over terminal result;
  2. delivered bitrate: known within budget > unknown > known over budget;
  3. meets the requested quality and output without degradation;
  4. route cost: original > video-copy remux with audio or subtitle adaptation > video transcode;
  5. video fidelity within the target: native dynamic range, resolution, bit depth, and a material bitrate advantage;
  6. audio cost: copy or client-side decode > server conversion;
  7. ties: exact bitrate or size, current effective file on replan, requested file, then file ID.

This is deliberately not a weighted score. An audio preference must never outweigh a higher-priority compatibility or video decision.

For otherwise comparable same-codec video, a bitrate advantage of at least 20% is material and is compared before audio cost. Smaller differences are compared after audio cost.

Consequences:

  • a compatible stored SDR route ranks above server tone mapping for SDR output;
  • a stored direct or remux route ranks above transcoding another file to the same quality rung;
  • with equivalent 4K video, AC3 or E-AC3 can beat TrueHD or DTS when those need server conversion;
  • a materially better 4K source can still win, with its TrueHD or DTS converted or downmixed;
  • lossless audio decoded locally to PCM has no server-conversion cost;
  • passthrough support is not preferred for TV speakers.

If every candidate is terminal, return the requested file's result and reason.

Bitrate budget

There are two limits, and they behave differently:

Measure the route actually delivered:

  • original-file delivery uses the container's average bitrate. If that is missing, the value is unknown, because unselected streams are still sent;
  • remux and transcode use delivered video bitrate + selected, copied, or converted audio bitrate + 3% transport overhead;
  • if any required component is unknown, the route bitrate is unknown;
  • unknown never counts as fitting, but ranks above a route known to exceed the budget.

Do not use only the primary video track's bitrate for original-file delivery.

Audio and subtitle intent

An explicit track choice is a hard constraint, not just a track index.

Audio tracks match on canonical language, codec and profile, channel layout, normalized title, and the commentary, hearing-impaired, visually-impaired, and original-language dispositions.

With implicit audio selection, each file may offer several eligible tracks. Pick the best audio route within a file before comparing files, so an AC3 compatibility track next to TrueHD can serve TV speakers without giving up the better video file.

Subtitle tracks match on source, language, codec, forced and hearing-impaired dispositions, and normalized label.

Add the missing disposition fields and a disposition schema version to persisted audio probe metadata. Existing rows are repaired through lazy probe repair. The JSONB payload needs no SQL migration.

Available qualities

available_qualities covers all eligible candidates:

  • original stays tied to the effective file;
  • the list includes deduplicated stored resolutions plus transcode rungs any eligible candidate can execute;
  • preserves_source keeps its meaning: another stored version does not preserve the current effective file;
  • a quality change may therefore select a different effective file.

Do not list a rung unless at least one eligible candidate can execute it with current settings, capabilities, and bitrate policy.

Web

The web client fetches and caches playback capability before its first start request. When the server lists automatic_source_selection_v1, it sends that feature in client_features on every start and replan.

It keeps allow_alternate_versions omitted for ordinary play, resume, restart, autoplay, and next episode or part, and sends false for:

  • explicit version selection before playback;
  • switching versions in the player;
  • deep links that name a file;
  • a Watch Together room's selected version (already sends false).

Pass the pinning choice explicitly through the watch request and controller. Do not infer it from file_id or quality_preference=original.

If the capability lookup fails or the feature is missing, leave client_features without it and keep today's behavior.

Resume, history, and identity

  • Resume position stays keyed to the item.
  • With automatic selection, last_file_id is a soft edition and presentation hint, never a compatibility or bandwidth override.
  • last_file_id and the stored media characteristics (last_hdr, last_resolution, last_codec_video) record the effective file.
  • Requested and effective IDs stay separately available in plan, session, and attempt data.
  • Progress duration and completion use the effective file's duration. The eligibility tolerance makes the substitution safe.
  • When pinned, requested and effective are the same file.

Failure, concurrency, and observability

  • Authorization and preflight of the requested file keep their current HTTP behavior.
  • Unauthorized, missing, unprobed, duration-incompatible, or track-incompatible alternates are skipped.
  • If sibling discovery fails, plan the requested or current file and log a structured warning.
  • A transport startup failure does not start a second candidate. Failure-recovery replan owns retries.
  • Replan selection and effective-file replacement stay under the existing lock, lease, and replacement transaction.
  • Log candidate counts, exclusion reasons, winning rank dimension, requested and effective IDs, and route classes. Never log media paths.

Implementation order

  1. Feature flag, capability advertisement, and audio disposition metadata.
  2. Pure selector with exhaustive deterministic tests.
  3. Handler candidate orchestration and start and replan integration.
  4. Scope-aware qualities and persistence audit.
  5. Web capability and client_features integration.
  6. Contract fixtures (contracts/api/v2), the full matrix, docs/playback-api.md and docs/architecture/playback-protocol-v3.md, and Apple and Android follow-up issues.

Acceptance criteria

Selector

  • Table-driven tests cover every ranking dimension.
  • Input-order permutation tests prove deterministic results.
  • Current, requested, and file-ID tie rules are stable.

Three-version matrix

Fixtures:

  • 80 Mbps 2160p HDR remux + TrueHD;
  • 20 Mbps 2160p HDR WEB-DL + E-AC3;
  • 8 Mbps 1080p SDR WEB-DL + AC3.

Cases:

  • a fast HDR display with an AVR gets the highest-fidelity route that fits;
  • a bandwidth-limited HDR TV gets the 4K WEB-DL;
  • a 12 Mbps SDR TV gets the stored 1080p SDR WEB-DL, not 4K tone mapping;
  • a server remote bitrate limit of 25 Mbps makes the remux terminal and selects the WEB-DL without a failed start.

Also verify:

  • AC3 copy beats TrueHD conversion for equivalent video;
  • a same-codec video advantage of at least 20% beats the audio preference;
  • stored SDR beats tone mapping, and tone mapping remains the fallback;
  • unknown and over-budget routes order correctly;
  • edition and part scope, duration tolerance, authorization, the candidate cap, and deterministic ties;
  • every row of the behavior table above.

Handler and contract

  • Requested and effective IDs, normalized request, plan identity, session, progress, and attempt rows are correct.
  • Quality and output replans may switch files; track and seek replans stay on the effective file.
  • Explicit audio and subtitle intent is preserved across versions.
  • Discovery and probe failures fall back safely.
  • Replacement rollback stays atomic.
  • /api/v1 behavior is unchanged.
  • Capability, schemas, generated v2 fixtures, and quality lists stay compatible.

Web

  • Capability caching and the legacy fallback work.
  • Every ordinary start path sends the feature and omits allow_alternate_versions.
  • Every explicit-version path sends allow_alternate_versions: false.
  • The requested and effective version display stays correct.
  • Quality replans handle a change of effective file.

Non-goals

  • No selection across editions or parts.
  • No client-side ranking. The Apple and Android follow-ups remove their existing ranking rather than adding to it.
  • No automatic failover between candidates during transport startup.
  • No /api/v1 change.
  • No Apple or Android implementation in the first PR.
  • No replacement for the v3 planner, the feat(playback): add HDR-to-SDR tone mapping #634 tone-map policy, or the server bitrate policy.

Evidence

The design was first traced against main at edd919c5 on 2026-08-17. This revision was checked against main at b4cf819a4 on 2026-10-02: #634 and #653 merged, #657 closed, allow_alternate_versions exists on v2 start (internal/playback/protocol_v3.go) and is stripped on v1, and the server bitrate policy returns bitrate_policy_unavailable from the planner. Nothing has been implemented, and the matrix cases above are design acceptance cases that have not been run.

AI disclosure

  • Tools: Codex desktop (original draft); Claude Code (this revision).
  • Models: GPT-5 / GPT-5.6 (original draft); claude-opus-5-5 (this revision).
  • Involvement: AI-assisted repository and PR analysis, design, issue drafting, and review under maintainer direction. The original draft's review against the existing alternate fallback, requested and effective identity, atomic replan replacement, quality menus, bitrate accounting, and track semantics changed the design from file ranking to complete-route ranking. This revision moved the contract to /api/v2, replaced the proposed mode field with the existing allow_alternate_versions plus a feature flag, and added the server bitrate policy.

Activity

  1. added
    v1.0-proposed
    on Aug 14, 2026
  2. added
    triaged
    and removed
    triaged
    on Sep 20, 2026
  3. changed the title [-][v1] Automatic stored-variant selection for playback[/-] [+]Automatic stored-variant selection for playback[/+] on Sep 26, 2026
  4. changed the title [-]Automatic stored-variant selection for playback[/-] [+][bug] Playback refuses a file the server can't convert instead of trying other versions[/+] on Sep 26, 2026
  5. changed the title [-][bug] Playback refuses a file the server can't convert instead of trying other versions[/-] [+][v1] Automatic stored-variant selection for playback[/+] on Sep 26, 2026
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

    v1.0-proposed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions