diff --git a/Common/GPU/Vulkan/VulkanLoader.cpp b/Common/GPU/Vulkan/VulkanLoader.cpp index 95c1bfa1d7..24e128aada 100644 --- a/Common/GPU/Vulkan/VulkanLoader.cpp +++ b/Common/GPU/Vulkan/VulkanLoader.cpp @@ -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 diff --git a/Common/System/System.h b/Common/System/System.h index 7c7b3c4c1b..3668d8074e 100644 --- a/Common/System/System.h +++ b/Common/System/System.h @@ -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 diff --git a/Core/Config.cpp b/Core/Config.cpp index 83a76ac3ce..ee757a61e1 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -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), }; diff --git a/Core/Config.h b/Core/Config.h index 7f545a3b57..e414309cf3 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -280,6 +280,10 @@ public: bool bAutoAudioDevice; bool bUseNewAtrac; + // iOS only for now + bool bAudioMixWithOthers; + bool bAudioRespectSilentMode; + // UI bool bShowDebuggerOnLoad; int iShowStatusFlags; diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 283775dc57..0719664aab 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -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")); diff --git a/ios/DisplayManager.mm b/ios/DisplayManager.mm index 3e19194bef..782332ee28 100644 --- a/ios/DisplayManager.mm +++ b/ios/DisplayManager.mm @@ -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]]; } }]; diff --git a/ios/iOSCoreAudio.h b/ios/iOSCoreAudio.h index a3c70ccbfc..944e0e81dc 100644 --- a/ios/iOSCoreAudio.h +++ b/ios/iOSCoreAudio.h @@ -19,4 +19,9 @@ // Originally written by jtraynham void iOSCoreAudioInit(); -void iOSCoreAudioShutdown(); \ No newline at end of file +void iOSCoreAudioShutdown(); + +// Ignore mute switch when connected to external display. +// Also, obey other settings. +void iOSCoreAudioUpdateSession(); +void iOSCoreAudioSetDisplayConnected(bool connected); \ No newline at end of file diff --git a/ios/iOSCoreAudio.mm b/ios/iOSCoreAudio.mm index 8129cc9afc..f54bd99007 100644 --- a/ios/iOSCoreAudio.mm +++ b/ios/iOSCoreAudio.mm @@ -21,6 +21,7 @@ #include "iOSCoreAudio.h" #include "Common/Log.h" +#include "Core/Config.h" #include #import @@ -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; diff --git a/ios/main.mm b/ios/main.mm index 9f1dcded49..dc6a83834e 100644 --- a/ios/main.mm +++ b/ios/main.mm @@ -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; }