Cog Audio: Fix memory leaks with new buffering

By applying copious amounts of autorelease pools, memory is freed in a
timely manner. Prior to this, buffer objects were freed, but not being
released, and thus accumulating in memory indefinitely, as the original
threads and functions had autorelease pools that scoped the entire
thread, rather than individual function blocks that utilized the new
buffering system. This fixes memory growth caused by playback.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
CQTexperiment
Christopher Snowhill 2022-02-07 04:06:36 -08:00
parent 6120fce40a
commit acb1dd75d3
4 changed files with 117 additions and 96 deletions

View File

@ -49,7 +49,7 @@
} }
- (BOOL)isFull { - (BOOL)isFull {
return listDuration >= maxDuration; return (maxDuration - listDuration) < 0.01;
} }
- (void)addChunk:(AudioChunk *)chunk { - (void)addChunk:(AudioChunk *)chunk {

View File

@ -422,7 +422,10 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes
// when the end of stream is reached. Convert function instead processes what it can, // when the end of stream is reached. Convert function instead processes what it can,
// and returns 0 samples when it has nothing more to process at the end of stream. // and returns 0 samples when it has nothing more to process at the end of stream.
while([self shouldContinue] == YES) { while([self shouldContinue] == YES) {
int amountConverted = [self convert:writeBuf amount:CHUNK_SIZE]; int amountConverted;
@autoreleasepool {
amountConverted = [self convert:writeBuf amount:CHUNK_SIZE];
}
if(!amountConverted) { if(!amountConverted) {
if(paused) { if(paused) {
while(paused) while(paused)
@ -985,13 +988,15 @@ static float db_to_scale(float db) {
[refillNode setChannelConfig:previousOutputConfig]; [refillNode setChannelConfig:previousOutputConfig];
for(;;) { for(;;) {
AudioChunk *chunk = [buffer removeSamples:16384]; @autoreleasepool {
size_t frameCount = [chunk frameCount]; AudioChunk *chunk = [buffer removeSamples:16384];
if(frameCount) { size_t frameCount = [chunk frameCount];
NSData *samples = [chunk removeSamples:frameCount]; if(frameCount) {
[refillNode writeData:[samples bytes] amount:frameCount]; NSData *samples = [chunk removeSamples:frameCount];
} else [refillNode writeData:[samples bytes] amount:frameCount];
break; } else
break;
}
} }
[self setupWithInputFormat:previousOutputFormat withInputConfig:[AudioChunk guessChannelConfig:previousOutputFormat.mChannelsPerFrame] outputFormat:outputFormat outputConfig:outputChannelConfig isLossless:rememberedLossless]; [self setupWithInputFormat:previousOutputFormat withInputConfig:[AudioChunk guessChannelConfig:previousOutputFormat.mChannelsPerFrame] outputFormat:outputFormat outputConfig:outputChannelConfig isLossless:rememberedLossless];

View File

@ -105,7 +105,9 @@
} }
if([previousNode shouldReset] == YES) { if([previousNode shouldReset] == YES) {
[buffer reset]; @autoreleasepool {
[buffer reset];
}
shouldReset = YES; shouldReset = YES;
[previousNode setShouldReset:NO]; [previousNode setShouldReset:NO];
@ -113,7 +115,11 @@
[[previousNode semaphore] signal]; [[previousNode semaphore] signal];
} }
AudioChunk *ret = [[previousNode buffer] removeSamples:maxFrames]; AudioChunk *ret;
@autoreleasepool {
ret = [[previousNode buffer] removeSamples:maxFrames];
}
[accessLock unlock]; [accessLock unlock];
@ -151,9 +157,11 @@
- (void)resetBuffer { - (void)resetBuffer {
shouldReset = YES; // Will reset on next write. shouldReset = YES; // Will reset on next write.
if(previousNode == nil) { if(previousNode == nil) {
[accessLock lock]; @autoreleasepool {
[buffer reset]; [accessLock lock];
[accessLock unlock]; [buffer reset];
[accessLock unlock];
}
} }
} }

View File

@ -48,108 +48,110 @@ static void scaleBuffersByVolume(AudioBufferList *ioData, float volume) {
} }
static OSStatus renderCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { static OSStatus renderCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) {
OutputCoreAudio *_self = (__bridge OutputCoreAudio *)inRefCon; @autoreleasepool {
OutputCoreAudio *_self = (__bridge OutputCoreAudio *)inRefCon;
const int channels = _self->deviceFormat.mChannelsPerFrame; const int channels = _self->deviceFormat.mChannelsPerFrame;
const int bytesPerPacket = channels * sizeof(float); const int bytesPerPacket = channels * sizeof(float);
size_t amountToRead, amountRead = 0; size_t amountToRead, amountRead = 0;
amountToRead = inNumberFrames * bytesPerPacket; amountToRead = inNumberFrames * bytesPerPacket;
if(_self->stopping == YES || [_self->outputController shouldContinue] == NO) { if(_self->stopping == YES || [_self->outputController shouldContinue] == NO) {
// Chain is dead, fill out the serial number pointer forever with silence // Chain is dead, fill out the serial number pointer forever with silence
clearBuffers(ioData, amountToRead / bytesPerPacket, 0); clearBuffers(ioData, amountToRead / bytesPerPacket, 0);
atomic_fetch_add(&_self->bytesRendered, amountToRead); atomic_fetch_add(&_self->bytesRendered, amountToRead);
_self->stopping = YES; _self->stopping = YES;
return 0; return 0;
}
if([[_self->outputController buffer] isEmpty] && ![_self->outputController chainQueueHasTracks]) {
// Hit end of last track, pad with silence until queue event stops us
clearBuffers(ioData, amountToRead / bytesPerPacket, 0);
atomic_fetch_add(&_self->bytesRendered, amountToRead);
return 0;
}
AudioChunk *chunk = [[_self->outputController buffer] removeSamples:(amountToRead / bytesPerPacket)];
size_t frameCount = [chunk frameCount];
AudioStreamBasicDescription format = [chunk format];
uint32_t config = [chunk channelConfig];
if(frameCount) {
if(!_self->streamFormatStarted || config != _self->streamChannelConfig || memcmp(&_self->streamFormat, &format, sizeof(format)) != 0) {
_self->streamFormat = format;
_self->streamChannelConfig = config;
_self->streamFormatStarted = YES;
_self->downmixer = [[DownmixProcessor alloc] initWithInputFormat:format inputConfig:config andOutputFormat:_self->deviceFormat outputConfig:_self->deviceChannelConfig];
} }
double chunkDuration = [chunk duration]; if([[_self->outputController buffer] isEmpty] && ![_self->outputController chainQueueHasTracks]) {
// Hit end of last track, pad with silence until queue event stops us
clearBuffers(ioData, amountToRead / bytesPerPacket, 0);
atomic_fetch_add(&_self->bytesRendered, amountToRead);
return 0;
}
NSData *samples = [chunk removeSamples:frameCount]; AudioChunk *chunk = [[_self->outputController buffer] removeSamples:(amountToRead / bytesPerPacket)];
float downmixedData[frameCount * channels]; size_t frameCount = [chunk frameCount];
[_self->downmixer process:[samples bytes] frameCount:frameCount output:downmixedData]; AudioStreamBasicDescription format = [chunk format];
uint32_t config = [chunk channelConfig];
fillBuffers(ioData, downmixedData, frameCount, 0);
amountRead = frameCount * bytesPerPacket;
[_self->outputController incrementAmountPlayed:chunkDuration];
atomic_fetch_add(&_self->bytesRendered, amountRead);
[_self->writeSemaphore signal];
}
// Try repeatedly! Buffer wraps can cause a slight data shortage, as can
// unexpected track changes.
while((amountRead < amountToRead) && [_self->outputController shouldContinue] == YES) {
chunk = [[_self->outputController buffer] removeSamples:((amountToRead - amountRead) / bytesPerPacket)];
frameCount = [chunk frameCount];
format = [chunk format];
config = [chunk channelConfig];
if(frameCount) { if(frameCount) {
if(!_self->streamFormatStarted || config != _self->streamChannelConfig || memcmp(&_self->streamFormat, &format, sizeof(format)) != 0) { if(!_self->streamFormatStarted || config != _self->streamChannelConfig || memcmp(&_self->streamFormat, &format, sizeof(format)) != 0) {
_self->streamFormat = format; _self->streamFormat = format;
_self->streamChannelConfig = config;
_self->streamFormatStarted = YES; _self->streamFormatStarted = YES;
_self->downmixer = [[DownmixProcessor alloc] initWithInputFormat:format inputConfig:config andOutputFormat:_self->deviceFormat outputConfig:_self->deviceChannelConfig]; _self->downmixer = [[DownmixProcessor alloc] initWithInputFormat:format inputConfig:config andOutputFormat:_self->deviceFormat outputConfig:_self->deviceChannelConfig];
} }
atomic_fetch_add(&_self->bytesRendered, frameCount * bytesPerPacket);
double chunkDuration = [chunk duration]; double chunkDuration = [chunk duration];
NSData *samples = [chunk removeSamples:frameCount]; NSData *samples = [chunk removeSamples:frameCount];
float downmixedData[frameCount * channels]; float downmixedData[frameCount * channels];
[_self->downmixer process:[samples bytes] frameCount:frameCount output:downmixedData]; [_self->downmixer process:[samples bytes] frameCount:frameCount output:downmixedData];
fillBuffers(ioData, downmixedData, frameCount, amountRead / bytesPerPacket);
fillBuffers(ioData, downmixedData, frameCount, 0);
amountRead = frameCount * bytesPerPacket;
[_self->outputController incrementAmountPlayed:chunkDuration]; [_self->outputController incrementAmountPlayed:chunkDuration];
atomic_fetch_add(&_self->bytesRendered, amountRead);
amountRead += frameCount * bytesPerPacket;
[_self->writeSemaphore signal]; [_self->writeSemaphore signal];
} else {
[_self->readSemaphore timedWait:500];
} }
}
float volumeScale = 1.0; // Try repeatedly! Buffer wraps can cause a slight data shortage, as can
long sustained = atomic_load_explicit(&_self->bytesHdcdSustained, memory_order_relaxed); // unexpected track changes.
if(sustained) { while((amountRead < amountToRead) && [_self->outputController shouldContinue] == YES) {
if(sustained < amountRead) { chunk = [[_self->outputController buffer] removeSamples:((amountToRead - amountRead) / bytesPerPacket)];
atomic_store(&_self->bytesHdcdSustained, 0); frameCount = [chunk frameCount];
} else { format = [chunk format];
atomic_fetch_sub(&_self->bytesHdcdSustained, amountRead); config = [chunk channelConfig];
if(frameCount) {
if(!_self->streamFormatStarted || config != _self->streamChannelConfig || memcmp(&_self->streamFormat, &format, sizeof(format)) != 0) {
_self->streamFormat = format;
_self->streamFormatStarted = YES;
_self->downmixer = [[DownmixProcessor alloc] initWithInputFormat:format inputConfig:config andOutputFormat:_self->deviceFormat outputConfig:_self->deviceChannelConfig];
}
atomic_fetch_add(&_self->bytesRendered, frameCount * bytesPerPacket);
double chunkDuration = [chunk duration];
NSData *samples = [chunk removeSamples:frameCount];
float downmixedData[frameCount * channels];
[_self->downmixer process:[samples bytes] frameCount:frameCount output:downmixedData];
fillBuffers(ioData, downmixedData, frameCount, amountRead / bytesPerPacket);
[_self->outputController incrementAmountPlayed:chunkDuration];
amountRead += frameCount * bytesPerPacket;
[_self->writeSemaphore signal];
} else {
[_self->readSemaphore timedWait:500];
}
} }
volumeScale = 0.5;
float volumeScale = 1.0;
long sustained = atomic_load_explicit(&_self->bytesHdcdSustained, memory_order_relaxed);
if(sustained) {
if(sustained < amountRead) {
atomic_store(&_self->bytesHdcdSustained, 0);
} else {
atomic_fetch_sub(&_self->bytesHdcdSustained, amountRead);
}
volumeScale = 0.5;
}
scaleBuffersByVolume(ioData, _self->volume * volumeScale);
if(amountRead < amountToRead) {
// Either underrun, or no data at all. Caller output tends to just
// buffer loop if it doesn't get anything, so always produce a full
// buffer, and silence anything we couldn't supply.
clearBuffers(ioData, (amountToRead - amountRead) / bytesPerPacket, amountRead / bytesPerPacket);
}
return 0;
} }
scaleBuffersByVolume(ioData, _self->volume * volumeScale);
if(amountRead < amountToRead) {
// Either underrun, or no data at all. Caller output tends to just
// buffer loop if it doesn't get anything, so always produce a full
// buffer, and silence anything we couldn't supply.
clearBuffers(ioData, (amountToRead - amountRead) / bytesPerPacket, amountRead / bytesPerPacket);
}
return 0;
}; };
- (id)initWithController:(OutputNode *)c { - (id)initWithController:(OutputNode *)c {
@ -220,7 +222,9 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
} }
if([outputController shouldReset]) { if([outputController shouldReset]) {
[[outputController buffer] reset]; @autoreleasepool {
[[outputController buffer] reset];
}
[outputController setShouldReset:NO]; [outputController setShouldReset:NO];
[delayedEvents removeAllObjects]; [delayedEvents removeAllObjects];
delayedEventsPopped = YES; delayedEventsPopped = YES;
@ -244,10 +248,12 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
size_t frameCount = 0; size_t frameCount = 0;
if(![[outputController buffer] isFull]) { if(![[outputController buffer] isFull]) {
AudioChunk *chunk = [outputController readChunk:512]; @autoreleasepool {
frameCount = [chunk frameCount]; AudioChunk *chunk = [outputController readChunk:512];
if(frameCount) { frameCount = [chunk frameCount];
[[outputController buffer] addChunk:chunk]; if(frameCount) {
[[outputController buffer] addChunk:chunk];
}
} }
} }
@ -467,7 +473,9 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
AVAudioFormat *renderFormat; AVAudioFormat *renderFormat;
[outputController incrementAmountPlayed:[[outputController buffer] listDuration]]; [outputController incrementAmountPlayed:[[outputController buffer] listDuration]];
[[outputController buffer] reset]; @autoreleasepool {
[[outputController buffer] reset];
}
_deviceFormat = format; _deviceFormat = format;
deviceFormat = *(format.streamDescription); deviceFormat = *(format.streamDescription);