Compare commits

...

10 Commits

Author SHA1 Message Date
Thong Nguyen 8da9ea9545 Updated podspec to 0.0.6 2013-07-17 15:59:59 +01:00
Thong Nguyen ee387dc098 Fixed deadlock 2013-07-17 15:48:18 +01:00
Thong Nguyen c2d5d4000d Fixed playerMutex not being released sometimes (bug from last commit) 2013-07-16 16:31:45 +01:00
Thong Nguyen 65c1715269 Fix potential threading crash in [AudioPlayer duration] (I think). Moved to Apple provided spinlock implementation 2013-07-16 16:09:52 +01:00
Thong Nguyen 05965388cc Merge pull request #29 from steveluscher/patch-1
Readme: spelling, etc.
2013-07-10 07:17:54 -07:00
Steven Luscher ba4e44b8a2 Readme: spelling, etc. 2013-07-06 11:59:32 -07:00
Thong Nguyen 08287bc6a7 Merge pull request #24 from gangverk/master
Add support for HE-AAC
2013-05-17 11:26:45 -07:00
Piers dd3bd913b5 Update podspec to version 0.0.5 2013-05-17 17:22:32 +00:00
Piers f8887b7ff1 Add support for HE-AAC
The code for handling HE-AAC has been copied from Matt Gallagher's
AudioStreamer library.

https://github.com/mattgallagher/AudioStreamer/blob/master/Classes/AudioStreamer.m#L1605
2013-05-15 16:29:24 +00:00
Thong Nguyen 6d2b656a58 Updated README 2013-05-03 18:46:34 +01:00
3 changed files with 166 additions and 116 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Audjustable"
s.version = "0.0.4"
s.version = "0.0.6"
s.summary = "A fast and extensible audio streamer for iOS and OSX with support for gapless playback and custom (non-HTTP) sources."
s.homepage = "http://tumtumtum.github.com/audjustable/"
s.license = 'MIT'
+159 -111
View File
@@ -48,12 +48,6 @@
#define OSSTATUS_PARAM_ERROR (-50)
#define SPIN_LOCK_LOCK(x) \
while(OSAtomicCompareAndSwapInt(0, 1, x) == false);
#define SPIN_LOCK_UNLOCK(x) \
*x = 0;
@interface NSMutableArray(AudioPlayerExtensions)
-(void) enqueue:(id)obj;
-(id) dequeue;
@@ -219,7 +213,7 @@
double calculatedBitRate = [self calculatedBitRate];
if (calculatedBitRate == 0 || self->dataSource.length == 0)
if (calculatedBitRate == 0 || dataSource.length == 0)
{
return 0;
}
@@ -321,9 +315,9 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
if ([self.delegate respondsToSelector:@selector(audioPlayer:internalStateChanged:)])
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self.delegate audioPlayer:self internalStateChanged:internalState];
});
{
[self.delegate audioPlayer:self internalStateChanged:internalState];
});
}
AudioPlayerState newState;
@@ -393,7 +387,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
{
fastApiQueue = [[NSOperationQueue alloc] init];
[fastApiQueue setMaxConcurrentOperationCount:1];
readBufferSize = readBufferSizeIn;
readBuffer = calloc(sizeof(UInt8), readBufferSize);
@@ -534,12 +528,12 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
[upcomingQueue removeAllObjects];
dispatch_async(dispatch_get_main_queue(), ^
{
if ([self.delegate respondsToSelector:@selector(audioPlayer:didCancelQueuedItems:)])
{
[self.delegate audioPlayer:self didCancelQueuedItems:array];
}
});
{
if ([self.delegate respondsToSelector:@selector(audioPlayer:didCancelQueuedItems:)])
{
[self.delegate audioPlayer:self didCancelQueuedItems:array];
}
});
}
pthread_mutex_unlock(&playerMutex);
}
@@ -554,34 +548,34 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
[fastApiQueue cancelAllOperations];
[fastApiQueue addOperationWithBlock:^
{
pthread_mutex_lock(&playerMutex);
{
[self startSystemBackgroundTask];
[self clearQueue];
[upcomingQueue enqueue:[[QueueEntry alloc] initWithDataSource:dataSourceIn andQueueItemId:queueItemId]];
self.internalState = AudioPlayerInternalStateRunning;
[self processQueue:YES];
}
pthread_mutex_unlock(&playerMutex);
}];
{
pthread_mutex_lock(&playerMutex);
{
[self startSystemBackgroundTask];
[self clearQueue];
[upcomingQueue enqueue:[[QueueEntry alloc] initWithDataSource:dataSourceIn andQueueItemId:queueItemId]];
self.internalState = AudioPlayerInternalStateRunning;
[self processQueue:YES];
}
pthread_mutex_unlock(&playerMutex);
}];
}
-(void) queueDataSource:(DataSource*)dataSourceIn withQueueItemId:(NSObject*)queueItemId
{
[fastApiQueue addOperationWithBlock:^
{
pthread_mutex_lock(&playerMutex);
{
[upcomingQueue enqueue:[[QueueEntry alloc] initWithDataSource:dataSourceIn andQueueItemId:queueItemId]];
[self processQueue:NO];
}
pthread_mutex_unlock(&playerMutex);
}];
{
pthread_mutex_lock(&playerMutex);
{
[upcomingQueue enqueue:[[QueueEntry alloc] initWithDataSource:dataSourceIn andQueueItemId:queueItemId]];
[self processQueue:NO];
}
pthread_mutex_unlock(&playerMutex);
}];
}
-(void) handlePropertyChangeForFileStream:(AudioFileStreamID)inAudioFileStream fileStreamPropertyID:(AudioFileStreamPropertyID)inPropertyID ioFlags:(UInt32*)ioFlags
@@ -605,12 +599,13 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
break;
case kAudioFileStreamProperty_DataFormat:
{
AudioStreamBasicDescription newBasicDescription;
UInt32 size = sizeof(newBasicDescription);
AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_DataFormat, &size, &newBasicDescription);
currentlyReadingEntry->audioStreamBasicDescription = newBasicDescription;
if (currentlyReadingEntry->audioStreamBasicDescription.mSampleRate == 0) {
AudioStreamBasicDescription newBasicDescription;
UInt32 size = sizeof(newBasicDescription);
AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_DataFormat, &size, &newBasicDescription);
currentlyReadingEntry->audioStreamBasicDescription = newBasicDescription;
}
currentlyReadingEntry->sampleRate = currentlyReadingEntry->audioStreamBasicDescription.mSampleRate;
currentlyReadingEntry->packetDuration = currentlyReadingEntry->audioStreamBasicDescription.mFramesPerPacket / currentlyReadingEntry->sampleRate;
@@ -652,6 +647,44 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
discontinuous = YES;
}
break;
case kAudioFileStreamProperty_FormatList:
{
Boolean outWriteable;
UInt32 formatListSize;
OSStatus err = AudioFileStreamGetPropertyInfo(inAudioFileStream, kAudioFileStreamProperty_FormatList, &formatListSize, &outWriteable);
if (err)
{
break;
}
AudioFormatListItem *formatList = malloc(formatListSize);
err = AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_FormatList, &formatListSize, formatList);
if (err)
{
free(formatList);
break;
}
for (int i = 0; i * sizeof(AudioFormatListItem) < formatListSize; i += sizeof(AudioFormatListItem))
{
AudioStreamBasicDescription pasbd = formatList[i].mASBD;
if (pasbd.mFormatID == kAudioFormatMPEG4AAC_HE ||
pasbd.mFormatID == kAudioFormatMPEG4AAC_HE_V2)
{
//
// We've found HE-AAC, remember this to tell the audio queue
// when we construct it.
//
#if !TARGET_IPHONE_SIMULATOR
currentlyReadingEntry->audioStreamBasicDescription = pasbd;
#endif
break;
}
}
free(formatList);
}
break;
}
}
@@ -815,7 +848,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
if (currentlyPlayingEntry)
{
SPIN_LOCK_LOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
{
if (currentlyPlayingEntry)
{
@@ -827,7 +860,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
}
}
}
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockUnlock(&currentlyPlayingLock);
}
int index = (int)bufferIn % audioQueueBufferRefLookupCount;
@@ -876,7 +909,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
if (!audioQueueFlushing && [self progress] > 4.0 && numberOfBuffersUsed == 0 ) {
self.internalState = AudioPlayerInternalStateRebuffering;
}
if (!audioQueueFlushing)
{
@@ -889,9 +922,9 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
if (playbackThread)
{
CFRunLoopPerformBlock([playbackThreadRunLoop getCFRunLoop], NSDefaultRunLoopMode, ^
{
[self audioQueueFinishedPlaying:entry];
});
{
[self audioQueueFinishedPlaying:entry];
});
CFRunLoopWakeUp([playbackThreadRunLoop getCFRunLoop]);
@@ -900,7 +933,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
}
}
}
if (self.internalState == AudioPlayerInternalStateStopped
|| self.internalState == AudioPlayerInternalStateStopping
|| self.internalState == AudioPlayerInternalStateDisposed
@@ -1040,7 +1073,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
pthread_mutex_unlock(&playerMutex);
pthread_mutex_lock(&queueBuffersMutex);
waiting = YES;
while (bufferUsed[fillBufferIndex] && !(seekToTimeWasRequested || self.internalState == AudioPlayerInternalStateStopped || self.internalState == AudioPlayerInternalStateStopping || self.internalState == AudioPlayerInternalStateDisposed))
@@ -1080,9 +1113,9 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
audioQueue = nil;
}
SPIN_LOCK_LOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
currentAudioStreamBasicDescription = currentlyPlayingEntry->audioStreamBasicDescription;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockUnlock(&currentlyPlayingLock);
error = AudioQueueNewOutput(&currentlyPlayingEntry->audioStreamBasicDescription, AudioQueueOutputCallbackProc, (__bridge void*)self, NULL, NULL, 0, &audioQueue);
@@ -1187,16 +1220,22 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
return 0;
}
SPIN_LOCK_LOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
QueueEntry* entry = currentlyPlayingEntry;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
if (entry == nil)
{
OSSpinLockUnlock(&currentlyPlayingLock);
return 0;
}
return [entry duration];
double retval = [entry duration];
OSSpinLockUnlock(&currentlyPlayingLock);
return retval;
}
-(double) progress
@@ -1211,11 +1250,22 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
return 0;
}
SPIN_LOCK_LOCK(&currentlyPlayingLock);
QueueEntry* entry = currentlyPlayingEntry;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
return [entry progress];
QueueEntry* entry = currentlyPlayingEntry;
if (entry == nil)
{
OSSpinLockUnlock(&currentlyPlayingLock);
return 0;
}
double retval = [entry progress];
OSSpinLockUnlock(&currentlyPlayingLock);
return retval;
}
-(void) wakeupPlaybackThread
@@ -1322,9 +1372,9 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
{
[currentlyReadingEntry.dataSource seekToOffset:0];
}
[currentlyReadingEntry.dataSource registerForEvents:[NSRunLoop currentRunLoop]];
if (startPlaying)
{
[bufferingQueue removeAllObjects];
@@ -1365,9 +1415,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
double progress = [entry progress];
double duration = [entry duration];
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
BOOL nextIsDifferent = currentlyPlayingEntry != next;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
if (next)
{
@@ -1378,35 +1426,33 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
seekToTimeWasRequested = NO;
}
SPIN_LOCK_LOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
currentlyPlayingEntry = next;
currentlyPlayingEntry->bytesPlayed = 0;
NSObject* playingQueueItemId = playingQueueItemId = currentlyPlayingEntry.queueItemId;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockUnlock(&currentlyPlayingLock);
if (nextIsDifferent && entry)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self.delegate audioPlayer:self didFinishPlayingQueueItemId:queueItemId withReason:stopReason andProgress:progress andDuration:duration];
});
{
[self.delegate audioPlayer:self didFinishPlayingQueueItemId:queueItemId withReason:stopReason andProgress:progress andDuration:duration];
});
}
if (nextIsDifferent)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self.delegate audioPlayer:self didStartPlayingQueueItemId:playingQueueItemId];
});
{
[self.delegate audioPlayer:self didStartPlayingQueueItemId:playingQueueItemId];
});
}
}
else
{
SPIN_LOCK_LOCK(&currentlyPlayingLock);
{
currentlyPlayingEntry = nil;
}
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
currentlyPlayingEntry = nil;
OSSpinLockUnlock(&currentlyPlayingLock);
if (currentlyReadingEntry == nil)
{
@@ -1475,11 +1521,9 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
[bufferingQueue dequeue];
}
SPIN_LOCK_LOCK(&currentlyPlayingLock);
{
currentlyPlayingEntry = nil;
}
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
currentlyPlayingEntry = nil;
OSSpinLockUnlock(&currentlyPlayingLock);
currentlyReadingEntry = nil;
seekToTimeWasRequested = NO;
@@ -1504,11 +1548,9 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
[bufferingQueue dequeue];
}
SPIN_LOCK_LOCK(&currentlyPlayingLock);
{
currentlyPlayingEntry = nil;
}
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
currentlyPlayingEntry = nil;
OSSpinLockUnlock(&currentlyPlayingLock);
currentlyReadingEntry = nil;
pthread_mutex_unlock(&queueBuffersMutex);
@@ -1589,16 +1631,16 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
{
[bufferingQueue dequeue];
}
QueueEntry* newEntry = [[QueueEntry alloc] initWithDataSource:currentlyReadingEntry.dataSource andQueueItemId:currentlyReadingEntry.queueItemId];
newEntry->audioStreamBasicDescription = currentlyReadingEntry->audioStreamBasicDescription;
[upcomingQueue skipQueue:newEntry];
SPIN_LOCK_LOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
currentlyReadingEntry = nil;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockUnlock(&currentlyPlayingLock);
}
}
}
@@ -1619,7 +1661,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
-(void) startInternal
{
@autoreleasepool
{
{
playbackThreadRunLoop = [NSRunLoop currentRunLoop];
NSThread.currentThread.threadPriority = 1;
@@ -1628,14 +1670,14 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
packetsFilled = 0;
[playbackThreadRunLoop addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
while (true)
{
if (![self processRunloop])
{
break;
}
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:10];
[playbackThreadRunLoop runMode:NSDefaultRunLoopMode beforeDate:date];
}
@@ -1648,11 +1690,11 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
currentlyReadingEntry = nil;
SPIN_LOCK_LOCK(&currentlyPlayingLock);
{
currentlyPlayingEntry = nil;
}
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
pthread_mutex_lock(&playerMutex);
OSSpinLockLock(&currentlyPlayingLock);
currentlyPlayingEntry = nil;
OSSpinLockUnlock(&currentlyPlayingLock);
pthread_mutex_unlock(&playerMutex);
self.internalState = AudioPlayerInternalStateDisposed;
@@ -1664,9 +1706,9 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
-(void) processSeekToTime
{
OSStatus error;
SPIN_LOCK_LOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
QueueEntry* currentEntry = currentlyReadingEntry;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
OSSpinLockUnlock(&currentlyPlayingLock);
NSAssert(currentEntry == currentlyPlayingEntry, @"playing and reading must be the same");
@@ -1817,14 +1859,14 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self didEncounterError:AudioPlayerErrorQueueStopFailed];;
});
{
[self didEncounterError:AudioPlayerErrorQueueStopFailed];;
});
}
}
}
pthread_mutex_unlock(&playerMutex);
pthread_mutex_lock(&queueBuffersMutex);
if (numberOfBuffersUsed != 0)
@@ -2031,7 +2073,7 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
[self wakeupPlaybackThread];
}
}
pthread_mutex_unlock(&playerMutex);
pthread_mutex_unlock(&playerMutex);
}
-(void) stop
@@ -2104,16 +2146,22 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ
-(NSObject*) currentlyPlayingQueueItemId
{
SPIN_LOCK_LOCK(&currentlyPlayingLock);
OSSpinLockLock(&currentlyPlayingLock);
QueueEntry* entry = currentlyPlayingEntry;
SPIN_LOCK_UNLOCK(&currentlyPlayingLock);
if (entry == nil)
{
OSSpinLockUnlock(&currentlyPlayingLock);
return nil;
}
return entry.queueItemId;
NSObject* retval = entry.queueItemId;
OSSpinLockUnlock(&currentlyPlayingLock);
return retval;
}
@end
+6 -4
View File
@@ -2,7 +2,7 @@
[Homepage](http://tumtumtum.github.com/audjustable)
Audjustable is audio streaming class for iOS and OSX. Audjustable uses CoreAudio to decompress and playback audio whilst providing a clean and simple object-oriented API.
Audjustable is an audio streaming class for iOS and OSX. Audjustable uses CoreAudio to decompress and playback audio whilst providing a clean and simple object-oriented API.
The primary motivation of this project was to decouple the input (DataSource/InputStreams) from the actual player logic in order to allow advanced customizable input handling such as: HTTP streaming, encryption, auto-recovery, dynamic-buffering. Along the way other features such as gapless playback were added as the opportunity arose.
@@ -14,7 +14,7 @@ The primary motivation of this project was to decouple the input (DataSource/Inp
* Mostly asynchronous API
* Buffered and gapless playback
* Easy to implement audio data sources (HTTP and local file system DataSources provided)
* Easy to extend DataSource to support adaptive buffering, encryption etc
* Easy to extend DataSource to support adaptive buffering, encryption, etc.
* Optimised for low CPU/battery usage
## Usage
@@ -23,6 +23,8 @@ Download the [source](https://github.com/tumtumtum/audjustable/zipball/master) w
If you would like to integrate the AudioPlayer directly into your project you only need to copy the files inside the `/Audjustable/Classes/AudioPlayer` [directory](https://github.com/tumtumtum/audjustable/tree/master/Audjustable/Classes/AudioPlayer) into your project.
Audjustable is also available as a [Cocoapod](http://cocoapods.org/?q=audjustable).
## Code
There are two main classes. The `DataSource` class which is the abstract base class for the various compressed audio data sources (HTTP, local file are provided). The `AudioPlayer` class manages and renders audio from a queue DataSources.
@@ -34,7 +36,7 @@ There are two main classes. The `DataSource` class which is the abstract base c
AudioPlayer* audioPlayer = [[AudioPlayer alloc] init];
audioPlayer.delegate = self;
// Queue on a URL to play. Each queue item has a unique ID (item1) that to identify the related file in delegate callbacks
// Queue on a URL to play. Each queue item has a unique ID (item1) that to identify the related file in delegate callbacks
[audioPlayer setDataSource:[audioPlayer dataSourceFromURL:@"https://github.com/downloads/tumtumtum/audjustable/sample.m4a"] withQueueItemId:@"item1"];
@@ -45,4 +47,4 @@ audioPlayer.delegate = self;
Background playback on iOS is easily added to your application by calling the `AudioSessionInitialize` in your AppDelegate.
### Authors and Contributors
Copyright 2012, Thong Nguyen (@tumtumtum)
Copyright 2012, Thong Nguyen (@tumtumtum)