From a9dfb2eddf4b8f54c4573a12e3afc55d40294277 Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Mon, 7 Apr 2014 15:28:34 +0100 Subject: [PATCH] Fixed thread related crashes in OutputRenderCallback --- StreamingKit/StreamingKit/STKAudioPlayer.m | 18 ++++++++++++++---- .../StreamingKit/STKLocalFileDataSource.m | 12 ++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 7e3a4e4..30ded81 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -2537,11 +2537,15 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* { STKAudioPlayer* audioPlayer = (__bridge STKAudioPlayer*)inRefCon; + OSSpinLockLock(&audioPlayer->currentEntryReferencesLock); + STKQueueEntry* entry = audioPlayer->currentlyPlayingEntry; + STKQueueEntry* currentlyReadingEntry = audioPlayer->currentlyReadingEntry; + OSSpinLockUnlock(&audioPlayer->currentEntryReferencesLock); + OSSpinLockLock(&audioPlayer->pcmBufferSpinLock); BOOL waitForBuffer = NO; BOOL muted = audioPlayer->muted; - STKQueueEntry* entry = audioPlayer->currentlyPlayingEntry; AudioBuffer* audioBuffer = audioPlayer->pcmAudioBuffer; UInt32 frameSizeInBytes = audioPlayer->pcmBufferFrameSizeInBytes; UInt32 used = audioPlayer->pcmBufferUsedFrameCount; @@ -2559,10 +2563,10 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* if (entry->lastFrameQueued >= 0) { - framesRequiredToStartPlaying = MIN(framesRequiredToStartPlaying, audioPlayer->currentlyPlayingEntry->lastFrameQueued); + framesRequiredToStartPlaying = MIN(framesRequiredToStartPlaying, entry->lastFrameQueued); } - if (entry && audioPlayer->currentlyReadingEntry == entry + if (entry && currentlyReadingEntry == entry && entry->framesQueued < framesRequiredToStartPlaying) { waitForBuffer = YES; @@ -2772,13 +2776,19 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* { pthread_mutex_lock(&audioPlayer->playerMutex); - if (lastFramePlayed && entry == audioPlayer->currentlyPlayingEntry) + OSSpinLockLock(&audioPlayer->currentEntryReferencesLock); + STKQueueEntry* currentlyPlayingEntry = audioPlayer->currentlyPlayingEntry; + OSSpinLockUnlock(&audioPlayer->currentEntryReferencesLock); + + if (lastFramePlayed && entry == currentlyPlayingEntry) { [audioPlayer audioQueueFinishedPlaying:entry]; while (extraFramesPlayedNotAssigned > 0) { + OSSpinLockLock(&audioPlayer->currentEntryReferencesLock); STKQueueEntry* newEntry = audioPlayer->currentlyPlayingEntry; + OSSpinLockUnlock(&audioPlayer->currentEntryReferencesLock); if (newEntry != nil) { diff --git a/StreamingKit/StreamingKit/STKLocalFileDataSource.m b/StreamingKit/StreamingKit/STKLocalFileDataSource.m index aa2ed12..2aded28 100644 --- a/StreamingKit/StreamingKit/STKLocalFileDataSource.m +++ b/StreamingKit/StreamingKit/STKLocalFileDataSource.m @@ -200,6 +200,18 @@ [self open]; } + if (stream == 0) + { + CFRunLoopPerformBlock(eventsRunLoop.getCFRunLoop, NSRunLoopCommonModes, ^ + { + [self errorOccured]; + }); + + CFRunLoopWakeUp(eventsRunLoop.getCFRunLoop); + + return; + } + if (CFReadStreamSetProperty(stream, kCFStreamPropertyFileCurrentOffset, (__bridge CFTypeRef)[NSNumber numberWithLongLong:offset]) != TRUE) { position = 0;