coreaudio: (maybe) patched to compile on iOS.

main
Ryan C. Gordon 2023-07-22 11:26:16 -04:00
parent 4836c2db07
commit 027b9e8787
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 44 additions and 23 deletions

View File

@ -290,32 +290,30 @@ static void COREAUDIO_DetectDevices(SDL_AudioDevice **default_output, SDL_AudioD
static SDL_bool session_active = SDL_FALSE; static SDL_bool session_active = SDL_FALSE;
static void PauseAudioDevices(void) // !!! FIXME: this needs to be updated, and we need a method to access SDL_audio.c's device lists. static SDL_bool PauseOneAudioDevice(SDL_AudioDevice *device, void *userdata)
{ {
if (!open_devices) { if (device->hidden && device->hidden->audioQueue && !device->hidden->interrupted) {
return; AudioQueuePause(device->hidden->audioQueue);
}
for (int i = 0; i < num_open_devices; ++i) {
SDL_AudioDevice *device = open_devices[i];
if (device->hidden->audioQueue && !device->hidden->interrupted) {
AudioQueuePause(device->hidden->audioQueue);
}
} }
return SDL_FALSE; // keep enumerating devices until we've paused them all.
} }
static void ResumeAudioDevices(void) // !!! FIXME: this needs to be updated, and we need a method to access SDL_audio.c's device lists. static void PauseAudioDevices(void)
{ {
if (!open_devices) { (void) SDL_FindPhysicalAudioDeviceByCallback(PauseOneAudioDevice, NULL);
return; }
}
for (int i = 0; i < num_open_devices; ++i) { static SDL_bool ResumeOneAudioDevice(SDL_AudioDevice *device, void *userdata)
SDL_AudioDevice *device = open_devices[i]; {
if (device->hidden->audioQueue && !device->hidden->interrupted) { if (device->hidden && device->hidden->audioQueue && !device->hidden->interrupted) {
AudioQueueStart(device->hidden->audioQueue, NULL); AudioQueueStart(device->hidden->audioQueue, NULL);
}
} }
return SDL_FALSE; // keep enumerating devices until we've resumed them all.
}
static void ResumeAudioDevices(void)
{
(void) SDL_FindPhysicalAudioDeviceByCallback(ResumeOneAudioDevice, NULL);
} }
static void InterruptionBegin(SDL_AudioDevice *device) static void InterruptionBegin(SDL_AudioDevice *device)
@ -362,6 +360,25 @@ static void InterruptionEnd(SDL_AudioDevice *device)
@end @end
typedef struct
{
int output;
int capture;
} CountOpenAudioDevicesData;
static SDL_bool CountOpenAudioDevices(SDL_AudioDevice *device, void *userdata)
{
CountOpenAudioDevicesData *data = (CountOpenAudioDevicesData *) userdata;
if (device->hidden != NULL) { // assume it's open if hidden != NULL
if (device->iscapture) {
data->capture++;
} else {
data->output++;
}
}
return SDL_FALSE; // keep enumerating until all devices have been checked.
}
static SDL_bool UpdateAudioSession(SDL_AudioDevice *device, SDL_bool open, SDL_bool allow_playandrecord) static SDL_bool UpdateAudioSession(SDL_AudioDevice *device, SDL_bool open, SDL_bool allow_playandrecord)
{ {
@autoreleasepool { @autoreleasepool {
@ -374,6 +391,10 @@ static SDL_bool UpdateAudioSession(SDL_AudioDevice *device, SDL_bool open, SDL_b
NSError *err = nil; NSError *err = nil;
const char *hint; const char *hint;
CountOpenAudioDevicesData data;
SDL_zero(data);
(void) SDL_FindPhysicalAudioDeviceByCallback(CountOpenAudioDevices, &data);
hint = SDL_GetHint(SDL_HINT_AUDIO_CATEGORY); hint = SDL_GetHint(SDL_HINT_AUDIO_CATEGORY);
if (hint) { if (hint) {
if (SDL_strcasecmp(hint, "AVAudioSessionCategoryAmbient") == 0) { if (SDL_strcasecmp(hint, "AVAudioSessionCategoryAmbient") == 0) {
@ -391,9 +412,9 @@ static SDL_bool UpdateAudioSession(SDL_AudioDevice *device, SDL_bool open, SDL_b
category = AVAudioSessionCategoryPlayAndRecord; category = AVAudioSessionCategoryPlayAndRecord;
} }
} }
} else if (open_playback_devices && open_capture_devices) { } else if (data.output && data.capture) {
category = AVAudioSessionCategoryPlayAndRecord; category = AVAudioSessionCategoryPlayAndRecord;
} else if (open_capture_devices) { } else if (data.capture) {
category = AVAudioSessionCategoryRecord; category = AVAudioSessionCategoryRecord;
} }
@ -446,7 +467,7 @@ static SDL_bool UpdateAudioSession(SDL_AudioDevice *device, SDL_bool open, SDL_b
} }
} }
if ((open_playback_devices || open_capture_devices) && !session_active) { if ((data.output || data.capture) && !session_active) {
if (![session setActive:YES error:&err]) { if (![session setActive:YES error:&err]) {
if ([err code] == AVAudioSessionErrorCodeResourceNotAvailable && if ([err code] == AVAudioSessionErrorCodeResourceNotAvailable &&
category == AVAudioSessionCategoryPlayAndRecord) { category == AVAudioSessionCategoryPlayAndRecord) {
@ -459,7 +480,7 @@ static SDL_bool UpdateAudioSession(SDL_AudioDevice *device, SDL_bool open, SDL_b
} }
session_active = SDL_TRUE; session_active = SDL_TRUE;
ResumeAudioDevices(); ResumeAudioDevices();
} else if (!open_playback_devices && !open_capture_devices && session_active) { } else if (!data.output && !data.capture && session_active) {
PauseAudioDevices(); PauseAudioDevices();
[session setActive:NO error:nil]; [session setActive:NO error:nil];
session_active = SDL_FALSE; session_active = SDL_FALSE;