STKSpinLock helper around OSSpinLock / os_unfair_lock added

OSSpinLock replaced with custom back compatible version of os_unfair_lock
This commit is contained in:
Diego Stamigni
2019-03-20 09:57:30 +00:00
parent d224c48cb9
commit d8aca1ec43
6 changed files with 224 additions and 115 deletions
@@ -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 = "<group>"; };
40B6239722423F28005D725D /* STKSpinLock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = STKSpinLock.h; sourceTree = "<group>"; };
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;
};
+109 -106
View File
@@ -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(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
currentlyPlayingEntry = nil;
OSSpinLockUnlock(&currentEntryReferencesLock);
lockUnlock(currentEntryReferencesLock);
}
[self closeRecordAudioFile];
@@ -999,9 +1002,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn
return 0;
}
OSSpinLockLock(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
STKQueueEntry* entry = currentlyPlayingEntry;
OSSpinLockUnlock(&currentEntryReferencesLock);
lockUnlock(currentEntryReferencesLock);
if (entry == nil)
{
@@ -1031,18 +1034,18 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn
return 0;
}
OSSpinLockLock(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
STKQueueEntry* entry = currentlyPlayingEntry;
OSSpinLockUnlock(&currentEntryReferencesLock);
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(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
currentlyReadingEntry = entry;
OSSpinLockUnlock(&currentEntryReferencesLock);
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(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
currentlyPlayingEntry = next;
NSObject* playingQueueItemId = playingQueueItemId = currentlyPlayingEntry.queueItemId;
OSSpinLockUnlock(&currentEntryReferencesLock);
lockUnlock(currentEntryReferencesLock);
if (!isPlayingSameItemProbablySeek && entry)
{
@@ -1259,9 +1262,9 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn
}
else
{
OSSpinLockLock(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
currentlyPlayingEntry = nil;
OSSpinLockUnlock(&currentEntryReferencesLock);
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(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
currentlyPlayingEntry = nil;
currentlyReadingEntry = nil;
OSSpinLockUnlock(&currentEntryReferencesLock);
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(&currentEntry->spinLock);
setLock(currentEntry->spinLock);
currentEntry->seekTime = requestedSeekTime;
OSSpinLockUnlock(&currentEntry->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(&currentEntry->spinLock);
setLock(currentEntry->spinLock);
currentEntry->seekTime -= delta;
OSSpinLockUnlock(&currentEntry->spinLock);
lockUnlock(currentEntry->spinLock);
}
}
}
@@ -1639,7 +1642,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn
return;
}
OSSpinLockLock(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
if (currentlyReadingEntry == nil)
{
@@ -1647,7 +1650,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn
[dataSourceIn close];
}
OSSpinLockUnlock(&currentEntryReferencesLock);
lockUnlock(currentEntryReferencesLock);
}
}
@@ -1697,17 +1700,17 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn
return;
}
OSSpinLockLock(&currentlyReadingEntry->spinLock);
setLock(currentlyReadingEntry->spinLock);
currentlyReadingEntry->lastFrameQueued = currentlyReadingEntry->framesQueued;
OSSpinLockUnlock(&currentlyReadingEntry->spinLock);
lockUnlock(currentlyReadingEntry->spinLock);
currentlyReadingEntry.dataSource.delegate = nil;
[currentlyReadingEntry.dataSource unregisterForEvents];
[currentlyReadingEntry.dataSource close];
OSSpinLockLock(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
currentlyReadingEntry = nil;
OSSpinLockUnlock(&currentEntryReferencesLock);
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(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
currentlyPlayingEntry = nil;
currentlyReadingEntry = nil;
seekToTimeWasRequested = NO;
OSSpinLockUnlock(&currentEntryReferencesLock);
lockUnlock(currentEntryReferencesLock);
}
pthread_mutex_unlock(&playerMutex);
}];
@@ -1944,20 +1947,20 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn
-(NSObject*) currentlyPlayingQueueItemId
{
OSSpinLockLock(&currentEntryReferencesLock);
setLock(currentEntryReferencesLock);
STKQueueEntry* entry = currentlyPlayingEntry;
if (entry == nil)
{
OSSpinLockUnlock(&currentEntryReferencesLock);
lockUnlock(currentEntryReferencesLock);
return nil;
}
NSObject* retval = entry.queueItemId;
OSSpinLockUnlock(&currentEntryReferencesLock);
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(&currentlyReadingEntry->spinLock);
setLock(currentlyReadingEntry->spinLock);
currentlyReadingEntry->framesQueued += framesAdded;
OSSpinLockUnlock(&currentlyReadingEntry->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(&currentlyReadingEntry->spinLock);
setLock(currentlyReadingEntry->spinLock);
currentlyReadingEntry->framesQueued += framesAdded;
OSSpinLockUnlock(&currentlyReadingEntry->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(&currentlyReadingEntry->spinLock);
setLock(currentlyReadingEntry->spinLock);
currentlyReadingEntry->framesQueued += framesAdded;
OSSpinLockUnlock(&currentlyReadingEntry->spinLock);
lockUnlock(currentlyReadingEntry->spinLock);
return;
}
else if (status == 0)
{
OSSpinLockLock(&pcmBufferSpinLock);
setLock(pcmBufferSpinLock);
pcmBufferUsedFrameCount += framesAdded;
OSSpinLockUnlock(&pcmBufferSpinLock);
lockUnlock(pcmBufferSpinLock);
OSSpinLockLock(&currentlyReadingEntry->spinLock);
setLock(currentlyReadingEntry->spinLock);
currentlyReadingEntry->framesQueued += framesAdded;
OSSpinLockUnlock(&currentlyReadingEntry->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(&currentlyReadingEntry->spinLock);
setLock(currentlyReadingEntry->spinLock);
currentlyReadingEntry->framesQueued += framesAdded;
OSSpinLockUnlock(&currentlyReadingEntry->spinLock);
lockUnlock(currentlyReadingEntry->spinLock);
return;
}
else if (status == 0)
{
OSSpinLockLock(&pcmBufferSpinLock);
setLock(pcmBufferSpinLock);
pcmBufferUsedFrameCount += framesAdded;
OSSpinLockUnlock(&pcmBufferSpinLock);
lockUnlock(pcmBufferSpinLock);
OSSpinLockLock(&currentlyReadingEntry->spinLock);
setLock(currentlyReadingEntry->spinLock);
currentlyReadingEntry->framesQueued += framesAdded;
OSSpinLockUnlock(&currentlyReadingEntry->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);
}
+29
View File
@@ -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 <objc/runtime.h>
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#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")
+2 -2
View File
@@ -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;
+5 -5
View File
@@ -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;
}
+65
View File
@@ -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 <dlfcn.h>
#if BASE_SDK_HIGHER_THAN_10
#import <os/lock.h>
#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 <libkern/OSAtomic.h>
#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
}