From 381795fed6b5599e509f2b7e687ca72da472f83d Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Sun, 26 Jan 2014 15:43:39 +0000 Subject: [PATCH] Fixed potential deadlock with clearQueue invokes callback on playback thread that may be blocking/waiting on audio packets --- StreamingKit/StreamingKit/STKAudioPlayer.m | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 8d74230..ff588f7 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -346,7 +346,6 @@ AudioQueueBufferRefLookupEntry; @property (readwrite) AudioPlayerInternalState stateBeforePaused; -(void) logInfo:(NSString*)line; --(void) processQueue:(BOOL)skipCurrent; -(void) createAudioQueue; -(void) enqueueBuffer; -(void) resetAudioQueueWithReason:(NSString*)reason; @@ -721,12 +720,16 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ [self startSystemBackgroundTask]; + [self resetAudioQueueWithReason:@"from skipCurrent"]; [self clearQueue]; - + [upcomingQueue enqueue:[[STKQueueEntry alloc] initWithDataSource:dataSourceIn andQueueItemId:queueItemId]]; self.internalState = AudioPlayerInternalStateRunning; - [self processQueue:YES]; + + newFileToPlay = YES; + + [self createOrWakeupPlaybackThread]; } pthread_mutex_unlock(&playerMutex); }]; @@ -742,7 +745,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ LOGINFO(([NSString stringWithFormat:@"Queueing: %@ (buffering.count=%d, upcoming.count=%d)", [queueItemId description], bufferingQueue.count, upcomingQueue.count])); - [self processQueue:NO]; + [self createOrWakeupPlaybackThread]; } pthread_mutex_unlock(&playerMutex); }]; @@ -1904,7 +1907,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ pthread_mutex_unlock(&playerMutex); } --(void) processQueue:(BOOL)skipCurrent +-(void) createOrWakeupPlaybackThread { if (playbackThread == nil) { @@ -1918,13 +1921,6 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ } else { - if (skipCurrent) - { - newFileToPlay = YES; - - [self resetAudioQueueWithReason:@"from skipCurrent"]; - } - [self wakeupPlaybackThread]; } }