diff --git a/StreamingKit/StreamingKit.xcodeproj/project.pbxproj b/StreamingKit/StreamingKit.xcodeproj/project.pbxproj index 5d6b289..84d70e1 100644 --- a/StreamingKit/StreamingKit.xcodeproj/project.pbxproj +++ b/StreamingKit/StreamingKit.xcodeproj/project.pbxproj @@ -7,6 +7,10 @@ objects = { /* Begin PBXBuildFile section */ + 40B6239322423B1E005D725D /* STKMacro.h in Headers */ = {isa = PBXBuildFile; fileRef = 40B6239222423B1E005D725D /* STKMacro.h */; }; + 40B6239422423B1F005D725D /* STKMacro.h in Headers */ = {isa = PBXBuildFile; fileRef = 40B6239222423B1E005D725D /* STKMacro.h */; }; + 40B6239822423F28005D725D /* STKSpinLock.h in Headers */ = {isa = PBXBuildFile; fileRef = 40B6239722423F28005D725D /* STKSpinLock.h */; }; + 40B6239922423F28005D725D /* STKSpinLock.h in Headers */ = {isa = PBXBuildFile; fileRef = 40B6239722423F28005D725D /* STKSpinLock.h */; }; 5B949CD21A1140E4005675A0 /* STKAudioPlayer.h in Headers */ = {isa = PBXBuildFile; fileRef = A1E7C4F1188D5E550010896F /* STKAudioPlayer.h */; settings = {ATTRIBUTES = (Public, ); }; }; 5B949CD31A1140E4005675A0 /* STKAutoRecoveringHTTPDataSource.h in Headers */ = {isa = PBXBuildFile; fileRef = A1E7C4F3188D5E550010896F /* STKAutoRecoveringHTTPDataSource.h */; settings = {ATTRIBUTES = (Public, ); }; }; 5B949CD41A1140E4005675A0 /* STKCoreFoundationDataSource.h in Headers */ = {isa = PBXBuildFile; fileRef = A1E7C4F5188D5E550010896F /* STKCoreFoundationDataSource.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -91,6 +95,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 40B6239222423B1E005D725D /* STKMacro.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = STKMacro.h; sourceTree = ""; }; + 40B6239722423F28005D725D /* STKSpinLock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = STKSpinLock.h; sourceTree = ""; }; A1A49969189E744400E2A2E2 /* libStreamingKitMac.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libStreamingKitMac.a; sourceTree = BUILT_PRODUCTS_DIR; }; A1A4996A189E744400E2A2E2 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = Library/Frameworks/Cocoa.framework; sourceTree = DEVELOPER_DIR; }; A1A4996D189E744500E2A2E2 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -270,6 +276,8 @@ A1E7C4CD188D57F50010896F /* StreamingKit */ = { isa = PBXGroup; children = ( + A1BF65D3189A65C6004DD08C /* NSMutableArray+STKAudioPlayer.h */, + A1BF65D4189A65C6004DD08C /* NSMutableArray+STKAudioPlayer.m */, A1E7C4F1188D5E550010896F /* STKAudioPlayer.h */, A1E7C4F2188D5E550010896F /* STKAudioPlayer.m */, A1E7C4F3188D5E550010896F /* STKAutoRecoveringHTTPDataSource.h */, @@ -284,10 +292,10 @@ A1E7C4FC188D5E550010896F /* STKHTTPDataSource.m */, A1E7C4FD188D5E550010896F /* STKLocalFileDataSource.h */, A1E7C4FE188D5E550010896F /* STKLocalFileDataSource.m */, + 40B6239222423B1E005D725D /* STKMacro.h */, A1BF65D0189A6582004DD08C /* STKQueueEntry.h */, A1BF65D1189A6582004DD08C /* STKQueueEntry.m */, - A1BF65D3189A65C6004DD08C /* NSMutableArray+STKAudioPlayer.h */, - A1BF65D4189A65C6004DD08C /* NSMutableArray+STKAudioPlayer.m */, + 40B6239722423F28005D725D /* STKSpinLock.h */, A1E7C4CE188D57F50010896F /* Supporting Files */, ); path = StreamingKit; @@ -326,9 +334,11 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 40B6239822423F28005D725D /* STKSpinLock.h in Headers */, 5B949CD21A1140E4005675A0 /* STKAudioPlayer.h in Headers */, 5B949CD31A1140E4005675A0 /* STKAutoRecoveringHTTPDataSource.h in Headers */, 5B949CD41A1140E4005675A0 /* STKCoreFoundationDataSource.h in Headers */, + 40B6239322423B1E005D725D /* STKMacro.h in Headers */, 5B949CD51A1140E4005675A0 /* STKDataSource.h in Headers */, 5B949CD61A1140E4005675A0 /* STKDataSourceWrapper.h in Headers */, 5B949CD71A1140E4005675A0 /* STKHTTPDataSource.h in Headers */, @@ -341,6 +351,8 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 40B6239922423F28005D725D /* STKSpinLock.h in Headers */, + 40B6239422423B1F005D725D /* STKMacro.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 939f438..e0b5ca0 100755 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -255,8 +255,8 @@ static AudioStreamBasicDescription recordAudioStreamBasicDescription; NSMutableArray* upcomingQueue; NSMutableArray* bufferingQueue; - OSSpinLock pcmBufferSpinLock; - OSSpinLock internalStateLock; + os_unfair_lock *pcmBufferSpinLock; + os_unfair_lock *internalStateLock; volatile UInt32 pcmBufferTotalFrameCount; volatile UInt32 pcmBufferFrameStartIndex; volatile UInt32 pcmBufferUsedFrameCount; @@ -289,8 +289,8 @@ static AudioStreamBasicDescription recordAudioStreamBasicDescription; void(^stopBackBackgroundTaskBlock)(); int32_t seekVersion; - OSSpinLock seekLock; - OSSpinLock currentEntryReferencesLock; + os_unfair_lock *seekLock; + os_unfair_lock *currentEntryReferencesLock; pthread_mutex_t playerMutex; pthread_cond_t playerThreadReadyCondition; @@ -451,13 +451,13 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn break; } - OSSpinLockLock(&internalStateLock); + setLock(internalStateLock); waitingForDataAfterSeekFrameCount = 0; if (value == internalState) { - OSSpinLockUnlock(&internalStateLock); + lockUnlock(internalStateLock); return; } @@ -466,7 +466,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn { if (!ifInState(self->internalState)) { - OSSpinLockUnlock(&internalStateLock); + lockUnlock(internalStateLock); return; } @@ -480,7 +480,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn { self.state = newState; - OSSpinLockUnlock(&internalStateLock); + lockUnlock(internalStateLock); dispatch_async(dispatch_get_main_queue(), ^ { @@ -489,7 +489,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn } else { - OSSpinLockUnlock(&internalStateLock); + lockUnlock(internalStateLock); } } @@ -525,6 +525,11 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn { if (self = [super init]) { + pcmBufferSpinLock = &(OS_UNFAIR_LOCK_INIT); + internalStateLock = &(OS_UNFAIR_LOCK_INIT); + seekLock = &(OS_UNFAIR_LOCK_INIT); + currentEntryReferencesLock = &(OS_UNFAIR_LOCK_INIT); + options = optionsIn; self->volume = 1.0; @@ -591,11 +596,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn currentlyPlayingEntry.dataSource.delegate = nil; [currentlyReadingEntry.dataSource unregisterForEvents]; - OSSpinLockLock(¤tEntryReferencesLock); - + setLock(currentEntryReferencesLock); currentlyPlayingEntry = nil; - - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); } [self closeRecordAudioFile]; @@ -999,9 +1002,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn return 0; } - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); STKQueueEntry* entry = currentlyPlayingEntry; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); if (entry == nil) { @@ -1031,18 +1034,18 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn return 0; } - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); STKQueueEntry* entry = currentlyPlayingEntry; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); if (entry == nil) { return 0; } - OSSpinLockLock(&entry->spinLock); + setLock(entry->spinLock); double retval = entry->seekTime + (entry->framesPlayed / canonicalAudioStreamBasicDescription.mSampleRate); - OSSpinLockUnlock(&entry->spinLock); + lockUnlock(entry->spinLock); return retval; } @@ -1110,7 +1113,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn return; } - OSSpinLockLock(&seekLock); + setLock(seekLock); BOOL seekAlreadyRequested = seekToTimeWasRequested; @@ -1121,14 +1124,14 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn { OSAtomicIncrement32(&seekVersion); - OSSpinLockUnlock(&seekLock); + lockUnlock(seekLock); [self wakeupPlaybackThread]; return; } - OSSpinLockUnlock(&seekLock); + lockUnlock(seekLock); } -(void) createPlaybackThread @@ -1179,9 +1182,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn [currentlyReadingEntry.dataSource close]; } - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); currentlyReadingEntry = entry; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); currentlyReadingEntry.dataSource.delegate = self; [currentlyReadingEntry.dataSource registerForEvents:[NSRunLoop currentRunLoop]]; @@ -1225,19 +1228,19 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn { if (!isPlayingSameItemProbablySeek) { - OSSpinLockLock(&next->spinLock); + setLock(next->spinLock); next->seekTime = 0; - OSSpinLockUnlock(&next->spinLock); + lockUnlock(next->spinLock); - OSSpinLockLock(&seekLock); + setLock(seekLock); seekToTimeWasRequested = NO; - OSSpinLockUnlock(&seekLock); + lockUnlock(seekLock); } - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); currentlyPlayingEntry = next; NSObject* playingQueueItemId = playingQueueItemId = currentlyPlayingEntry.queueItemId; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); if (!isPlayingSameItemProbablySeek && entry) { @@ -1259,9 +1262,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn } else { - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); currentlyPlayingEntry = nil; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); if (!isPlayingSameItemProbablySeek && entry) { @@ -1423,21 +1426,21 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn int32_t originalSeekVersion; BOOL originalSeekToTimeRequested; - OSSpinLockLock(&seekLock); + setLock(seekLock); originalSeekVersion = seekVersion; originalSeekToTimeRequested = seekToTimeWasRequested; - OSSpinLockUnlock(&seekLock); + lockUnlock(seekLock); if (originalSeekToTimeRequested && currentlyReadingEntry == currentlyPlayingEntry) { [self processSeekToTime]; - OSSpinLockLock(&seekLock); + setLock(seekLock); if (originalSeekVersion == seekVersion) { seekToTimeWasRequested = NO; } - OSSpinLockUnlock(&seekLock); + lockUnlock(seekLock); } } else if (currentlyPlayingEntry == nil && seekToTimeWasRequested) @@ -1486,10 +1489,10 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn currentlyPlayingEntry.dataSource.delegate = nil; pthread_mutex_lock(&playerMutex); - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); currentlyPlayingEntry = nil; currentlyReadingEntry = nil; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); pthread_mutex_unlock(&playerMutex); [self closeRecordAudioFile]; @@ -1524,9 +1527,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn seekByteOffset = currentEntry.dataSource.length - 2 * currentEntry->packetBufferSize; } - OSSpinLockLock(¤tEntry->spinLock); + setLock(currentEntry->spinLock); currentEntry->seekTime = requestedSeekTime; - OSSpinLockUnlock(¤tEntry->spinLock); + lockUnlock(currentEntry->spinLock); double calculatedBitRate = [currentEntry calculatedBitRate]; @@ -1543,9 +1546,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn if (!(ioFlags & kAudioFileStreamSeekFlag_OffsetIsEstimated)) { double delta = ((seekByteOffset - (SInt64)currentEntry->audioDataOffset) - packetAlignedByteOffset) / calculatedBitRate * 8; - OSSpinLockLock(¤tEntry->spinLock); + setLock(currentEntry->spinLock); currentEntry->seekTime -= delta; - OSSpinLockUnlock(¤tEntry->spinLock); + lockUnlock(currentEntry->spinLock); } } } @@ -1639,7 +1642,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn return; } - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); if (currentlyReadingEntry == nil) { @@ -1647,7 +1650,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn [dataSourceIn close]; } - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); } } @@ -1697,17 +1700,17 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn return; } - OSSpinLockLock(¤tlyReadingEntry->spinLock); + setLock(currentlyReadingEntry->spinLock); currentlyReadingEntry->lastFrameQueued = currentlyReadingEntry->framesQueued; - OSSpinLockUnlock(¤tlyReadingEntry->spinLock); + lockUnlock(currentlyReadingEntry->spinLock); currentlyReadingEntry.dataSource.delegate = nil; [currentlyReadingEntry.dataSource unregisterForEvents]; [currentlyReadingEntry.dataSource close]; - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); currentlyReadingEntry = nil; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); pthread_mutex_unlock(&playerMutex); @@ -1788,7 +1791,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn -(void) resetPcmBuffers { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); self->pcmBufferFrameStartIndex = 0; self->pcmBufferUsedFrameCount = 0; @@ -1797,7 +1800,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn self->averagePowerDb[0] = STK_DBMIN; self->averagePowerDb[1] = STK_DBMIN; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); } -(void) stop @@ -1832,11 +1835,11 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn [self clearQueue]; - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); currentlyPlayingEntry = nil; currentlyReadingEntry = nil; seekToTimeWasRequested = NO; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); } pthread_mutex_unlock(&playerMutex); }]; @@ -1944,20 +1947,20 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn -(NSObject*) currentlyPlayingQueueItemId { - OSSpinLockLock(¤tEntryReferencesLock); + setLock(currentEntryReferencesLock); STKQueueEntry* entry = currentlyPlayingEntry; if (entry == nil) { - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); return nil; } NSObject* retval = entry.queueItemId; - OSSpinLockUnlock(¤tEntryReferencesLock); + lockUnlock(currentEntryReferencesLock); return retval; } @@ -2623,12 +2626,12 @@ OSStatus AudioConverterCallback(AudioConverterRef inAudioConverter, UInt32* ioNu while (true) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); UInt32 used = pcmBufferUsedFrameCount; UInt32 start = pcmBufferFrameStartIndex; UInt32 end = (pcmBufferFrameStartIndex + pcmBufferUsedFrameCount) % pcmBufferTotalFrameCount; UInt32 framesLeftInsideBuffer = pcmBufferTotalFrameCount - used; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); if (framesLeftInsideBuffer == 0) { @@ -2636,12 +2639,12 @@ OSStatus AudioConverterCallback(AudioConverterRef inAudioConverter, UInt32* ioNu while (true) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); used = pcmBufferUsedFrameCount; start = pcmBufferFrameStartIndex; end = (pcmBufferFrameStartIndex + pcmBufferUsedFrameCount) % pcmBufferTotalFrameCount; framesLeftInsideBuffer = pcmBufferTotalFrameCount - used; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); if (framesLeftInsideBuffer > 0) { @@ -2703,13 +2706,13 @@ OSStatus AudioConverterCallback(AudioConverterRef inAudioConverter, UInt32* ioNu if (status == 100) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); pcmBufferUsedFrameCount += framesAdded; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); - OSSpinLockLock(¤tlyReadingEntry->spinLock); + setLock(currentlyReadingEntry->spinLock); currentlyReadingEntry->framesQueued += framesAdded; - OSSpinLockUnlock(¤tlyReadingEntry->spinLock); + lockUnlock(currentlyReadingEntry->spinLock); return; } @@ -2724,13 +2727,13 @@ OSStatus AudioConverterCallback(AudioConverterRef inAudioConverter, UInt32* ioNu if (framesToDecode == 0) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); pcmBufferUsedFrameCount += framesAdded; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); - OSSpinLockLock(¤tlyReadingEntry->spinLock); + setLock(currentlyReadingEntry->spinLock); currentlyReadingEntry->framesQueued += framesAdded; - OSSpinLockUnlock(¤tlyReadingEntry->spinLock); + lockUnlock(currentlyReadingEntry->spinLock); continue; } @@ -2750,25 +2753,25 @@ OSStatus AudioConverterCallback(AudioConverterRef inAudioConverter, UInt32* ioNu if (status == 100) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); pcmBufferUsedFrameCount += framesAdded; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); - OSSpinLockLock(¤tlyReadingEntry->spinLock); + setLock(currentlyReadingEntry->spinLock); currentlyReadingEntry->framesQueued += framesAdded; - OSSpinLockUnlock(¤tlyReadingEntry->spinLock); + lockUnlock(currentlyReadingEntry->spinLock); return; } else if (status == 0) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); pcmBufferUsedFrameCount += framesAdded; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); - OSSpinLockLock(¤tlyReadingEntry->spinLock); + setLock(currentlyReadingEntry->spinLock); currentlyReadingEntry->framesQueued += framesAdded; - OSSpinLockUnlock(¤tlyReadingEntry->spinLock); + lockUnlock(currentlyReadingEntry->spinLock); continue; } @@ -2799,25 +2802,25 @@ OSStatus AudioConverterCallback(AudioConverterRef inAudioConverter, UInt32* ioNu if (status == 100) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); pcmBufferUsedFrameCount += framesAdded; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); - OSSpinLockLock(¤tlyReadingEntry->spinLock); + setLock(currentlyReadingEntry->spinLock); currentlyReadingEntry->framesQueued += framesAdded; - OSSpinLockUnlock(¤tlyReadingEntry->spinLock); + lockUnlock(currentlyReadingEntry->spinLock); return; } else if (status == 0) { - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); pcmBufferUsedFrameCount += framesAdded; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); - OSSpinLockLock(¤tlyReadingEntry->spinLock); + setLock(currentlyReadingEntry->spinLock); currentlyReadingEntry->framesQueued += framesAdded; - OSSpinLockUnlock(¤tlyReadingEntry->spinLock); + lockUnlock(currentlyReadingEntry->spinLock); continue; } @@ -2895,12 +2898,12 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* { STKAudioPlayer* audioPlayer = (__bridge STKAudioPlayer*)inRefCon; - OSSpinLockLock(&audioPlayer->currentEntryReferencesLock); + setLock(audioPlayer->currentEntryReferencesLock); STKQueueEntry* entry = audioPlayer->currentlyPlayingEntry; STKQueueEntry* currentlyReadingEntry = audioPlayer->currentlyReadingEntry; - OSSpinLockUnlock(&audioPlayer->currentEntryReferencesLock); + lockUnlock(audioPlayer->currentEntryReferencesLock); - OSSpinLockLock(&audioPlayer->pcmBufferSpinLock); + setLock(audioPlayer->pcmBufferSpinLock); BOOL waitForBuffer = NO; BOOL muted = audioPlayer->muted; @@ -2960,7 +2963,7 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* } } - OSSpinLockUnlock(&audioPlayer->pcmBufferSpinLock); + lockUnlock(audioPlayer->pcmBufferSpinLock); UInt32 totalFramesCopied = 0; @@ -2993,10 +2996,10 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* totalFramesCopied = framesToCopy; - OSSpinLockLock(&audioPlayer->pcmBufferSpinLock); + setLock(audioPlayer->pcmBufferSpinLock); audioPlayer->pcmBufferFrameStartIndex = (audioPlayer->pcmBufferFrameStartIndex + totalFramesCopied) % audioPlayer->pcmBufferTotalFrameCount; audioPlayer->pcmBufferUsedFrameCount -= totalFramesCopied; - OSSpinLockUnlock(&audioPlayer->pcmBufferSpinLock); + lockUnlock(audioPlayer->pcmBufferSpinLock); } else { @@ -3036,10 +3039,10 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* totalFramesCopied = framesToCopy + moreFramesToCopy; - OSSpinLockLock(&audioPlayer->pcmBufferSpinLock); + setLock(audioPlayer->pcmBufferSpinLock); audioPlayer->pcmBufferFrameStartIndex = (audioPlayer->pcmBufferFrameStartIndex + totalFramesCopied) % audioPlayer->pcmBufferTotalFrameCount; audioPlayer->pcmBufferUsedFrameCount -= totalFramesCopied; - OSSpinLockUnlock(&audioPlayer->pcmBufferSpinLock); + lockUnlock(audioPlayer->pcmBufferSpinLock); } [audioPlayer setInternalState:STKAudioPlayerInternalStatePlaying ifInState:^BOOL(STKAudioPlayerInternalState state) @@ -3113,7 +3116,7 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* return 0; } - OSSpinLockLock(&entry->spinLock); + setLock(entry->spinLock); SInt64 extraFramesPlayedNotAssigned = 0; SInt64 framesPlayedForCurrent = totalFramesCopied; @@ -3128,15 +3131,15 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* BOOL lastFramePlayed = entry->framesPlayed == entry->lastFrameQueued; - OSSpinLockUnlock(&entry->spinLock); + lockUnlock(entry->spinLock); if (signal || lastFramePlayed) { pthread_mutex_lock(&audioPlayer->playerMutex); - OSSpinLockLock(&audioPlayer->currentEntryReferencesLock); + setLock(audioPlayer->currentEntryReferencesLock); STKQueueEntry* currentlyPlayingEntry = audioPlayer->currentlyPlayingEntry; - OSSpinLockUnlock(&audioPlayer->currentEntryReferencesLock); + lockUnlock(audioPlayer->currentEntryReferencesLock); if (lastFramePlayed && entry == currentlyPlayingEntry) { @@ -3144,15 +3147,15 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* while (extraFramesPlayedNotAssigned > 0) { - OSSpinLockLock(&audioPlayer->currentEntryReferencesLock); + setLock(audioPlayer->currentEntryReferencesLock); STKQueueEntry* newEntry = audioPlayer->currentlyPlayingEntry; - OSSpinLockUnlock(&audioPlayer->currentEntryReferencesLock); + lockUnlock(audioPlayer->currentEntryReferencesLock); if (newEntry != nil) { SInt64 framesPlayedForCurrent = extraFramesPlayedNotAssigned; - OSSpinLockLock(&newEntry->spinLock); + setLock(newEntry->spinLock); if (newEntry->lastFrameQueued > 0) { @@ -3163,13 +3166,13 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* if (newEntry->framesPlayed == newEntry->lastFrameQueued) { - OSSpinLockUnlock(&newEntry->spinLock); + lockUnlock(newEntry->spinLock); [audioPlayer audioQueueFinishedPlaying:newEntry]; } else { - OSSpinLockUnlock(&newEntry->spinLock); + lockUnlock(newEntry->spinLock); } extraFramesPlayedNotAssigned -= framesPlayedForCurrent; @@ -3415,7 +3418,7 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* NSArray* replacement = [NSArray arrayWithArray:newFrameFilters]; - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); if (newFrameFilters.count > 0) { frameFilters = replacement; @@ -3424,7 +3427,7 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* { frameFilters = nil; } - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); pthread_mutex_unlock(&self->playerMutex); } @@ -3455,9 +3458,9 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* NSArray* replacement = [NSArray arrayWithArray:newFrameFilters]; - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); frameFilters = replacement; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); pthread_mutex_unlock(&self->playerMutex); } @@ -3488,9 +3491,9 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* NSArray* replacement = [NSArray arrayWithArray:newFrameFilters]; - OSSpinLockLock(&pcmBufferSpinLock); + setLock(pcmBufferSpinLock); frameFilters = replacement; - OSSpinLockUnlock(&pcmBufferSpinLock); + lockUnlock(pcmBufferSpinLock); pthread_mutex_unlock(&self->playerMutex); } diff --git a/StreamingKit/StreamingKit/STKMacro.h b/StreamingKit/StreamingKit/STKMacro.h new file mode 100644 index 0000000..9e13485 --- /dev/null +++ b/StreamingKit/StreamingKit/STKMacro.h @@ -0,0 +1,29 @@ +// +// STKMacro.h +// StreamingKit +// +// Created by Diego Stamigni on 20/03/2019. +// Copyright © 2019 Thong Nguyen. All rights reserved. +// + +#pragma once + +#import + +#if TARGET_OS_IPHONE + #import + #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) \ + ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) +#endif + +#define DEPLOYMENT_TARGET_HIGHER_THAN_10 TARGET_OS_WATCH || \ + TARGET_OS_TV || \ + (TARGET_OS_IOS &&__IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || \ + (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_ALLOWED >= 101200) + +#define BASE_SDK_HIGHER_THAN_10 (TARGET_OS_WATCH || \ + TARGET_OS_TV || \ + (TARGET_OS_IOS &&__IPHONE_OS_VERSION_MAX_ALLOWED >= 100000) || \ + (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101200)) + +#define DEVICE_HIGHER_THAN_10 objc_getClass("NSDimension") diff --git a/StreamingKit/StreamingKit/STKQueueEntry.h b/StreamingKit/StreamingKit/STKQueueEntry.h index f13d277..943c6ff 100755 --- a/StreamingKit/StreamingKit/STKQueueEntry.h +++ b/StreamingKit/StreamingKit/STKQueueEntry.h @@ -7,7 +7,7 @@ // #import "STKDataSource.h" -#import "libkern/OSAtomic.h" +#import "STKSpinLock.h" #import "AudioToolbox/AudioToolbox.h" NS_ASSUME_NONNULL_BEGIN @@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN @interface STKQueueEntry : NSObject { @public - OSSpinLock spinLock; + os_unfair_lock *spinLock; BOOL parsedHeader; Float64 sampleRate; diff --git a/StreamingKit/StreamingKit/STKQueueEntry.m b/StreamingKit/StreamingKit/STKQueueEntry.m index 0c1d1e3..fb56afc 100755 --- a/StreamingKit/StreamingKit/STKQueueEntry.m +++ b/StreamingKit/StreamingKit/STKQueueEntry.m @@ -18,7 +18,7 @@ { if (self = [super init]) { - self->spinLock = OS_SPINLOCK_INIT; + self->spinLock = &(OS_UNFAIR_LOCK_INIT); self.dataSource = dataSourceIn; self.queueItemId = queueItemIdIn; @@ -31,11 +31,11 @@ -(void) reset { - OSSpinLockLock(&self->spinLock); + setLock(self->spinLock); self->framesQueued = 0; self->framesPlayed = 0; self->lastFrameQueued = -1; - OSSpinLockUnlock(&self->spinLock); + lockUnlock(self->spinLock); } -(double) calculatedBitRate @@ -109,9 +109,9 @@ -(Float64) progressInFrames { - OSSpinLockLock(&self->spinLock); + setLock(self->spinLock); Float64 retval = (self->seekTime + self->audioStreamBasicDescription.mSampleRate) + self->framesPlayed; - OSSpinLockUnlock(&self->spinLock); + lockUnlock(self->spinLock); return retval; } diff --git a/StreamingKit/StreamingKit/STKSpinLock.h b/StreamingKit/StreamingKit/STKSpinLock.h new file mode 100644 index 0000000..3359666 --- /dev/null +++ b/StreamingKit/StreamingKit/STKSpinLock.h @@ -0,0 +1,65 @@ +// +// STKSpinLock.h +// StreamingKit +// +// Created by Diego Stamigni on 20/03/2019. +// Copyright © 2019 Thong Nguyen. All rights reserved. +// + +#pragma once + +#import "STKMacro.h" +#include + +#if BASE_SDK_HIGHER_THAN_10 +#import +#else +#define OS_UNFAIR_LOCK_INIT ((os_unfair_lock){0}) + +typedef struct _os_unfair_lock_s { + uint32_t _os_unfair_lock_opaque; +} os_unfair_lock, *os_unfair_lock_t; +#endif + +#if !DEPLOYMENT_TARGET_HIGHER_THAN_10 +#import +#endif + +static void setLock(os_unfair_lock *lock) +{ +#if DEPLOYMENT_TARGET_HIGHER_THAN_10 + os_unfair_lock_lock(lock); +#else + if (DEVICE_HIGHER_THAN_10) + { + void (*os_unfair_lock_lock)(void *lock) = dlsym(dlopen(NULL, RTLD_NOW | RTLD_GLOBAL), "os_unfair_lock_lock"); + if (os_unfair_lock_lock != NULL) + { + os_unfair_lock_lock(lock); + return; + } + } + + OSSpinLockLock((void *)lock); +#endif +} + +static void lockUnlock(os_unfair_lock *lock) +{ +#if DEPLOYMENT_TARGET_HIGHER_THAN_10 + os_unfair_lock_unlock(lock); +#else + if (DEVICE_HIGHER_THAN_10) + { + void (*os_unfair_lock_unlock)(void *lock) = dlsym(dlopen(NULL, RTLD_NOW | RTLD_GLOBAL), "os_unfair_lock_unlock"); + if (os_unfair_lock_unlock != NULL) + { + os_unfair_lock_unlock(lock); + return; + } + } + + OSSpinLockUnlock((void *)lock); +#endif +} +