SDL: aaudio: apply the buffer size after the stream opens

From 87f0e4a25fc5a4146cde74f6f6d934df36eba080 Mon Sep 17 00:00:00 2001
From: Temir Zharaspayev <[EMAIL REDACTED]>
Date: Tue, 3 Nov 2026 16:36:56 +0300
Subject: [PATCH] aaudio: apply the buffer size after the stream opens

The buffer capacity passed to the builder is only a request.
Some streams can set much higher capacity than requested. If we want to
control a sound latency, we must operate with the size of buffer after
stream has been created.

For details see
https://developer.android.com/ndk/guides/audio/aaudio/aaudio#tuning-buffers
---
 src/audio/aaudio/SDL_aaudio.c      | 29 +++++++++++++++++++++++------
 src/audio/aaudio/SDL_aaudiofuncs.h |  4 ++--
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/src/audio/aaudio/SDL_aaudio.c b/src/audio/aaudio/SDL_aaudio.c
index 69fbe773a7d56..cf0a0cf6bab02 100644
--- a/src/audio/aaudio/SDL_aaudio.c
+++ b/src/audio/aaudio/SDL_aaudio.c
@@ -359,15 +359,16 @@ static bool BuildAAudioStream(SDL_AudioDevice *device)
     ctx.AAudioStreamBuilder_setSampleRate(builder, device->spec.freq);
     ctx.AAudioStreamBuilder_setChannelCount(builder, device->spec.channels);
 
-    int32_t sample_frames;
-    if (SDL_GetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES)) {
-        sample_frames = device->sample_frames;
+    const bool sample_frames_hint = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES) != NULL;
+    int32_t requested_sample_frames;
+    if (sample_frames_hint) {
+        requested_sample_frames = device->sample_frames;
     } else {
         // Use 20 ms for the default audio buffer size
-        sample_frames = (device->spec.freq / 50);
+        requested_sample_frames = (device->spec.freq / 50);
     }
-    ctx.AAudioStreamBuilder_setBufferCapacityInFrames(builder, 2 * sample_frames); // AAudio requires that the buffer capacity is at least
-    ctx.AAudioStreamBuilder_setFramesPerDataCallback(builder, sample_frames);      // twice the size of the data callback buffer size
+    ctx.AAudioStreamBuilder_setBufferCapacityInFrames(builder, 2 * requested_sample_frames); // AAudio requires that the buffer capacity is at least
+    ctx.AAudioStreamBuilder_setFramesPerDataCallback(builder, requested_sample_frames);      // twice the size of the data callback buffer size
 
     const aaudio_direction_t direction = (recording ? AAUDIO_DIRECTION_INPUT : AAUDIO_DIRECTION_OUTPUT);
     ctx.AAudioStreamBuilder_setDirection(builder, direction);
@@ -411,6 +412,22 @@ static bool BuildAAudioStream(SDL_AudioDevice *device)
         device->sample_frames = (int)ctx.AAudioStream_getBufferCapacityInFrames(hidden->stream) / 2;
     }
 
+    if (!recording && sample_frames_hint) {
+        // It is not enough to set only capacity, buffer size handles latency.
+        // Created audio stream can have much larger capacity, than requested by builder.
+        // see https://developer.android.com/ndk/guides/audio/aaudio/aaudio#tuning-buffers
+        const int32_t burst_frames = ctx.AAudioStream_getFramesPerBurst(hidden->stream);
+        const int32_t requested_frames = SDL_max(2 * requested_sample_frames, 2 * burst_frames);
+        const int32_t actual_frames = ctx.AAudioStream_setBufferSizeInFrames(hidden->stream, requested_frames);
+        if (actual_frames < 0) {
+            SDL_Log("SDL could not set the AAudio buffer size to %d frames: %s",
+                    requested_frames, ctx.AAudio_convertResultToText(actual_frames));
+        } else {
+            SDL_Log("SDL AAudio buffer size set to %d frames (requested %d, burst %d)",
+                    actual_frames, requested_frames, burst_frames);
+        }
+    }
+
     device->spec.freq = ctx.AAudioStream_getSampleRate(hidden->stream);
     device->spec.channels = ctx.AAudioStream_getChannelCount(hidden->stream);
 
diff --git a/src/audio/aaudio/SDL_aaudiofuncs.h b/src/audio/aaudio/SDL_aaudiofuncs.h
index 6568c12b0fd78..e70338a33811e 100644
--- a/src/audio/aaudio/SDL_aaudiofuncs.h
+++ b/src/audio/aaudio/SDL_aaudiofuncs.h
@@ -58,9 +58,9 @@ SDL_PROC(aaudio_stream_state_t, AAudioStream_getState, (AAudioStream * stream))
 SDL_PROC_UNUSED(aaudio_result_t, AAudioStream_waitForStateChange, (AAudioStream * stream, aaudio_stream_state_t inputState, aaudio_stream_state_t *nextState, int64_t timeoutNanoseconds))
 SDL_PROC_UNUSED(aaudio_result_t, AAudioStream_read, (AAudioStream * stream, void *buffer, int32_t numFrames, int64_t timeoutNanoseconds))
 SDL_PROC_UNUSED(aaudio_result_t, AAudioStream_write, (AAudioStream * stream, const void *buffer, int32_t numFrames, int64_t timeoutNanoseconds))
-SDL_PROC_UNUSED(aaudio_result_t, AAudioStream_setBufferSizeInFrames, (AAudioStream * stream, int32_t numFrames))
+SDL_PROC(aaudio_result_t, AAudioStream_setBufferSizeInFrames, (AAudioStream * stream, int32_t numFrames))
 SDL_PROC_UNUSED(int32_t, AAudioStream_getBufferSizeInFrames, (AAudioStream * stream))
-SDL_PROC_UNUSED(int32_t, AAudioStream_getFramesPerBurst, (AAudioStream * stream))
+SDL_PROC(int32_t, AAudioStream_getFramesPerBurst, (AAudioStream * stream))
 SDL_PROC(int32_t, AAudioStream_getBufferCapacityInFrames, (AAudioStream * stream))
 SDL_PROC(int32_t, AAudioStream_getFramesPerDataCallback, (AAudioStream * stream))
 SDL_PROC_UNUSED(int32_t, AAudioStream_getXRunCount, (AAudioStream * stream))