Merge pull request #19200 from hrydgard/ios-audio-session-mode

iOS: Add audio session mode controls
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2024-05-27 23:54:25 +02:00
commit 2fc34563e2
9 files changed
+91 -8

No files matched your search

+1 -1
View File
@@ -382,7 +382,7 @@ bool VulkanMayBeAvailable() {
#if PPSSPP_PLATFORM(IOS)
g_vulkanAvailabilityChecked = true;
g_vulkanMayBeAvailable = System_GetPropertyInt(SYSPROP_SYSTEMVERSION) >= 13;
INFO_LOG(SYSTEM, "VulkanMayBeAvailable: Detected version: %d", System_GetPropertyInt(SYSPROP_SYSTEMVERSION));
INFO_LOG(SYSTEM, "VulkanMayBeAvailable: Detected version: %d", (int)System_GetPropertyInt(SYSPROP_SYSTEMVERSION));
return g_vulkanMayBeAvailable;
#else
// Unsupported in VR at the moment
+1
View File
@@ -230,6 +230,7 @@ enum class SystemNotification {
KEEP_SCREEN_AWAKE,
ACTIVITY,
UI_STATE_CHANGED,
AUDIO_MODE_CHANGED,
};
// I guess it's not super great architecturally to centralize this, since it's not general - but same with a lot of
+2
View File
@@ -700,6 +700,8 @@ static const ConfigSetting soundSettings[] = {
ConfigSetting("AchievementSoundVolume", &g_Config.iAchievementSoundVolume, 6, CfgFlag::PER_GAME),
ConfigSetting("AudioDevice", &g_Config.sAudioDevice, "", CfgFlag::DEFAULT),
ConfigSetting("AutoAudioDevice", &g_Config.bAutoAudioDevice, true, CfgFlag::DEFAULT),
ConfigSetting("AudioMixWithOthers", &g_Config.bAudioMixWithOthers, true, CfgFlag::DEFAULT),
ConfigSetting("AudioRespectSilentMode", &g_Config.bAudioRespectSilentMode, false, CfgFlag::DEFAULT),
ConfigSetting("UseNewAtrac", &g_Config.bUseNewAtrac, false, CfgFlag::DEFAULT),
};
+4
View File
@@ -280,6 +280,10 @@ public:
bool bAutoAudioDevice;
bool bUseNewAtrac;
// iOS only for now
bool bAudioMixWithOthers;
bool bAudioRespectSilentMode;
// UI
bool bShowDebuggerOnLoad;
int iShowStatusFlags;
+17 -1
View File
@@ -617,7 +617,23 @@ void GameSettingsScreen::CreateAudioSettings(UI::ViewGroup *audioSettings) {
auto ms = GetI18NCategory(I18NCat::MAINSETTINGS);
audioSettings->Add(new ItemHeader(ms->T("Audio")));
CheckBox *enableSound = audioSettings->Add(new CheckBox(&g_Config.bEnableSound,a->T("Enable Sound")));
CheckBox *enableSound = audioSettings->Add(new CheckBox(&g_Config.bEnableSound,a->T("Enable Sound")));
#if PPSSPP_PLATFORM(IOS)
CheckBox *respectSilentMode = audioSettings->Add(new CheckBox(&g_Config.bAudioRespectSilentMode, a->T("Respect silent mode")));
respectSilentMode->OnClick.Add([=](EventParams &e) {
System_Notify(SystemNotification::AUDIO_MODE_CHANGED);
return UI::EVENT_DONE;
});
respectSilentMode->SetEnabledPtr(&g_Config.bEnableSound);
CheckBox *mixWithOthers = audioSettings->Add(new CheckBox(&g_Config.bAudioMixWithOthers, a->T("Mix audio with other apps")));
mixWithOthers->OnClick.Add([=](EventParams &e) {
System_Notify(SystemNotification::AUDIO_MODE_CHANGED);
return UI::EVENT_DONE;
});
mixWithOthers->SetEnabledPtr(&g_Config.bEnableSound);
#endif
PopupSliderChoice *volume = audioSettings->Add(new PopupSliderChoice(&g_Config.iGlobalVolume, VOLUME_OFF, VOLUME_FULL, VOLUME_FULL, a->T("Global volume"), screenManager()));
volume->SetEnabledPtr(&g_Config.bEnableSound);
volume->SetZeroLabel(a->T("Mute"));
+6 -4
View File
@@ -6,6 +6,7 @@
//
#import "DisplayManager.h"
#import "iOSCoreAudio.h"
#import "ViewController.h"
#import "AppDelegate.h"
#include "Common/System/Display.h"
@@ -64,6 +65,9 @@
[self setOriginalFrame: [gameWindow frame]];
[self setOriginalBounds:[gameWindow bounds]];
[self setOriginalTransform:[gameWindow transform]];
// TODO: From iOS 13, should use UIScreenDidConnectNotification instead of the below.
// Display connected
[[NSNotificationCenter defaultCenter] addObserverForName:UIScreenDidConnectNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull notification) {
UIScreen *screen = (UIScreen *) notification.object;
@@ -74,8 +78,7 @@
return;
}
// Ignore mute switch when connected to external display
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
iOSCoreAudioSetDisplayConnected(true);
[self updateScreen:screen];
}];
// Display disconnected
@@ -89,8 +92,7 @@
UIScreen *newScreen = [[self extDisplays] lastObject];
[self updateScreen:newScreen];
} else {
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
iOSCoreAudioSetDisplayConnected(false);
[self updateScreen:[UIScreen mainScreen]];
}
}];
+6 -1
View File
@@ -19,4 +19,9 @@
// Originally written by jtraynham
void iOSCoreAudioInit();
void iOSCoreAudioShutdown();
void iOSCoreAudioShutdown();
// Ignore mute switch when connected to external display.
// Also, obey other settings.
void iOSCoreAudioUpdateSession();
void iOSCoreAudioSetDisplayConnected(bool connected);
+48 -1
View File
@@ -21,6 +21,7 @@
#include "iOSCoreAudio.h"
#include "Common/Log.h"
#include "Core/Config.h"
#include <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@@ -28,6 +29,50 @@
#define SAMPLE_RATE 44100
static AudioComponentInstance audioInstance = nil;
static bool g_displayConnected = false;
void iOSCoreAudioUpdateSession() {
NSError *error = nil;
if (g_displayConnected) {
INFO_LOG(AUDIO, "Display connected, setting Playback mode");
// Special handling when a display is connected. Always exclusive.
// Let's revisit this later.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
return;
}
INFO_LOG(AUDIO, "RespectSilentMode: %d MixWithOthers: %d", g_Config.bAudioRespectSilentMode, g_Config.bAudioMixWithOthers);
// Hacky hack to force iOS to re-evaluate.
// Switching from CatogoryPlayback to CategoryPlayback with an option otherwise does nothing.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAudioProcessing error:&error];
// Here, we apply the settings.
const bool mixWithOthers = g_Config.bAudioMixWithOthers;
if (g_Config.bAudioMixWithOthers) {
if (g_Config.bAudioRespectSilentMode) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
}
} else {
if (g_Config.bAudioRespectSilentMode) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:&error];
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:&error];
}
// Can't achieve exclusive + respect silent mode
}
if (error) {
NSLog(@"%@", error);
}
}
void iOSCoreAudioSetDisplayConnected(bool connected) {
g_displayConnected = connected;
iOSCoreAudioUpdateSession();
}
int NativeMix(short *audio, int numSamples, int sampleRate);
@@ -64,6 +109,8 @@ OSStatus iOSCoreAudioCallback(void *inRefCon,
void iOSCoreAudioInit()
{
iOSCoreAudioUpdateSession();
NSError *error = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
if (![session setActive:YES error:&error]) {
@@ -75,7 +122,7 @@ void iOSCoreAudioInit()
NSLog(@"%@", error.localizedFailureReason);
}
}
if (audioInstance) {
// Already running
return;
+6
View File
@@ -24,6 +24,7 @@
#import "AppDelegate.h"
#import "PPSSPPUIApplication.h"
#import "ViewController.h"
#import "iOSCoreAudio.h"
#include "Common/MemoryUtil.h"
#include "Common/System/NativeApp.h"
@@ -393,6 +394,11 @@ void System_Notify(SystemNotification notification) {
}
});
break;
case SystemNotification::AUDIO_MODE_CHANGED:
dispatch_async(dispatch_get_main_queue(), ^{
iOSCoreAudioUpdateSession();
});
break;
default:
break;
}