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

Switched away from @synchronized as it appears possibly to be the lock used during -dealloc, creating deadlock with the CSAudioQueueDeallocLock.

This commit is contained in:
Thomas Harte 2017-02-22 21:42:10 -05:00
parent 8291a63d5f
commit 2f0c923c29

View File

@ -17,8 +17,8 @@ static NSLock *CSAudioQueueDeallocLock;
@implementation CSAudioQueue @implementation CSAudioQueue
{ {
AudioQueueRef _audioQueue; AudioQueueRef _audioQueue;
AudioQueueBufferRef _storedBuffers[NumberOfStoredAudioQueueBuffer]; AudioQueueBufferRef _storedBuffers[NumberOfStoredAudioQueueBuffer];
NSLock *_storedBuffersLock;
} }
#pragma mark - AudioQueue callbacks #pragma mark - AudioQueue callbacks
@ -27,18 +27,18 @@ static NSLock *CSAudioQueueDeallocLock;
{ {
[self.delegate audioQueueIsRunningDry:self]; [self.delegate audioQueueIsRunningDry:self];
@synchronized(self) [_storedBuffersLock lock];
for(int c = 0; c < NumberOfStoredAudioQueueBuffer; c++)
{ {
for(int c = 0; c < NumberOfStoredAudioQueueBuffer; c++) if(!_storedBuffers[c] || buffer->mAudioDataBytesCapacity > _storedBuffers[c]->mAudioDataBytesCapacity)
{ {
if(!_storedBuffers[c] || buffer->mAudioDataBytesCapacity > _storedBuffers[c]->mAudioDataBytesCapacity) if(_storedBuffers[c]) AudioQueueFreeBuffer(_audioQueue, _storedBuffers[c]);
{ _storedBuffers[c] = buffer;
if(_storedBuffers[c]) AudioQueueFreeBuffer(_audioQueue, _storedBuffers[c]); [_storedBuffersLock unlock];
_storedBuffers[c] = buffer; return;
return;
}
} }
} }
[_storedBuffersLock unlock];
AudioQueueFreeBuffer(_audioQueue, buffer); AudioQueueFreeBuffer(_audioQueue, buffer);
} }
@ -66,6 +66,7 @@ static void audioOutputCallback(
{ {
CSAudioQueueDeallocLock = [[NSLock alloc] init]; CSAudioQueueDeallocLock = [[NSLock alloc] init];
} }
_storedBuffersLock = [[NSLock alloc] init];
_samplingRate = samplingRate; _samplingRate = samplingRate;
@ -126,28 +127,28 @@ static void audioOutputCallback(
{ {
size_t bufferBytes = lengthInSamples * sizeof(int16_t); size_t bufferBytes = lengthInSamples * sizeof(int16_t);
@synchronized(self) [_storedBuffersLock lock];
for(int c = 0; c < NumberOfStoredAudioQueueBuffer; c++)
{ {
for(int c = 0; c < NumberOfStoredAudioQueueBuffer; c++) if(_storedBuffers[c] && _storedBuffers[c]->mAudioDataBytesCapacity >= bufferBytes)
{ {
if(_storedBuffers[c] && _storedBuffers[c]->mAudioDataBytesCapacity >= bufferBytes) memcpy(_storedBuffers[c]->mAudioData, buffer, bufferBytes);
{ _storedBuffers[c]->mAudioDataByteSize = (UInt32)bufferBytes;
memcpy(_storedBuffers[c]->mAudioData, buffer, bufferBytes);
_storedBuffers[c]->mAudioDataByteSize = (UInt32)bufferBytes;
AudioQueueEnqueueBuffer(_audioQueue, _storedBuffers[c], 0, NULL); AudioQueueEnqueueBuffer(_audioQueue, _storedBuffers[c], 0, NULL);
_storedBuffers[c] = NULL; _storedBuffers[c] = NULL;
return; [_storedBuffersLock unlock];
} return;
} }
AudioQueueBufferRef newBuffer;
AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &newBuffer);
memcpy(newBuffer->mAudioData, buffer, bufferBytes);
newBuffer->mAudioDataByteSize = (UInt32)bufferBytes;
AudioQueueEnqueueBuffer(_audioQueue, newBuffer, 0, NULL);
} }
[_storedBuffersLock unlock];
AudioQueueBufferRef newBuffer;
AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &newBuffer);
memcpy(newBuffer->mAudioData, buffer, bufferBytes);
newBuffer->mAudioDataByteSize = (UInt32)bufferBytes;
AudioQueueEnqueueBuffer(_audioQueue, newBuffer, 0, NULL);
} }
#pragma mark - Sampling Rate getters #pragma mark - Sampling Rate getters