1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-25 18:30:21 +00:00

Attempted to resolve risk of an audio callback being in progress when -dealloc is received.

This commit is contained in:
Thomas Harte 2017-02-22 21:12:59 -05:00
parent 6120dae61a
commit 4c947ad553

View File

@ -12,6 +12,8 @@
#define AudioQueueBufferMaxLength 8192
#define NumberOfStoredAudioQueueBuffer 16
static NSLock *CSAudioQueueDeallocLock;
@implementation CSAudioQueue
{
AudioQueueRef _audioQueue;
@ -44,8 +46,12 @@ static void audioOutputCallback(
void *inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer)
{
if([CSAudioQueueDeallocLock tryLock])
{
[(__bridge CSAudioQueue *)inUserData audioQueue:inAQ didCallbackWithBuffer:inBuffer];
[CSAudioQueueDeallocLock unlock];
}
}
#pragma mark - Standard object lifecycle
@ -56,6 +62,11 @@ static void audioOutputCallback(
if(self)
{
if(!CSAudioQueueDeallocLock)
{
CSAudioQueueDeallocLock = [[NSLock alloc] init];
}
_samplingRate = samplingRate;
// determine preferred buffer sizes
@ -104,7 +115,9 @@ static void audioOutputCallback(
- (void)dealloc
{
if(_audioQueue) AudioQueueDispose(_audioQueue, YES);
[CSAudioQueueDeallocLock lock];
if(_audioQueue) AudioQueueDispose(_audioQueue, true);
[CSAudioQueueDeallocLock unlock];
}
#pragma mark - Audio enqueuer