2008-02-12 22:12:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
// AudioController.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 8/7/05.
|
|
|
|
// Copyright 2005 Vincent Spader. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "AudioPlayer.h"
|
|
|
|
#import "BufferChain.h"
|
2008-02-17 18:44:11 +00:00
|
|
|
#import "Helper.h"
|
2022-02-07 05:49:27 +00:00
|
|
|
#import "OutputNode.h"
|
2007-03-02 01:36:52 +00:00
|
|
|
#import "PluginController.h"
|
2022-02-07 05:49:27 +00:00
|
|
|
#import "Status.h"
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@implementation AudioPlayer
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)init {
|
2007-02-24 20:36:27 +00:00
|
|
|
self = [super init];
|
2022-02-07 05:49:27 +00:00
|
|
|
if(self) {
|
2007-02-24 20:36:27 +00:00
|
|
|
output = NULL;
|
2016-05-05 20:05:39 +00:00
|
|
|
bufferChain = nil;
|
2007-03-19 00:19:47 +00:00
|
|
|
outputLaunched = NO;
|
2009-02-28 19:11:22 +00:00
|
|
|
endOfInputReached = NO;
|
2021-12-28 07:21:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
chainQueue = [[NSMutableArray alloc] init];
|
|
|
|
|
|
|
|
semaphore = [[Semaphore alloc] init];
|
|
|
|
|
|
|
|
atomic_init(&resettingNow, false);
|
|
|
|
atomic_init(&refCount, 0);
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setDelegate:(id)d {
|
2007-02-24 20:36:27 +00:00
|
|
|
delegate = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)delegate {
|
|
|
|
return delegate;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)play:(NSURL *)url {
|
2021-12-26 12:35:54 +00:00
|
|
|
[self play:url withUserInfo:nil withRGInfo:nil startPaused:NO andSeekTo:0.0];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)play:(NSURL *)url withUserInfo:(id)userInfo withRGInfo:(NSDictionary *)rgi {
|
|
|
|
[self play:url withUserInfo:userInfo withRGInfo:rgi startPaused:NO andSeekTo:0.0];
|
2013-10-13 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)play:(NSURL *)url withUserInfo:(id)userInfo withRGInfo:(NSDictionary *)rgi startPaused:(BOOL)paused {
|
|
|
|
[self play:url withUserInfo:userInfo withRGInfo:rgi startPaused:paused andSeekTo:0.0];
|
2021-12-26 12:35:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)play:(NSURL *)url withUserInfo:(id)userInfo withRGInfo:(NSDictionary *)rgi startPaused:(BOOL)paused andSeekTo:(double)time {
|
|
|
|
ALog(@"Opening file for playback: %@ at seek offset %f%@", url, time, (paused) ? @", starting paused" : @"");
|
|
|
|
|
|
|
|
[self waitUntilCallbacksExit];
|
|
|
|
if(output) {
|
|
|
|
[output setShouldContinue:NO];
|
2022-06-25 12:12:43 +00:00
|
|
|
[output close];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2022-06-25 02:14:48 +00:00
|
|
|
if(!output) {
|
|
|
|
output = [[OutputNode alloc] initWithController:self previous:nil];
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
[output setup];
|
|
|
|
[output setVolume:volume];
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
for(id anObject in chainQueue) {
|
2007-10-13 08:14:05 +00:00
|
|
|
[anObject setShouldContinue:NO];
|
|
|
|
}
|
|
|
|
[chainQueue removeAllObjects];
|
2009-02-28 19:11:22 +00:00
|
|
|
endOfInputReached = NO;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(bufferChain) {
|
2007-10-13 08:14:05 +00:00
|
|
|
[bufferChain setShouldContinue:NO];
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
bufferChain = nil;
|
2007-10-13 08:14:05 +00:00
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
2013-10-12 20:59:34 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
bufferChain = [[BufferChain alloc] initWithController:self];
|
2009-03-05 17:03:30 +00:00
|
|
|
[self notifyStreamChanged:userInfo];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
while(![bufferChain open:url withUserInfo:userInfo withRGInfo:rgi]) {
|
2007-05-22 23:15:07 +00:00
|
|
|
bufferChain = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
[self requestNextStream:userInfo];
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2022-07-06 09:20:05 +00:00
|
|
|
if([nextStream isEqualTo:url]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
url = nextStream;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(url == nil) {
|
2007-02-24 20:36:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
userInfo = nextStreamUserInfo;
|
2022-02-07 05:49:27 +00:00
|
|
|
rgi = nextStreamRGInfo;
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[self notifyStreamChanged:userInfo];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
bufferChain = [[BufferChain alloc] initWithController:self];
|
|
|
|
}
|
2013-10-12 20:59:34 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(time > 0.0) {
|
|
|
|
[output seek:time];
|
|
|
|
[bufferChain seek:time];
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[self setShouldContinue:YES];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-19 00:19:47 +00:00
|
|
|
outputLaunched = NO;
|
2022-02-07 05:49:27 +00:00
|
|
|
startedPaused = paused;
|
|
|
|
initialBufferFilled = NO;
|
2022-06-28 04:46:36 +00:00
|
|
|
previousUserInfo = userInfo;
|
2007-07-11 01:20:32 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[bufferChain launchThreads];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(paused)
|
|
|
|
[self setPlaybackStatus:CogStatusPaused waitUntilDone:YES];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)stop {
|
|
|
|
// Set shouldoContinue to NO on all things
|
2007-02-24 20:36:27 +00:00
|
|
|
[self setShouldContinue:NO];
|
2021-02-06 21:20:03 +00:00
|
|
|
[self setPlaybackStatus:CogStatusStopped waitUntilDone:YES];
|
2022-01-30 05:10:59 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
@synchronized(chainQueue) {
|
|
|
|
for(id anObject in chainQueue) {
|
|
|
|
[anObject setShouldContinue:NO];
|
|
|
|
}
|
|
|
|
[chainQueue removeAllObjects];
|
|
|
|
endOfInputReached = NO;
|
|
|
|
if(bufferChain) {
|
|
|
|
bufferChain = nil;
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 02:14:48 +00:00
|
|
|
if(output) {
|
|
|
|
[output setShouldContinue:NO];
|
2022-06-25 12:12:43 +00:00
|
|
|
[output close];
|
2022-06-25 02:14:48 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
output = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)pause {
|
2007-02-24 20:36:27 +00:00
|
|
|
[output pause];
|
|
|
|
|
2021-02-06 21:20:03 +00:00
|
|
|
[self setPlaybackStatus:CogStatusPaused waitUntilDone:YES];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)resume {
|
|
|
|
if(startedPaused) {
|
|
|
|
startedPaused = NO;
|
|
|
|
if(initialBufferFilled)
|
|
|
|
[self launchOutputThread];
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[output resume];
|
|
|
|
|
2021-02-06 21:20:03 +00:00
|
|
|
[self setPlaybackStatus:CogStatusPlaying waitUntilDone:YES];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)seekToTime:(double)time {
|
|
|
|
if(endOfInputReached) {
|
|
|
|
// This is a dirty hack in case the playback has finished with the track
|
|
|
|
// that the user thinks they're seeking into
|
|
|
|
CogStatus status = (CogStatus)currentPlaybackStatus;
|
|
|
|
NSURL *url;
|
|
|
|
id userInfo;
|
|
|
|
NSDictionary *rgi;
|
|
|
|
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
url = [bufferChain streamURL];
|
|
|
|
userInfo = [bufferChain userInfo];
|
|
|
|
rgi = [bufferChain rgInfo];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self stop];
|
|
|
|
|
|
|
|
[self play:url withUserInfo:userInfo withRGInfo:rgi startPaused:(status == CogStatusPaused) andSeekTo:time];
|
|
|
|
} else {
|
|
|
|
// Still decoding the current file, safe to seek within it
|
|
|
|
[output seek:time];
|
|
|
|
[bufferChain seek:time];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVolume:(double)v {
|
2008-02-13 01:50:39 +00:00
|
|
|
volume = v;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[output setVolume:v];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (double)volume {
|
2008-02-13 01:50:39 +00:00
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// This is called by the delegate DURING a requestNextStream request.
|
|
|
|
- (void)setNextStream:(NSURL *)url {
|
2013-10-02 09:30:04 +00:00
|
|
|
[self setNextStream:url withUserInfo:nil withRGInfo:nil];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setNextStream:(NSURL *)url withUserInfo:(id)userInfo withRGInfo:(NSDictionary *)rgi {
|
2007-02-24 20:36:27 +00:00
|
|
|
nextStream = url;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
nextStreamUserInfo = userInfo;
|
2013-10-02 09:30:04 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
nextStreamRGInfo = rgi;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 18:57:21 +00:00
|
|
|
// Called when the playlist changed before we actually started playing a requested stream. We will re-request.
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)resetNextStreams {
|
|
|
|
[self waitUntilCallbacksExit];
|
2022-01-14 07:54:16 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
@synchronized(chainQueue) {
|
|
|
|
for(id anObject in chainQueue) {
|
2009-03-01 03:14:52 +00:00
|
|
|
[anObject setShouldContinue:NO];
|
|
|
|
}
|
2009-02-28 18:57:21 +00:00
|
|
|
[chainQueue removeAllObjects];
|
2009-03-01 06:02:26 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(endOfInputReached) {
|
2009-02-28 19:01:49 +00:00
|
|
|
[self endOfInputReached:bufferChain];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2009-02-28 19:01:49 +00:00
|
|
|
}
|
2009-02-28 18:57:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 06:44:56 +00:00
|
|
|
- (void)restartPlaybackAtCurrentPosition {
|
2022-06-28 05:03:02 +00:00
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:restartPlaybackAtCurrentPosition:) withObject:previousUserInfo waitUntilDone:NO];
|
2022-02-08 06:44:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 15:29:02 +00:00
|
|
|
- (void)pushInfo:(NSDictionary *)info toTrack:(id)userInfo {
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:pushInfo:toTrack:) withObject:info withObject:userInfo waitUntilDone:NO];
|
2022-02-09 03:56:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 06:00:08 +00:00
|
|
|
- (void)reportPlayCountForTrack:(id)userInfo {
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:reportPlayCountForTrack:) withObject:userInfo waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setShouldContinue:(BOOL)s {
|
|
|
|
shouldContinue = s;
|
|
|
|
|
|
|
|
if(bufferChain)
|
2007-10-13 08:25:44 +00:00
|
|
|
[bufferChain setShouldContinue:s];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(output)
|
2007-10-13 08:25:44 +00:00
|
|
|
[output setShouldContinue:s];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (double)amountPlayed {
|
2007-02-24 20:36:27 +00:00
|
|
|
return [output amountPlayed];
|
|
|
|
}
|
|
|
|
|
2022-06-19 06:00:08 +00:00
|
|
|
- (double)amountPlayedInterval {
|
|
|
|
return [output amountPlayedInterval];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)launchOutputThread {
|
|
|
|
initialBufferFilled = YES;
|
|
|
|
if(outputLaunched == NO && startedPaused == NO) {
|
|
|
|
[self setPlaybackStatus:CogStatusPlaying];
|
2007-03-19 00:19:47 +00:00
|
|
|
[output launchThread];
|
2007-05-26 12:42:00 +00:00
|
|
|
outputLaunched = YES;
|
|
|
|
}
|
2007-03-19 00:19:47 +00:00
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)requestNextStream:(id)userInfo {
|
2009-03-05 17:03:30 +00:00
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:willEndStream:) withObject:userInfo waitUntilDone:YES];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)notifyStreamChanged:(id)userInfo {
|
2022-01-15 10:09:26 +00:00
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:didBeginStream:) withObject:userInfo waitUntilDone:YES];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 04:55:42 +00:00
|
|
|
- (void)notifyPlaybackStopped:(id)userInfo {
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:didStopNaturally:) withObject:userInfo waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)beginEqualizer:(AudioUnit)eq {
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:displayEqualizer:) withVoid:eq waitUntilDone:YES];
|
2022-01-16 15:32:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)refreshEqualizer:(AudioUnit)eq {
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:refreshEqualizer:) withVoid:eq waitUntilDone:YES];
|
2022-01-18 04:43:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)endEqualizer:(AudioUnit)eq {
|
2022-06-26 21:47:34 +00:00
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:removeEqualizer:) withVoid:eq waitUntilDone:YES];
|
2022-01-16 15:32:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)addChainToQueue:(BufferChain *)newChain {
|
2007-10-11 02:08:29 +00:00
|
|
|
[newChain setShouldContinue:YES];
|
|
|
|
[newChain launchThreads];
|
2021-12-28 07:21:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[chainQueue insertObject:newChain atIndex:[chainQueue count]];
|
2021-12-28 23:40:26 +00:00
|
|
|
}
|
2021-12-28 07:21:48 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)endOfInputReached:(BufferChain *)sender // Sender is a BufferChain
|
2007-02-24 20:36:27 +00:00
|
|
|
{
|
2022-06-28 04:46:36 +00:00
|
|
|
previousUserInfo = [sender userInfo];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
BufferChain *newChain = nil;
|
|
|
|
|
|
|
|
if(atomic_load_explicit(&resettingNow, memory_order_relaxed))
|
|
|
|
return YES;
|
|
|
|
|
|
|
|
atomic_fetch_add(&refCount, 1);
|
|
|
|
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
// No point in constructing new chain for the next playlist entry
|
|
|
|
// if there's already one at the head of chainQueue... r-r-right?
|
|
|
|
for(BufferChain *chain in chainQueue) {
|
|
|
|
if([chain isRunning]) {
|
2022-12-10 05:18:47 +00:00
|
|
|
if(output)
|
|
|
|
[output setShouldPlayOutBuffer:YES];
|
2022-02-07 05:49:27 +00:00
|
|
|
atomic_fetch_sub(&refCount, 1);
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to do this, it may happen with a lot of short files
|
|
|
|
// if ([chainQueue count] >= 5)
|
|
|
|
//{
|
|
|
|
// return YES;
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
double duration = 0.0;
|
|
|
|
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
for(BufferChain *chain in chainQueue) {
|
|
|
|
duration += [chain secondsBuffered];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(duration >= 30.0 && shouldContinue) {
|
|
|
|
[semaphore wait];
|
|
|
|
if(atomic_load_explicit(&resettingNow, memory_order_relaxed)) {
|
2022-12-10 05:18:47 +00:00
|
|
|
if(output)
|
|
|
|
[output setShouldPlayOutBuffer:YES];
|
2022-02-07 05:49:27 +00:00
|
|
|
atomic_fetch_sub(&refCount, 1);
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
duration = 0.0;
|
|
|
|
for(BufferChain *chain in chainQueue) {
|
|
|
|
duration += [chain secondsBuffered];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nextStreamUserInfo = [sender userInfo];
|
|
|
|
|
|
|
|
nextStreamRGInfo = [sender rgInfo];
|
|
|
|
|
|
|
|
// This call can sometimes lead to invoking a chainQueue block on another thread
|
|
|
|
[self requestNextStream:nextStreamUserInfo];
|
|
|
|
|
|
|
|
if(!nextStream) {
|
2022-12-10 05:18:47 +00:00
|
|
|
if(output)
|
|
|
|
[output setShouldPlayOutBuffer:YES];
|
2022-02-07 05:49:27 +00:00
|
|
|
atomic_fetch_sub(&refCount, 1);
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
BufferChain *lastChain;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
@synchronized(chainQueue) {
|
2009-02-28 19:11:22 +00:00
|
|
|
newChain = [[BufferChain alloc] initWithController:self];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2009-02-28 19:11:22 +00:00
|
|
|
endOfInputReached = YES;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
lastChain = [chainQueue lastObject];
|
2022-02-07 05:49:27 +00:00
|
|
|
if(lastChain == nil) {
|
2007-10-20 03:24:27 +00:00
|
|
|
lastChain = bufferChain;
|
2007-10-11 02:08:29 +00:00
|
|
|
}
|
2022-07-05 07:53:55 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
BOOL pathsEqual = NO;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
if([nextStream isFileURL] && [[lastChain streamURL] isFileURL]) {
|
|
|
|
NSString *unixPathNext = [nextStream path];
|
|
|
|
NSString *unixPathPrev = [[lastChain streamURL] path];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
if([unixPathNext isEqualToString:unixPathPrev])
|
|
|
|
pathsEqual = YES;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
if(pathsEqual || ([[nextStream scheme] isEqualToString:[[lastChain streamURL] scheme]] && (([nextStream host] == nil && [[lastChain streamURL] host] == nil) || [[nextStream host] isEqualToString:[[lastChain streamURL] host]]) && [[nextStream path] isEqualToString:[[lastChain streamURL] path]])) {
|
|
|
|
if([lastChain setTrack:nextStream] && [newChain openWithInput:[lastChain inputNode] withUserInfo:nextStreamUserInfo withRGInfo:nextStreamRGInfo]) {
|
|
|
|
[newChain setStreamURL:nextStream];
|
2007-10-20 03:24:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
@synchronized(chainQueue) {
|
2007-10-20 03:24:27 +00:00
|
|
|
[self addChainToQueue:newChain];
|
|
|
|
}
|
2022-07-05 07:53:55 +00:00
|
|
|
DLog(@"TRACK SET!!! %@", newChain);
|
|
|
|
// Keep on-playin
|
|
|
|
newChain = nil;
|
|
|
|
|
|
|
|
atomic_fetch_sub(&refCount, 1);
|
|
|
|
return NO;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
2022-07-05 07:53:55 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
lastChain = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-06 09:20:05 +00:00
|
|
|
NSURL *url = nextStream;
|
|
|
|
|
|
|
|
while(shouldContinue && ![newChain open:url withUserInfo:nextStreamUserInfo withRGInfo:nextStreamRGInfo]) {
|
2022-07-05 07:53:55 +00:00
|
|
|
if(nextStream == nil) {
|
2022-02-07 05:49:27 +00:00
|
|
|
newChain = nil;
|
2022-12-10 05:18:47 +00:00
|
|
|
if(output)
|
|
|
|
[output setShouldPlayOutBuffer:YES];
|
2022-07-05 07:53:55 +00:00
|
|
|
atomic_fetch_sub(&refCount, 1);
|
|
|
|
return YES;
|
2007-10-20 03:24:27 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
newChain = nil;
|
2022-07-05 07:53:55 +00:00
|
|
|
[self requestNextStream:nextStreamUserInfo];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-06 09:20:05 +00:00
|
|
|
if([nextStream isEqualTo:url]) {
|
|
|
|
newChain = nil;
|
2022-12-10 05:18:47 +00:00
|
|
|
if(output)
|
|
|
|
[output setShouldPlayOutBuffer:YES];
|
2022-07-06 09:20:05 +00:00
|
|
|
atomic_fetch_sub(&refCount, 1);
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
url = nextStream;
|
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
newChain = [[BufferChain alloc] initWithController:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
[self addChainToQueue:newChain];
|
2007-10-11 02:08:29 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-05 07:53:55 +00:00
|
|
|
newChain = nil;
|
|
|
|
|
|
|
|
// I'm stupid and can't hold too much stuff in my head all at once, so writing it here.
|
|
|
|
//
|
|
|
|
// Once we get here:
|
|
|
|
// - buffer chain for previous stream finished reading
|
|
|
|
// - there are (probably) some bytes of the previous stream in the output buffer which haven't been played
|
|
|
|
// (by output node) yet
|
|
|
|
// - self.bufferChain == previous playlist entry's buffer chain
|
|
|
|
// - self.nextStream == next playlist entry's URL
|
|
|
|
// - self.nextStreamUserInfo == next playlist entry
|
|
|
|
// - head of chainQueue is the buffer chain for the next entry (which has launched its threads already)
|
|
|
|
|
2022-12-10 05:18:47 +00:00
|
|
|
if(output)
|
|
|
|
[output setShouldPlayOutBuffer:YES];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
atomic_fetch_sub(&refCount, 1);
|
2007-10-11 02:08:29 +00:00
|
|
|
return YES;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 06:00:08 +00:00
|
|
|
- (void)reportPlayCount {
|
2022-06-28 04:46:36 +00:00
|
|
|
[self reportPlayCountForTrack:previousUserInfo];
|
2022-06-19 06:00:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
- (BOOL)selectNextBuffer {
|
|
|
|
BOOL signalStopped = NO;
|
|
|
|
do {
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
endOfInputReached = NO;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
if([chainQueue count] <= 0) {
|
|
|
|
// End of playlist
|
|
|
|
signalStopped = YES;
|
|
|
|
break;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-20 03:24:27 +00:00
|
|
|
bufferChain = nil;
|
2022-06-24 06:17:31 +00:00
|
|
|
bufferChain = [chainQueue objectAtIndex:0];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
[chainQueue removeObjectAtIndex:0];
|
|
|
|
DLog(@"New!!! %@ %@", bufferChain, [[bufferChain inputNode] decoder]);
|
2022-02-22 04:55:42 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
[semaphore signal];
|
2007-10-20 03:24:27 +00:00
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
} while(0);
|
|
|
|
|
|
|
|
if(signalStopped) {
|
2022-06-25 13:42:56 +00:00
|
|
|
double latency = 0;
|
|
|
|
if(output) latency = [output latency];
|
|
|
|
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, latency * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
|
|
|
[self stop];
|
2013-10-12 20:52:58 +00:00
|
|
|
|
2022-06-25 13:42:56 +00:00
|
|
|
self->bufferChain = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-25 13:42:56 +00:00
|
|
|
[self notifyPlaybackStopped:nil];
|
|
|
|
});
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
return YES;
|
2007-10-13 08:14:05 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
[output setEndOfStream:NO];
|
|
|
|
|
|
|
|
return NO;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)endOfInputPlayed {
|
|
|
|
// Once we get here:
|
|
|
|
// - the buffer chain for the next playlist entry (started in endOfInputReached) have been working for some time
|
|
|
|
// already, so that there is some decoded and converted data to play
|
|
|
|
// - the buffer chain for the next entry is the first item in chainQueue
|
2022-06-30 06:26:30 +00:00
|
|
|
previousUserInfo = [bufferChain userInfo];
|
|
|
|
[self notifyStreamChanged:previousUserInfo];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)chainQueueHasTracks {
|
|
|
|
@synchronized(chainQueue) {
|
|
|
|
return [chainQueue count] > 0;
|
|
|
|
}
|
|
|
|
return NO;
|
2022-01-15 06:46:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)sendDelegateMethod:(SEL)selector withVoid:(void *)obj waitUntilDone:(BOOL)wait {
|
|
|
|
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:selector]];
|
|
|
|
[invocation setTarget:delegate];
|
|
|
|
[invocation setSelector:selector];
|
|
|
|
[invocation setArgument:(void *)&self atIndex:2];
|
|
|
|
[invocation setArgument:&obj atIndex:3];
|
|
|
|
[invocation retainArguments];
|
|
|
|
|
|
|
|
[invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:wait];
|
2022-01-16 15:32:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)sendDelegateMethod:(SEL)selector withObject:(id)obj waitUntilDone:(BOOL)wait {
|
2007-02-24 20:36:27 +00:00
|
|
|
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:selector]];
|
2022-02-07 05:49:27 +00:00
|
|
|
[invocation setTarget:delegate];
|
2007-02-24 20:36:27 +00:00
|
|
|
[invocation setSelector:selector];
|
2022-02-07 05:49:27 +00:00
|
|
|
[invocation setArgument:(void *)&self atIndex:2];
|
|
|
|
[invocation setArgument:&obj atIndex:3];
|
|
|
|
[invocation retainArguments];
|
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
[invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:wait];
|
2009-03-05 17:03:30 +00:00
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)sendDelegateMethod:(SEL)selector withObject:(id)obj withObject:(id)obj2 waitUntilDone:(BOOL)wait {
|
2009-03-05 17:03:30 +00:00
|
|
|
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:selector]];
|
2022-02-07 05:49:27 +00:00
|
|
|
[invocation setTarget:delegate];
|
|
|
|
[invocation setSelector:selector];
|
|
|
|
[invocation setArgument:(void *)&self atIndex:2];
|
|
|
|
[invocation setArgument:&obj atIndex:3];
|
|
|
|
[invocation setArgument:&obj2 atIndex:4];
|
|
|
|
[invocation retainArguments];
|
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
[invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:wait];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setPlaybackStatus:(int)status waitUntilDone:(BOOL)wait {
|
|
|
|
currentPlaybackStatus = status;
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2022-06-17 13:39:02 +00:00
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:didChangeStatus:userInfo:) withObject:@(status) withObject:[bufferChain userInfo] waitUntilDone:wait];
|
2008-02-22 03:46:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)sustainHDCD {
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:sustainHDCD:) withObject:[bufferChain userInfo] waitUntilDone:NO];
|
2022-01-21 07:53:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 10:15:48 +00:00
|
|
|
- (void)setError:(BOOL)status {
|
2022-06-17 13:39:02 +00:00
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:setError:toTrack:) withObject:@(status) withObject:[bufferChain userInfo] waitUntilDone:NO];
|
2022-02-10 10:15:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setPlaybackStatus:(int)status {
|
2008-02-22 03:46:04 +00:00
|
|
|
[self setPlaybackStatus:status waitUntilDone:NO];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BufferChain *)bufferChain {
|
2007-02-24 20:36:27 +00:00
|
|
|
return bufferChain;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (OutputNode *)output {
|
2007-02-24 20:36:27 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)containerTypes {
|
2007-10-09 01:20:46 +00:00
|
|
|
return [[[PluginController sharedPluginController] containers] allKeys];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)fileTypes {
|
2007-02-24 20:36:27 +00:00
|
|
|
PluginController *pluginController = [PluginController sharedPluginController];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-09 01:20:46 +00:00
|
|
|
NSArray *containerTypes = [[pluginController containers] allKeys];
|
2007-10-14 18:12:15 +00:00
|
|
|
NSArray *decoderTypes = [[pluginController decodersByExtension] allKeys];
|
2007-02-24 20:36:27 +00:00
|
|
|
NSArray *metdataReaderTypes = [[pluginController metadataReaders] allKeys];
|
2007-10-14 18:12:15 +00:00
|
|
|
NSArray *propertiesReaderTypes = [[pluginController propertiesReadersByExtension] allKeys];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
NSMutableSet *types = [NSMutableSet set];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-09 01:20:46 +00:00
|
|
|
[types addObjectsFromArray:containerTypes];
|
2007-02-24 20:36:27 +00:00
|
|
|
[types addObjectsFromArray:decoderTypes];
|
|
|
|
[types addObjectsFromArray:metdataReaderTypes];
|
|
|
|
[types addObjectsFromArray:propertiesReaderTypes];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
return [types allObjects];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)schemes {
|
2007-03-09 01:16:06 +00:00
|
|
|
PluginController *pluginController = [PluginController sharedPluginController];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
return [[pluginController sources] allKeys];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (double)volumeUp:(double)amount {
|
|
|
|
BOOL volumeLimit = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"volumeLimit"];
|
|
|
|
const double MAX_VOLUME = (volumeLimit) ? 100.0 : 800.0;
|
|
|
|
|
2022-01-18 06:41:26 +00:00
|
|
|
double newVolume = linearToLogarithmic(logarithmicToLinear(volume + amount, MAX_VOLUME), MAX_VOLUME);
|
2022-02-07 05:49:27 +00:00
|
|
|
if(newVolume > MAX_VOLUME)
|
2008-02-17 18:44:11 +00:00
|
|
|
newVolume = MAX_VOLUME;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2008-02-17 18:44:11 +00:00
|
|
|
[self setVolume:newVolume];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2008-02-17 18:44:11 +00:00
|
|
|
// the playbackController needs to know the new volume, so it can update the
|
|
|
|
// volumeSlider accordingly.
|
|
|
|
return newVolume;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (double)volumeDown:(double)amount {
|
|
|
|
BOOL volumeLimit = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"volumeLimit"];
|
|
|
|
const double MAX_VOLUME = (volumeLimit) ? 100.0 : 800.0;
|
2022-01-18 06:41:26 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
double newVolume;
|
|
|
|
if(amount > volume)
|
2008-02-17 18:44:11 +00:00
|
|
|
newVolume = 0.0;
|
|
|
|
else
|
2022-01-18 06:41:26 +00:00
|
|
|
newVolume = linearToLogarithmic(logarithmicToLinear(volume - amount, MAX_VOLUME), MAX_VOLUME);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2008-02-17 18:44:11 +00:00
|
|
|
[self setVolume:newVolume];
|
|
|
|
return newVolume;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)waitUntilCallbacksExit {
|
|
|
|
// This sucks! And since the thread that's inside the function can be calling
|
|
|
|
// event dispatches, we have to pump the message queue if we're on the main
|
|
|
|
// thread. Damn.
|
|
|
|
if(atomic_load_explicit(&refCount, memory_order_relaxed) != 0) {
|
|
|
|
BOOL mainThread = (dispatch_queue_get_label(dispatch_get_main_queue()) == dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL));
|
|
|
|
atomic_store(&resettingNow, true);
|
|
|
|
while(atomic_load_explicit(&refCount, memory_order_relaxed) != 0) {
|
|
|
|
[semaphore signal]; // Gotta poke this periodically
|
|
|
|
if(mainThread)
|
|
|
|
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.001]];
|
|
|
|
else
|
|
|
|
usleep(500);
|
|
|
|
}
|
|
|
|
atomic_store(&resettingNow, false);
|
|
|
|
}
|
2022-01-14 07:54:16 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@end
|