SDL: Fixed infinite recursion when the microphone is unavailable

From 932bb968d7a6261a517bc10bc487d622c032b0ed Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 20 Nov 2024 16:07:43 -0800
Subject: [PATCH] Fixed infinite recursion when the microphone is unavailable

---
 src/audio/coreaudio/SDL_coreaudio.m | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index 9b6b81984a84a..569499854bdc1 100644
--- a/src/audio/coreaudio/SDL_coreaudio.m
+++ b/src/audio/coreaudio/SDL_coreaudio.m
@@ -436,7 +436,12 @@ static bool UpdateAudioSession(SDL_AudioDevice *device, bool open, bool allow_pl
                 }
             }
         } else if (data.playback && data.recording) {
-            category = AVAudioSessionCategoryPlayAndRecord;
+            if (allow_playandrecord) {
+                category = AVAudioSessionCategoryPlayAndRecord;
+            } else {
+                // We already failed play and record with AVAudioSessionErrorCodeResourceNotAvailable
+                return false;
+            }
         } else if (data.recording) {
             category = AVAudioSessionCategoryRecord;
         }
@@ -494,12 +499,15 @@ static bool UpdateAudioSession(SDL_AudioDevice *device, bool open, bool allow_pl
             if (![session setActive:YES error:&err]) {
                 if ([err code] == AVAudioSessionErrorCodeResourceNotAvailable &&
                     category == AVAudioSessionCategoryPlayAndRecord) {
-                    return UpdateAudioSession(device, open, false);
+                    if (UpdateAudioSession(device, open, false)) {
+                        return true;
+                    } else {
+                        return SDL_SetError("Could not activate Audio Session: Resource not available");
+                    }
                 }
 
                 NSString *desc = err.description;
-                SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String);
-                return false;
+                return SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String);
             }
             session_active = true;
             ResumeAudioDevices();