From c5a5eed997247a1d4bd6c3ba7ce4bb410b756271 Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Sun, 10 Mar 2013 17:31:26 +0000 Subject: [PATCH] Changed lock around currentlyPlayingEntry from playerMutex to independent spinlock --- Audjustable/Classes/AudioPlayer/AudioPlayer.h | 1 + Audjustable/Classes/AudioPlayer/AudioPlayer.m | 100 +++++++++++------- 2 files changed, 63 insertions(+), 38 deletions(-) diff --git a/Audjustable/Classes/AudioPlayer/AudioPlayer.h b/Audjustable/Classes/AudioPlayer/AudioPlayer.h index 7d75308..fe4e7fa 100644 --- a/Audjustable/Classes/AudioPlayer/AudioPlayer.h +++ b/Audjustable/Classes/AudioPlayer/AudioPlayer.h @@ -159,6 +159,7 @@ AudioQueueBufferRefLookupEntry; AudioPlayerErrorCode errorCode; AudioPlayerStopReason stopReason; + int currentlyPlayingLock; pthread_mutex_t playerMutex; pthread_mutex_t queueBuffersMutex; pthread_cond_t queueBufferReadyCondition; diff --git a/Audjustable/Classes/AudioPlayer/AudioPlayer.m b/Audjustable/Classes/AudioPlayer/AudioPlayer.m index 15b53d1..5d2f87e 100644 --- a/Audjustable/Classes/AudioPlayer/AudioPlayer.m +++ b/Audjustable/Classes/AudioPlayer/AudioPlayer.m @@ -709,13 +709,12 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ } } - AudioQueueBufferRef bufferToFill = audioQueueBuffer[fillBufferIndex]; - - if (bytesFilled + packetSize > bufferToFill->mAudioDataBytesCapacity) + if (bytesFilled + packetSize > currentlyReadingEntry->packetBufferSize) { return; } - + + AudioQueueBufferRef bufferToFill = audioQueueBuffer[fillBufferIndex]; memcpy((char*)bufferToFill->mAudioData + bytesFilled, (const char*)inputData + packetOffset, packetSize); packetDescs[packetsFilled] = packetDescriptionsIn[i]; @@ -802,20 +801,23 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ QueueEntry* entry = nil; - pthread_mutex_lock(&playerMutex); - if (currentlyPlayingEntry) { - entry = currentlyPlayingEntry; - - if (!audioQueueFlushing) + SPIN_LOCK_LOCK(¤tlyPlayingLock); { - currentlyPlayingEntry->bytesPlayed += bufferIn->mAudioDataByteSize; + if (currentlyPlayingEntry) + { + entry = currentlyPlayingEntry; + + if (!audioQueueFlushing) + { + currentlyPlayingEntry->bytesPlayed += bufferIn->mAudioDataByteSize; + } + } } + SPIN_LOCK_UNLOCK(¤tlyPlayingLock); } - pthread_mutex_unlock(&playerMutex); - int index = (int)bufferIn % audioQueueBufferRefLookupCount; for (int i = 0; i < audioQueueBufferCount; i++) @@ -1315,13 +1317,15 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ -(void) audioQueueFinishedPlaying:(QueueEntry*)entry { pthread_mutex_lock(&playerMutex); - pthread_mutex_lock(&queueBuffersMutex); - - QueueEntry* next = [bufferingQueue dequeue]; - - [self processDidFinishPlaying:entry withNext:next]; - - pthread_mutex_unlock(&queueBuffersMutex); + { + pthread_mutex_lock(&queueBuffersMutex); + { + QueueEntry* next = [bufferingQueue dequeue]; + + [self processDidFinishPlaying:entry withNext:next]; + } + pthread_mutex_unlock(&queueBuffersMutex); + } pthread_mutex_unlock(&playerMutex); } @@ -1347,10 +1351,13 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ seekToTimeWasRequested = NO; } - pthread_mutex_lock(&playerMutex); - currentlyPlayingEntry = next; + SPIN_LOCK_LOCK(¤tlyPlayingLock); + { + currentlyPlayingEntry = next; + } + SPIN_LOCK_UNLOCK(¤tlyPlayingLock); + currentlyPlayingEntry->bytesPlayed = 0; - pthread_mutex_unlock(&playerMutex); NSObject* playingQueueItemId = currentlyPlayingEntry.queueItemId; @@ -1372,9 +1379,11 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ } else { - pthread_mutex_lock(&playerMutex); - currentlyPlayingEntry = nil; - pthread_mutex_unlock(&playerMutex); + SPIN_LOCK_LOCK(¤tlyPlayingLock); + { + currentlyPlayingEntry = nil; + } + SPIN_LOCK_UNLOCK(¤tlyPlayingLock); if (currentlyReadingEntry == nil) { @@ -1441,7 +1450,12 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ [bufferingQueue dequeue]; } - currentlyPlayingEntry = nil; + SPIN_LOCK_LOCK(¤tlyPlayingLock); + { + currentlyPlayingEntry = nil; + } + SPIN_LOCK_UNLOCK(¤tlyPlayingLock); + currentlyReadingEntry = nil; seekToTimeWasRequested = NO; @@ -1464,7 +1478,12 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ [bufferingQueue dequeue]; } - currentlyPlayingEntry = nil; + SPIN_LOCK_LOCK(¤tlyPlayingLock); + { + currentlyPlayingEntry = nil; + } + SPIN_LOCK_UNLOCK(¤tlyPlayingLock); + currentlyReadingEntry = nil; pthread_mutex_unlock(&queueBuffersMutex); @@ -1562,7 +1581,12 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ currentlyPlayingEntry.dataSource.delegate = nil; currentlyReadingEntry = nil; - currentlyPlayingEntry = nil; + + SPIN_LOCK_LOCK(¤tlyPlayingLock); + { + currentlyPlayingEntry = nil; + } + SPIN_LOCK_UNLOCK(¤tlyPlayingLock); self.internalState = AudioPlayerInternalStateDisposed; @@ -1713,22 +1737,22 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ OSStatus error; pthread_mutex_lock(&playerMutex); - - audioQueueFlushing = YES; - - if (audioQueue) { - error = AudioQueueReset(audioQueue); + audioQueueFlushing = YES; - if (error) + if (audioQueue) { - dispatch_async(dispatch_get_main_queue(), ^ + error = AudioQueueReset(audioQueue); + + if (error) { - [self didEncounterError:AudioPlayerErrorQueueStopFailed];; - }); + dispatch_async(dispatch_get_main_queue(), ^ + { + [self didEncounterError:AudioPlayerErrorQueueStopFailed];; + }); + } } } - pthread_mutex_unlock(&playerMutex); pthread_mutex_lock(&queueBuffersMutex);