Constrain ALSA buffer size and start_threshold in fallback path#1703
Open
honza wants to merge 1 commit intolibrespot-org:devfrom
Open
Constrain ALSA buffer size and start_threshold in fallback path#1703honza wants to merge 1 commit intolibrespot-org:devfrom
honza wants to merge 1 commit intolibrespot-org:devfrom
Conversation
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 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.
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.