8000 AAudio: Add an option to allocate a session ID by reybits · Pull Request #1187 · mackron/miniaudio · GitHub
[go: up one dir, main page]

Skip to content

AAudio: Add an option to allocate a session ID - #1187

Open
reybits wants to merge 1 commit into
mackron:devfrom
reybits:aaudio-session-id
Open

reybits wants to merge 1 commit into
mackron:devfrom
reybits:aaudio-session-id

Conversation

@reybits
@reybits reybits commented Nov 2, 2026
Copy link
Copy Markdown

Summary

Adds aaudio.allocateSessionId to ma_device_config. When set, the stream is opened with
AAUDIO_SESSION_ID_ALLOCATE. AAudio never uses the MMAP path for a stream with a session ID
(AudioStreamBuilder::build(): "MMAP not used because sessionId specified"), while a low latency
stream still gets a FAST track on the legacy path (AudioStreamTrack::open()).

Why

We need a low latency stream that stays off MMAP. On a Samsung Galaxy Z Fold5 (Android 17) an MMAP
stream fails to start after a system sound with haptics (see #1184): the HAL cannot route it and
AAudio returns AAUDIO_ERROR_DISCONNECTED after about a second. The conservative profile avoids
MMAP too, but Samsung's audio policy sends that stream to its deep buffer output, about 0.2 s of
latency, audible on every button press.

With a session ID the stream gets AUDIO_OUTPUT_FLAG_FAST on the low latency output, and launches
in that state play normally. AudioFlinger grants a FAST track only when the stream runs at the
output's sample rate, so we open the device with sampleRate = 0.

Details

  • allocateSessionId is stored in ma_device.aaudio and carried into the config rebuilt in
    ma_device_reinit__aaudio(), like the other AAudio settings (see AAudio: allowSetBufferCapacity is not preserved across a reroute, so the configured buffer size is silently lost #1158), so a reroute does not
    bring MMAP back.
  • AAudioStreamBuilder_setSessionId() is API 28. It is loaded with the other functions and skipped
    when missing. With MA_NO_RUNTIME_LINKING it is linked directly, next to
    AAudioStreamBuilder_setUsage(), which is API 28 as well.
  • The option is documented next to aaudio.noAutoStartAfterReroute.

Testing

  • Samsung Galaxy Z Fold5 (SM-F946B, Android 17): logcat shows MMAP not used because sessionId specified, AUDIO_OUTPUT_FLAG_FAST successful and got Legacy ... perf = LL. Three launches
    right after a system sound with haptics all played; without the option 5 out of 5 such launches
    failed.
  • Compiles as C89 for Android (NDK 28, API 29, -std=c89 -Wall -Wextra -Wpedantic), with and
    without MA_NO_RUNTIME_LINKING, with no new warnings.
  • Not tested on a device yet: a reroute (headphones, Bluetooth).

dev-0.12 has aaudio.performanceMode but no session ID either. Happy to port this there if you
want it.

AAudio never uses the MMAP path for a stream with a session ID, while a
low latency stream still gets a FAST track on the legacy path. This
gives a way to stay off MMAP without the conservative profile, which on
some devices sends the stream to a deep buffer output with a high
latency.

The option is carried into the config rebuilt on a reroute, like the
other AAudio settings.
Copilot AI balanced review requested due to automatic review settings November 2, 2026 18:58
Copilot AI left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

A critical active finding remains because miniaudio.h assigns AAudioStreamBuilder_setSessionId unconditionally under MA_NO_RUNTIME_LINKING, which can fail to compile on Android targets below API 28 due to the symbol not being declared without an API-level guard.

1 open issue
What changed in this PR

Adds an AAudio-specific configuration flag to request allocation of an Android audio session ID, enabling low-latency playback while staying on the legacy (non-MMAP) path for improved robustness on affected devices.

Changes:

  • Added ma_device_config.aaudio.allocateSessionId and persisted it through ma_device state and the AAudio reroute reinit path.
  • Added runtime/dynamic loading support for AAudioStreamBuilder_setSessionId() and applied it during stream builder configuration when requested.
  • Documented the new option alongside other AAudio device configuration settings.
File Description
miniaudio.h Updated as part of this pull request.

Review effort: Balanced

Comment thread miniaudio.h
pContext->aaudio.AAudioStreamBuilder_setUsage = (ma_proc)AAudioStreamBuilder_setUsage;
pContext->aaudio.AAudioStreamBuilder_setContentType = (ma_proc)AAudioStreamBuilder_setContentType;
pContext->aaudio.AAudioStreamBuilder_setInputPreset = (ma_proc)AAudioStreamBuilder_setInputPreset;
pContext->aaudio.AAudioStreamBuilder_setSessionId = (ma_proc)AAudioStreamBuilder_setSessionId;

This branch has not been deployed

No deployments
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.

2 participants

0