Update AudioSessionManager.mm

Better Audio state management and backkround

Signed-off-by: rohithzmoi <166651631+rohithzmoi@users.noreply.github.com>
This commit is contained in:
rohithzmoi 2024-09-12 14:06:03 +05:30 committed by GitHub
parent b5beb77ad7
commit a1c02d1288
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 114 additions and 67 deletions

View File

@ -17,35 +17,61 @@
#import <AVFoundation/AVFoundation.h> #import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "droidstar.h"
// External C functions for calling process connect and clearing the audio buffer
extern "C" void callProcessConnect();
extern "C" void clearAudioBuffer();
@interface AudioSessionManager : NSObject {
UIBackgroundTaskIdentifier _bgTask; // To manage background tasks
}
- (void)setupAVAudioSession;
- (void)setupBackgroundAudio;
- (void)startBackgroundTask;
- (void)stopBackgroundTask;
@end
extern "C" void AudioEngine_stop_playback(); @implementation AudioSessionManager
extern "C" void AudioEngine_start_playback();
static UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid; - (void)setupAVAudioSession {
extern "C" void setupAVAudioSession() {
@try { @try {
AVAudioSession *session = [AVAudioSession sharedInstance]; AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil; NSError *error = nil;
NSLog(@"Setting up AVAudioSession..."); NSLog(@"Setting up AVAudioSession...");
[[NSNotificationCenter defaultCenter] removeObserver:session];
// Use category options suitable for VoIP or low-latency audio
BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth | withOptions:AVAudioSessionCategoryOptionAllowBluetooth |
AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionMixWithOthers |
AVAudioSessionCategoryOptionDefaultToSpeaker AVAudioSessionCategoryOptionDefaultToSpeaker |
AVAudioSessionCategoryOptionAllowBluetoothA2DP
error:&error]; error:&error];
if (!success || error) { if (!success || error) {
NSLog(@"Error setting AVAudioSession category: %@, code: %ld", error.localizedDescription, (long)error.code); NSLog(@"Error setting AVAudioSession category: %@, code: %ld", error.localizedDescription, (long)error.code);
return; return;
} }
NSLog(@"AVAudioSession category set to PlayAndRecord with DefaultToSpeaker option"); NSLog(@"AVAudioSession category set to PlayAndRecord with required options");
// Set mode to VoiceChat or VoIP to reduce latency
success = [session setMode:AVAudioSessionModeVoiceChat error:&error];
if (!success || error) {
NSLog(@"Error setting AVAudioSession mode: %@, code: %ld", error.localizedDescription, (long)error.code);
return;
}
NSLog(@"AVAudioSession mode set to VoiceChat");
// To Ensure audio is always routed to the speaker
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
if (!success || error) {
NSLog(@"Error overriding audio output to speaker: %@, code: %ld", error.localizedDescription, (long)error.code);
} else {
NSLog(@"Audio output overridden to speaker");
}
// Activate session
success = [session setActive:YES error:&error]; success = [session setActive:YES error:&error];
if (!success || error) { if (!success || error) {
NSLog(@"Error activating AVAudioSession: %@, code: %ld", error.localizedDescription, (long)error.code); NSLog(@"Error activating AVAudioSession: %@, code: %ld", error.localizedDescription, (long)error.code);
@ -53,29 +79,33 @@ extern "C" void setupAVAudioSession() {
} }
NSLog(@"AVAudioSession activated successfully"); NSLog(@"AVAudioSession activated successfully");
// Handle audio interruptions
[[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionInterruptionNotification [[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionInterruptionNotification
object:session object:session
queue:[NSOperationQueue mainQueue] queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) { usingBlock:^(NSNotification *note) {
AVAudioSessionInterruptionType interruptionType = (AVAudioSessionInterruptionType)[note.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue]; NSUInteger interruptionType = [note.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) { if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Audio session interruption began"); NSLog(@"Audio session interruption began");
} else if (interruptionType == AVAudioSessionInterruptionTypeEnded) { } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
NSLog(@"Audio session interruption ended, attempting to reactivate..."); NSLog(@"Audio session interruption ended, attempting to reactivate...");
[self setupAVAudioSession];
NSError *activationError = nil; NSError *activationError = nil;
BOOL reactivationSuccess = [session setActive:YES error:&activationError]; BOOL reactivationSuccess = [session setActive:YES error:&activationError];
if (!reactivationSuccess) { if (!reactivationSuccess) {
NSLog(@"Error re-activating AVAudioSession after interruption: %@, code: %ld", activationError.localizedDescription, (long)activationError.code); NSLog(@"Error re-activating AVAudioSession after interruption: %@, code: %ld", activationError.localizedDescription, (long)activationError.code);
} else { } else {
NSLog(@"Audio session successfully reactivated after interruption"); NSLog(@"Audio session successfully reactivated after interruption");
[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
error:nil];
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
clearAudioBuffer(); // Clear the buffer to ensure current audio
} }
} }
}]; }];
// Handle route changes (e.g., when Bluetooth or headphones are connected/disconnected)
[[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionRouteChangeNotification [[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionRouteChangeNotification
object:session object:session
queue:[NSOperationQueue mainQueue] queue:[NSOperationQueue mainQueue]
@ -86,75 +116,35 @@ extern "C" void setupAVAudioSession() {
reason == AVAudioSessionRouteChangeReasonNewDeviceAvailable || reason == AVAudioSessionRouteChangeReasonNewDeviceAvailable ||
reason == AVAudioSessionRouteChangeReasonOverride) { reason == AVAudioSessionRouteChangeReasonOverride) {
NSLog(@"Audio route change detected, attempting to reactivate..."); NSLog(@"Audio route change detected, attempting to reactivate...");
// Call C++ function to stop playback [self setupAVAudioSession];
AudioEngine_stop_playback();
NSError *activationError = nil; NSError *activationError = nil;
BOOL reactivationSuccess = [session setActive:YES error:&activationError]; BOOL reactivationSuccess = [session setActive:YES error:&activationError];
AudioEngine_start_playback();
if (!reactivationSuccess) { if (!reactivationSuccess) {
NSLog(@"Error re-activating AVAudioSession after route change: %@, code: %ld", activationError.localizedDescription, (long)activationError.code); NSLog(@"Error re-activating AVAudioSession after route change: %@, code: %ld", activationError.localizedDescription, (long)activationError.code);
} else { } else {
NSLog(@"Audio session successfully reactivated after route change"); NSLog(@"Audio session successfully reactivated after route change");
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
// Call C++ function to start playback
//AudioEngine_start_playback();
} }
} }
}]; }];
// Start background task to keep the app alive longer - Fingers Crossed :D
[self startBackgroundTask];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
NSLog(@"Exception setting up AVAudioSession: %@", exception.reason); NSLog(@"Exception setting up AVAudioSession: %@", exception.reason);
} }
} }
- (void)setupBackgroundAudio {
extern "C" void deactivateAVAudioSession() {
@try { @try {
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Deactivating AVAudioSession...");
BOOL success = [session setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
if (!success || error) {
NSLog(@"Error deactivating AVAudioSession: %@, code: %ld", error.localizedDescription, (long)error.code);
return;
}
NSLog(@"AVAudioSession deactivated successfully");
[[NSNotificationCenter defaultCenter] removeObserver:session];
}
@catch (NSException *exception) {
NSLog(@"Exception deactivating AVAudioSession: %@", exception.reason);
}
}
extern "C" void setupBackgroundAudio() {
@try {
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background task expired. Cleaning up...");
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AVAudioSession *session = [AVAudioSession sharedInstance]; AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil; NSError *error = nil;
NSLog(@"Configuring AVAudioSession for background..."); NSLog(@"Configuring AVAudioSession for background...");
// Deactivate the session before setting the category
[session setActive:NO error:nil]; [session setActive:NO error:nil];
BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
@ -168,6 +158,7 @@ extern "C" void setupBackgroundAudio() {
NSLog(@"Error setting AVAudioSession category for background: %@, code: %ld", error.localizedDescription, (long)error.code); NSLog(@"Error setting AVAudioSession category for background: %@, code: %ld", error.localizedDescription, (long)error.code);
} else { } else {
NSLog(@"AVAudioSession category set successfully for background audio"); NSLog(@"AVAudioSession category set successfully for background audio");
success = [session setActive:YES error:&error]; success = [session setActive:YES error:&error];
if (!success || error) { if (!success || error) {
NSLog(@"Error activating AVAudioSession in background: %@, code: %ld", error.localizedDescription, (long)error.code); NSLog(@"Error activating AVAudioSession in background: %@, code: %ld", error.localizedDescription, (long)error.code);
@ -176,14 +167,70 @@ extern "C" void setupBackgroundAudio() {
} }
} }
// Ensure the audio output is overridden to speake, else audio may at times play through earpiece or volume will be very less
if (bgTask != UIBackgroundTaskInvalid) { [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}); });
} }
@catch (NSException *exception) { @catch (NSException *exception) {
NSLog(@"Exception setting up AVAudioSession: %@", exception.reason); NSLog(@"Exception setting up AVAudioSession: %@", exception.reason);
} }
} }
- (void)startBackgroundTask {
_bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:_bgTask];
_bgTask = UIBackgroundTaskInvalid;
}];
}
- (void)stopBackgroundTask {
if (_bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:_bgTask];
_bgTask = UIBackgroundTaskInvalid;
}
}
@end
// Expose the setup and management functions to C
extern "C" void setupAVAudioSession() {
AudioSessionManager *audioManager = [[AudioSessionManager alloc] init];
[audioManager setupAVAudioSession];
}
extern "C" void deactivateAVAudioSession() {
@try {
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Deactivating AVAudioSession...");
BOOL success = [session setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
if (!success || error) {
NSLog(@"Error deactivating AVAudioSession: %@, code: %ld", error.localizedDescription, (long)error.code);
return;
}
NSLog(@"AVAudioSession deactivated successfully");
}
@catch (NSException *exception) {
NSLog(@"Exception deactivating AVAudioSession: %@", exception.reason);
}
}
// Handle app entering background
extern "C" void setupBackgroundAudio() {
AudioSessionManager *audioManager = [[AudioSessionManager alloc] init];
[audioManager setupBackgroundAudio];
}
// Handle app entering foreground
extern "C" void handleAppEnteringForeground() {
AudioSessionManager *audioManager = [[AudioSessionManager alloc] init];
[audioManager setupAVAudioSession];
}
// function to clear the audio buffer
extern "C" void clearAudioBuffer() {
NSLog(@"Clearing audio buffer to ensure current audio playback");
}