Fix #15316 - on iOS, stop/start audio on interruptions

This commit is contained in:
Eric Warmenhoven 2023-05-25 15:54:13 -04:00 committed by LibretroAdmin
parent 45d9f91212
commit 6506b65ce7
2 changed files with 27 additions and 31 deletions

View File

@ -52,10 +52,6 @@ typedef struct coreaudio
bool nonblock;
} coreaudio_t;
#if TARGET_OS_IOS
static bool g_interrupted;
#endif
static void coreaudio_free(void *data)
{
coreaudio_t *dev = (coreaudio_t*)data;
@ -123,14 +119,7 @@ static OSStatus audio_write_cb(void *userdata,
return noErr;
}
#if TARGET_OS_IPHONE
static void coreaudio_interrupt_listener(void *data, UInt32 interrupt_state)
{
#if TARGET_OS_IOS
g_interrupted = (interrupt_state == kAudioSessionBeginInterruption);
#endif
}
#else
#if !TARGET_OS_IPHONE
static void choose_output_device(coreaudio_t *dev, const char* device)
{
int i;
@ -203,9 +192,6 @@ static void *coreaudio_init(const char *device,
#endif
AURenderCallbackStruct cb = {0};
AudioStreamBasicDescription stream_desc = {0};
#if TARGET_OS_IOS
static bool session_initialized = false;
#endif
#if !HAS_MACOSX_10_12
ComponentDescription desc = {0};
#else
@ -219,15 +205,6 @@ static void *coreaudio_init(const char *device,
dev->lock = slock_new();
dev->cond = scond_new();
#if TARGET_OS_IOS
if (!session_initialized)
{
session_initialized = true;
AudioSessionInitialize(0, 0, coreaudio_interrupt_listener, 0);
AudioSessionSetActive(true);
}
#endif
/* Create AudioComponent */
desc.componentType = kAudioUnitType_Output;
#if TARGET_OS_IPHONE
@ -341,11 +318,7 @@ static ssize_t coreaudio_write(void *data, const void *buf_, size_t size)
const uint8_t *buf = (const uint8_t*)buf_;
size_t written = 0;
#if TARGET_OS_IOS
while (!g_interrupted && size > 0)
#else
while (size > 0)
#endif
while (!dev->is_paused && size > 0)
{
size_t write_avail;
@ -368,8 +341,11 @@ static ssize_t coreaudio_write(void *data, const void *buf_, size_t size)
#if TARGET_OS_IOS
if (write_avail == 0 && !scond_wait_timeout(
dev->cond, dev->lock, 3000000))
g_interrupted = true;
dev->cond, dev->lock, 300000))
{
slock_unlock(dev->lock);
break;
}
#else
if (write_avail == 0)
scond_wait(dev->cond, dev->lock);

View File

@ -28,6 +28,7 @@
#include "cocoa/cocoa_common.h"
#include "cocoa/apple_platform.h"
#include "../ui_companion_driver.h"
#include "../../audio/audio_driver.h"
#include "../../configuration.h"
#include "../../frontend/frontend.h"
#include "../../input/drivers/cocoa_input.h"
@ -428,6 +429,24 @@ enum
return _documentsDirectory;
}
- (void)handleAudioSessionInterruption:(NSNotification *)notification
{
NSNumber *type = notification.userInfo[AVAudioSessionInterruptionTypeKey];
if (![type isKindOfClass:[NSNumber class]])
return;
if ([type unsignedIntegerValue] == AVAudioSessionInterruptionTypeBegan)
{
RARCH_LOG("AudioSession Interruption Began\n");
audio_driver_stop();
}
else if ([type unsignedIntegerValue] == AVAudioSessionInterruptionTypeEnded)
{
RARCH_LOG("AudioSession Interruption Ended\n");
audio_driver_start(false);
}
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSError *error;
@ -443,6 +462,7 @@ enum
[self.window makeKeyAndVisible];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioSessionInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
[self refreshSystemConfig];
[self showGameView];