1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-08 15:29:09 +00:00

Attempt to get a bit more rigorous in diagnosing queue stoppages.

This commit is contained in:
Thomas Harte 2022-07-12 21:43:33 -04:00
parent 1c537a877e
commit b7ad94c676

View File

@ -12,44 +12,36 @@
#define AudioQueueBufferMaxLength 8192 #define AudioQueueBufferMaxLength 8192
#define OSSGuard(x) { \
const OSStatus status = x; \
assert(!status); \
(void)status; \
}
#define IsDry(x) (x) < 3
@implementation CSAudioQueue { @implementation CSAudioQueue {
AudioQueueRef _audioQueue; AudioQueueRef _audioQueue;
NSLock *_deallocLock; NSLock *_deallocLock;
NSLock *_queueLock;
atomic_int _enqueuedBuffers; atomic_int _enqueuedBuffers;
} }
#pragma mark - AudioQueue callbacks #pragma mark - Status
/*!
@returns @c YES if the queue is running dry; @c NO otherwise.
*/
- (BOOL)audioQueue:(AudioQueueRef)theAudioQueue didCallbackWithBuffer:(AudioQueueBufferRef)buffer {
const int buffers = atomic_fetch_add(&_enqueuedBuffers, -1);
// If that suggests the queue may be exhausted soon, re-enqueue whatever just came back in order to
// keep the queue going. AudioQueues seem to stop playing and never restart no matter how much
// encouragement if exhausted.
if(!buffers) {
AudioQueueEnqueueBuffer(theAudioQueue, buffer, 0, NULL);
atomic_fetch_add(&_enqueuedBuffers, 1);
} else {
AudioQueueFreeBuffer(_audioQueue, buffer);
}
return YES;
}
- (BOOL)isRunningDry { - (BOOL)isRunningDry {
return atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) < 3; return IsDry(atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed));
} }
#pragma mark - Standard object lifecycle #pragma mark - Object lifecycle
- (instancetype)initWithSamplingRate:(Float64)samplingRate isStereo:(BOOL)isStereo { - (instancetype)initWithSamplingRate:(Float64)samplingRate isStereo:(BOOL)isStereo {
self = [super init]; self = [super init];
if(self) { if(self) {
_deallocLock = [[NSLock alloc] init]; _deallocLock = [[NSLock alloc] init];
_queueLock = [[NSLock alloc] init];
atomic_store_explicit(&_enqueuedBuffers, 0, memory_order_relaxed);
_samplingRate = samplingRate; _samplingRate = samplingRate;
@ -87,13 +79,21 @@
} }
if([queue->_deallocLock tryLock]) { if([queue->_deallocLock tryLock]) {
BOOL isRunningDry = NO; [queue->_queueLock lock];
isRunningDry = [queue audioQueue:inAQ didCallbackWithBuffer:inBuffer];
OSSGuard(AudioQueueFreeBuffer(inAQ, inBuffer));
const int buffers = atomic_fetch_add(&queue->_enqueuedBuffers, -1) - 1;
// if(!buffers) {
// OSSGuard(AudioQueueStop(inAQ, true));
// }
[queue->_queueLock unlock];
id<CSAudioQueueDelegate> delegate = queue.delegate; id<CSAudioQueueDelegate> delegate = queue.delegate;
[queue->_deallocLock unlock]; [queue->_deallocLock unlock];
if(isRunningDry) [delegate audioQueueIsRunningDry:queue]; if(IsDry(buffers)) [delegate audioQueueIsRunningDry:queue];
} }
} }
) )
@ -108,7 +108,7 @@
- (void)dealloc { - (void)dealloc {
[_deallocLock lock]; [_deallocLock lock];
if(_audioQueue) { if(_audioQueue) {
AudioQueueDispose(_audioQueue, true); OSSGuard(AudioQueueDispose(_audioQueue, true));
_audioQueue = NULL; _audioQueue = NULL;
} }
@ -125,23 +125,27 @@
size_t bufferBytes = lengthInSamples * sizeof(int16_t); size_t bufferBytes = lengthInSamples * sizeof(int16_t);
// Don't enqueue more than 4 buffers ahead of now, to ensure not too much latency accrues. // Don't enqueue more than 4 buffers ahead of now, to ensure not too much latency accrues.
if(atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) > 4) { if(atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) == 4) {
return; return;
} }
const int enqueuedBuffers = atomic_fetch_add(&_enqueuedBuffers, 1); const int enqueuedBuffers = atomic_fetch_add(&_enqueuedBuffers, 1) + 1;
[_queueLock lock];
AudioQueueBufferRef newBuffer; AudioQueueBufferRef newBuffer;
AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &newBuffer); OSSGuard(AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &newBuffer));
memcpy(newBuffer->mAudioData, buffer, bufferBytes); memcpy(newBuffer->mAudioData, buffer, bufferBytes);
newBuffer->mAudioDataByteSize = (UInt32)bufferBytes; newBuffer->mAudioDataByteSize = (UInt32)bufferBytes;
AudioQueueEnqueueBuffer(_audioQueue, newBuffer, 0, NULL); OSSGuard(AudioQueueEnqueueBuffer(_audioQueue, newBuffer, 0, NULL));
// 'Start' the queue. This is documented to be a no-op if the queue is already started, // 'Start' the queue. This is documented to be a no-op if the queue is already started,
// and it's better to defer starting it until at least some data is available. // and it's better to defer starting it until at least some data is available.
if(enqueuedBuffers > 2) { if(enqueuedBuffers > 2) {
AudioQueueStart(_audioQueue, NULL); OSSGuard(AudioQueueStart(_audioQueue, NULL));
} }
[_queueLock unlock];
} }
#pragma mark - Sampling Rate getters #pragma mark - Sampling Rate getters