Skip to content

[proposal] Buffer high-bitrate direct play to disk on Android TV instead of only the heap #497

Open

Description

@CoffeeKnyte

On Android TV, direct play of a high-bitrate file buffers far less video than the player asks for, and those plays stall more often. The limit is memory. I'd like to buffer ahead on disk instead, and I'd like other developers to tell me whether that is the right approach before anyone writes it.

What happens today

PlaybackBufferPolicy asks for 180 s of forward buffer. SiloLoadControl turns that into a byte target and clamps it to half the app heap (memoryClass / 2), capped at 192 MiB. Media3 keeps that buffer in the Java heap.

At low bitrates, the time target is what binds. At high bitrates, the byte cap does:

Heap budget Seconds buffered at 20 Mbps At 35 Mbps
96 MiB (Shield: memoryClass 192) ~40 s ~23 s
192 MiB (Google TV Streamer: memoryClass 384) ~80 s ~46 s
24 MiB (low-RAM fallback) ~10 s ~6 s

Any network dip longer than the buffer becomes a visible stall.

What we measured on a production server

We estimated each client's buffer from the server side, as bytes delivered minus media time played, and counted stalls of 15 s or longer. This covers direct play over a couple of weeks.

  • Under 12 Mbps: Android TV devices hold about 200–260 s ahead, which matches the 180 s target plus the load-control idle window.
  • 12–25 Mbps: the same devices hold about 40 s. The stall rate rose about 2–4× on a Shield and a Fire TV 4K stick, and much more on some cheaper boxes.
  • Apple TV: in the same 12–25 Mbps band its buffer is much deeper, and its stall rate stays close to its low-bitrate rate.
  • Above about 25 Mbps: every client stalls, Apple included. The link itself can't keep up there, so a bigger buffer won't fix that range.
  • One connection is enough. On the stalling plays, a single connection usually reached 1.2–7× the file's bitrate at some point. So the stalls come from dips that outlast the buffer, not from a per-connection speed limit, and opening more connections wouldn't help.

There are two caveats. Far fewer high-bitrate plays were sampled than low-bitrate ones, because the server transcodes many of them, so treat the 12–25 Mbps numbers as a direction rather than proof. And the app doesn't report memoryClass to the server, so the heap sizes come from device model, not from measurement.

Proposal

For direct play (progressive, not HLS) where the byte cap would leave the buffer well under the 180 s target, run a read-ahead to disk:

  • A SimpleCache in the app's cache dir, with an LRU evictor sized from free space.
  • The player reads through CacheDataSource, so it reads from disk whenever the range is already cached.
  • A background writer (CacheWriter or similar) fetches the range ahead of the playhead on its own connection, up to roughly the 180 s target. On a seek it restarts at the new position. When playback stops, it stops.
  • The in-heap Media3 buffer stays at its current size. The disk holds the rest.

CacheDataSource on its own only caches what the player already read, so the separate writer is the part that adds buffer depth.

Questions for reviewers

  1. Is a disk read-ahead the right tool here? Or should we first try something cheaper, such as android:largeHeap="true"? The app doesn't set it today. On many TV devices largeMemoryClass is several times memoryClass, which might be enough for 12–25 Mbps, though it trades away memory headroom and GC behaviour.
  2. Storage: many sticks ship with 8 GB of eMMC and little free space. Three minutes at 25 Mbps is about 560 MB. What floor and ceiling should the cache use, and should we skip the read-ahead below some free-space threshold?
  3. Flash wear and I/O speed: cheap eMMC can be slow and wears with repeated writes. Is writing a few hundred MB per film acceptable? Can a slow disk end up stalling playback?
  4. Interaction with existing behaviour: retries (MediaLoadRetryPolicy), the 15 s load-idle window that keeps proxies from closing idle connections, stream-token refresh, and replans that change the URL mid-play.
  5. Server side: the server already accepts parallel range requests for one session. Does anyone see a reason the second connection would cause trouble with reverse proxies or CDNs that meter requests?
  6. Anything already in Media3 that does this and that we should use instead?

If this direction holds up, the server side can then let more 12–25 Mbps files play without transcoding on Android TV.

Activity

  1. feardubin commented on Nov 10, 2026

    @feardubin
    Contributor

    Adding field data from a production server in support of this, plus a request about the Dolby Vision halving in playbackBufferBudgetBytes.

    What we see

    Direct-play sessions (>60 s played) since 2026-11-01, from plan_summary route events:

    Client Files Sessions Sessions with ≥1 stall
    Android TV Dolby Vision, >45 Mbit/s 111 (12 users) 32 (29%)
    Apple TV Dolby Vision, >45 Mbit/s 18 0
    Web Dolby Vision, >45 Mbit/s 14 0
    Android TV <20 Mbit/s ~1,000 2–3%

    Stalls on Android TV happen at all hours, including sessions where the bandwidth estimate was 164–289 Mbit/s. Typical stalls are 1–8 s each, several per episode.

    Why

    SiloLoadControl caps the forward buffer at memoryClass / 2 (max 192 MiB) and playbackBufferBudgetBytes halves it again for Dolby Vision. For a typical 4K DV remux (~64 Mbit/s average), that is roughly 6–13 s of media on 192–384 MB heaps, against a 180 s MAX_DEPTH_MS target. Remux bitrate is far from flat. One episode we measured from its MKV cues averages 63.6 Mbit/s, but its worst 10 s window is 105 Mbit/s, its worst 30 s window is 99 Mbit/s, and a third of all 10 s windows exceed 70 Mbit/s.

    So any upstream hiccup longer than a few seconds becomes a visible stall. We caught one live: the origin's storage read for one stream hung for ~45 s while every other stream and the network path were healthy. The player had 13–24 s buffered and stalled for 33 s. With a buffer of a minute or more, as other clients effectively have, short storage or network hiccups would be invisible.

    Asks

    1. Reconsider the Dolby Vision halving for direct play of high-bitrate files, or make it apply only to low-RAM devices. As it stands it puts DV remuxes, the highest-bitrate content, on the smallest time buffer.
    2. +1 to buffering high-bitrate direct play to disk (this issue). A few GB of app cache would give minutes of cushion without heap pressure.
    3. Optionally, report the computed target_bytes / depth_ms in the diagnostics bundle. Right now they only go to logcat, so admins can't see them.

    Happy to provide more data if useful.

  2. weswhet commented on Nov 13, 2026

    @weswhet

    On an NVIDIA Shield, setting android:largeHeap="true" and removing the Dolby Vision halving stopped the stalls in a high-bitrate Dolby Vision remux. The stock app stalled repeatedly with about 4 seconds of buffer. The test build played about 50 minutes with no stalls, held 13–26 seconds of buffer, and stayed well within the larger heap. @Quick104, this result bears on the halving in playbackBufferBudgetBytes, and it answers question 1 in this issue for one device.

    Test setup

    • Device: NVIDIA SHIELD TV Pro (2019), Android 11, wired gigabit Ethernet.
    • Title: a 4K Dolby Vision profile 7 FEL (full enhancement layer) remux with TrueHD Atmos 7.1, averaging about 80 Mbit/s. The server chose direct play with the client_dv7_to_dv81 transformation.
    • Stock app: Android TV 1.2.0 (31).
    • Test build: a local build with three changes:
      1. android:largeHeap="true" in the TV manifest.
      2. The memory budget computed from Runtime.getRuntime().maxMemory() instead of ActivityManager.memoryClass. The stock code reads memoryClass, which doesn't change with largeHeap, so the manifest change alone doesn't raise the budget.
      3. No Dolby Vision halving.

    Results

    Measure Stock app Test build
    Heap growth limit 192 MiB 512 MiB
    target_bytes / budget_bytes 50331648 / 50331648 (48 MiB) 144310272 / 268435456 (138 / 256 MiB)
    Buffered ahead 0.1–4.5 s; playback started at 2.2 s 13–26 s; 18.7 s after a pause
    Stalls Repeated buffering_quality event=waiting cause=network None in about 50 minutes, including the high-bitrate second half
    Java heap during playback Not measured 175–195 MiB, steady; no OutOfMemoryError

    Why the network isn't the cause

    • The server-to-Shield link measured about 1 Gbit/s, and the server sent the same file at 0.9–1.7 Gbit/s on a fresh request.
    • During stock-app playback, the TCP connection was limited by the client's receive window 77–90% of the time. In other words, the buffer was usually full and the app had stopped reading.
    • The stalls lined up with upstream read slowdowns of a few seconds. A 4-second buffer turns each slowdown into a visible stall; the test build's buffer absorbs them.

    Suggestions

    1. Scale the Dolby Vision halving with heap size. The code comment on playbackBufferBudgetBytes cites a 96 MiB target that exhausted a 192 MiB heap during a Dolby Vision profile 5 stream. In this test, a 138 MiB target in a 512 MiB heap left more than 300 MiB free. The risk appears to depend on the buffer's share of the heap rather than on Dolby Vision itself. Applying the halving only on small heaps, or not at all when largeHeap is set, keeps the original protection where it's needed.
    2. Add a heap-based floor for high-bitrate direct play. target_bytes stayed at 138 MiB of a 256 MiB budget because selectBufferSizingBitrateBps uses the file's bitrate metadata, which appears to understate this remux.
    3. Report the buffer figures in diagnostics. I agree with the earlier request to include target_bytes, budget_bytes, and the buffered-ahead time in the diagnostics bundle; today they appear only in logcat. In the test build, I added them as live rows on the player's Stats tab, which made the results easy to check on the device.

    Unrelated finding

    Seeking to the last few seconds of the file logged failure=dv7_transform_stall and quarantined client_dv7_to_dv81 for the rest of the session. This looks like end-of-stream handling rather than an actual transformation stall.

    Limitations

    These results come from one device and one title. They support trying largeHeap first but don't prove it's safe on every device. I can run a specific variant on the same Shield and title if that helps.

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