From d417d245f40f490e2f5260ce206c5c7b7b02b1c0 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Thu, 14 Jul 2022 03:16:21 -0700 Subject: [PATCH] [Audio Output] Make toggling DSPs safe The DSPs should not be deinitialized from another thread, possibly while they are currently processing. Signed-off-by: Christopher Snowhill --- Audio/Output/OutputAVFoundation.h | 2 ++ Audio/Output/OutputAVFoundation.m | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Audio/Output/OutputAVFoundation.h b/Audio/Output/OutputAVFoundation.h index bbf9c64d4..3728b36a5 100644 --- a/Audio/Output/OutputAVFoundation.h +++ b/Audio/Output/OutputAVFoundation.h @@ -113,6 +113,8 @@ using std::atomic_long; BOOL enableFSurround; FSurroundFilter *fsurround; + BOOL resetStreamFormat; + float *samplePtr; float tempBuffer[512 * 32]; float inputBuffer[4096 * 32]; // 4096 samples times maximum supported channel count diff --git a/Audio/Output/OutputAVFoundation.m b/Audio/Output/OutputAVFoundation.m index be61cbf64..5e4150c1b 100644 --- a/Audio/Output/OutputAVFoundation.m +++ b/Audio/Output/OutputAVFoundation.m @@ -304,12 +304,10 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons eqPreamp = pow(10.0, preamp / 20.0); } else if([keyPath isEqualToString:@"values.enableHrtf"]) { enableHrtf = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"enableHrtf"]; - if(streamFormatStarted) - [self updateStreamFormat]; + resetStreamFormat = YES; } else if([keyPath isEqualToString:@"values.enableFSurround"]) { enableFSurround = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"enableFSurround"]; - if(streamFormatStarted) - [self updateStreamFormat]; + resetStreamFormat = YES; } } @@ -606,6 +604,7 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons - (void)updateStreamFormat { /* Set the channel layout for the audio queue */ streamFormatChanged = YES; + resetStreamFormat = NO; uint32_t channels = realStreamFormat.mChannelsPerFrame; uint32_t channelConfig = realStreamChannelConfig; @@ -746,6 +745,10 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons status = CMBlockBufferCreateEmpty(kCFAllocatorDefault, 0, 0, &blockListBuffer); if(status != noErr || !blockListBuffer) return 0; + if(resetStreamFormat) { + [self updateStreamFormat]; + } + int inputRendered = 0; int bytesRendered = 0; do { @@ -898,7 +901,9 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons commandStop = NO; audioFormatDescription = NULL; - + + resetStreamFormat = NO; + running = NO; stopping = NO; stopped = NO;