openslES: OpenSL ES on Android only supports two formats, limit to that.

(Three with the floating point extension, which we use.)

This is according to:

https://developer.android.com/ndk/guides/audio/opensl/opensl-for-android

Previously, this would accept a request for Sint8 or Sint32 and disaster
would ensue.

Fixes #13779.
This commit is contained in:
Ryan C. Gordon
2025-09-01 10:21:55 -04:00
parent 4587bc385e
commit 0b2a003a35

View File

@@ -426,28 +426,29 @@ static void OPENSLES_DestroyPCMPlayer(SDL_AudioDevice *device)
static bool OPENSLES_CreatePCMPlayer(SDL_AudioDevice *device) static bool OPENSLES_CreatePCMPlayer(SDL_AudioDevice *device)
{ {
/* If we want to add floating point audio support (requires API level 21) /* according to https://developer.android.com/ndk/guides/audio/opensl/opensl-for-android,
it can be done as described here: Android's OpenSL ES only supports Uint8 and _littleendian_ Sint16.
https://developer.android.com/ndk/guides/audio/opensl/android-extensions.html#floating-point (and float32, with an extension we use, below.) */
*/
if (SDL_GetAndroidSDKVersion() >= 21) { if (SDL_GetAndroidSDKVersion() >= 21) {
const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(device->spec.format); const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(device->spec.format);
SDL_AudioFormat test_format; SDL_AudioFormat test_format;
while ((test_format = *(closefmts++)) != 0) { while ((test_format = *(closefmts++)) != 0) {
if (SDL_AUDIO_ISSIGNED(test_format)) { switch (test_format) {
case SDL_AUDIO_U8:
case SDL_AUDIO_S16LE:
case SDL_AUDIO_F32:
break; break;
}
} }
if (!test_format) { if (!test_format) {
// Didn't find a compatible format : // Didn't find a compatible format :
LOGI("No compatible audio format, using signed 16-bit audio"); LOGI("No compatible audio format, using signed 16-bit LE audio");
test_format = SDL_AUDIO_S16; test_format = SDL_AUDIO_S16LE;
} }
device->spec.format = test_format; device->spec.format = test_format;
} else { } else {
// Just go with signed 16-bit audio as it's the most compatible // Just go with signed 16-bit audio as it's the most compatible
device->spec.format = SDL_AUDIO_S16; device->spec.format = SDL_AUDIO_S16LE;
} }
// Update the fragment size as size in bytes // Update the fragment size as size in bytes