Skip to content

Constrain ALSA buffer size and start_threshold in fallback path#1703

Open
honza wants to merge 1 commit into
librespot-org:devfrom
honza:alsa-fallback-buffer-size
Open

Constrain ALSA buffer size and start_threshold in fallback path#1703
honza wants to merge 1 commit into
librespot-org:devfrom
honza:alsa-fallback-buffer-size

Conversation

@honza

@honza honza commented Apr 15, 2026

Copy link
Copy Markdown

When buffer/period negotiation fails and we fall back to the device's defaults, some devices (e.g. USB audio via the front: plugin) end up with enormous buffers (1M+ frames at 44.1 kHz = ~23 seconds of audio).

This causes two user-visible problems:

  1. Playback start is delayed because start_threshold is set to buffer_size - period_size, so ALSA waits for ~23 seconds of data to be written before producing any output.

  2. Track changes block because drain() must wait for the entire buffer to play out before the sink can be reopened for the next track.

Fix both by:

  • Calling set_buffer_size_near(MAX_BUFFER) on the fallback hw params so the device picks a buffer close to 500 ms instead of its unconstrained default. This is best-effort (errors are ignored) and does not change behavior for devices that already negotiate successfully.

  • Capping start_threshold to at most SAMPLE_RATE frames (1 second) so that even if the buffer is still large, playback begins promptly.

When buffer/period negotiation fails and we fall back to the device's
defaults, some devices (e.g. USB audio via the `front:` plugin) end up
with enormous buffers (1M+ frames at 44.1 kHz = ~23 seconds of audio).

This causes two user-visible problems:

1. Playback start is delayed because start_threshold is set to
   buffer_size - period_size, so ALSA waits for ~23 seconds of data
   to be written before producing any output.

2. Track changes block because drain() must wait for the entire buffer
   to play out before the sink can be reopened for the next track.

Fix both by:

- Calling set_buffer_size_near(MAX_BUFFER) on the fallback hw params
  so the device picks a buffer close to 500 ms instead of its
  unconstrained default. This is best-effort (errors are ignored) and
  does not change behavior for devices that already negotiate
  successfully.

- Capping start_threshold to at most SAMPLE_RATE frames (1 second) so
  that even if the buffer is still large, playback begins promptly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@photovoltex photovoltex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds reasonable to me. what do you think about the changes @roderickvd?

@roderickvd

Copy link
Copy Markdown
Member

The issue with pathologically large device defaults is real. Capping is a smart thing to do. 500 ms is still quite large, I'd go for 100 ms. There's an ALSA API to set this in terms of duration instead of frames, which could be easier, as you wish.

Take care with the start threshold: if it's larger than the buffer could fill up, then there's a chance that playback would never start. Ideally it's at least twice the device's period size, for double buffering.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants