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"
|
|
|
|
#import "OutputNode.h"
|
|
|
|
#import "Status.h"
|
2007-03-02 01:36:52 +00:00
|
|
|
#import "PluginController.h"
|
2007-02-24 20:36:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation AudioPlayer
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
output = NULL;
|
|
|
|
bufferChain = NULL;
|
2007-03-19 00:19:47 +00:00
|
|
|
outputLaunched = NO;
|
2007-02-24 20:36:27 +00:00
|
|
|
|
|
|
|
chainQueue = [[NSMutableArray alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setDelegate:(id)d
|
|
|
|
{
|
|
|
|
delegate = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)delegate {
|
|
|
|
return delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)play:(NSURL *)url
|
|
|
|
{
|
|
|
|
[self play:url withUserInfo:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)play:(NSURL *)url withUserInfo:(id)userInfo
|
|
|
|
{
|
|
|
|
if (output)
|
|
|
|
{
|
|
|
|
[output release];
|
|
|
|
}
|
|
|
|
output = [[OutputNode alloc] initWithController:self previous:nil];
|
|
|
|
[output setup];
|
|
|
|
|
2007-10-13 08:14:05 +00:00
|
|
|
@synchronized(chainQueue) {
|
|
|
|
NSEnumerator *enumerator = [chainQueue objectEnumerator];
|
|
|
|
id anObject;
|
|
|
|
while (anObject = [enumerator nextObject])
|
|
|
|
{
|
|
|
|
[anObject setShouldContinue:NO];
|
|
|
|
}
|
|
|
|
[chainQueue removeAllObjects];
|
|
|
|
|
|
|
|
if (bufferChain)
|
|
|
|
{
|
|
|
|
[bufferChain setShouldContinue:NO];
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2007-10-13 08:14:05 +00:00
|
|
|
[bufferChain release];
|
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
2007-10-13 08:14:05 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
bufferChain = [[BufferChain alloc] initWithController:self];
|
|
|
|
|
|
|
|
while (![bufferChain open:url withOutputFormat:[output format]])
|
|
|
|
{
|
|
|
|
[bufferChain release];
|
2007-05-22 23:15:07 +00:00
|
|
|
bufferChain = nil;
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[self requestNextStream: userInfo];
|
|
|
|
|
|
|
|
url = nextStream;
|
|
|
|
if (url == nil)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
userInfo = nextStreamUserInfo;
|
|
|
|
|
|
|
|
[self notifyStreamChanged:userInfo];
|
|
|
|
|
|
|
|
bufferChain = [[BufferChain alloc] initWithController:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
[bufferChain setUserInfo:userInfo];
|
|
|
|
|
|
|
|
[self setShouldContinue:YES];
|
|
|
|
|
2007-03-19 00:19:47 +00:00
|
|
|
outputLaunched = NO;
|
2007-07-11 01:20:32 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[bufferChain launchThreads];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stop
|
|
|
|
{
|
|
|
|
//Set shouldoContinue to NO on allll things
|
|
|
|
[self setShouldContinue:NO];
|
|
|
|
[self setPlaybackStatus:kCogStatusStopped];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)pause
|
|
|
|
{
|
|
|
|
[output pause];
|
|
|
|
|
|
|
|
[self setPlaybackStatus:kCogStatusPaused];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resume
|
|
|
|
{
|
|
|
|
[output resume];
|
|
|
|
|
|
|
|
[self setPlaybackStatus:kCogStatusPlaying];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)seekToTime:(double)time
|
|
|
|
{
|
|
|
|
//Need to reset everything's buffers, and then seek?
|
|
|
|
/*HACK TO TEST HOW WELL THIS WOULD WORK*/
|
|
|
|
[output seek:time];
|
2007-05-26 22:13:11 +00:00
|
|
|
[bufferChain seek:time];
|
2007-02-24 20:36:27 +00:00
|
|
|
/*END HACK*/
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVolume:(double)v
|
|
|
|
{
|
|
|
|
[output setVolume:v];
|
|
|
|
}
|
|
|
|
|
2007-10-11 11:31:36 +00:00
|
|
|
//This is called by the delegate DURING a requestNextStream request.
|
2007-02-24 20:36:27 +00:00
|
|
|
- (void)setNextStream:(NSURL *)url
|
|
|
|
{
|
|
|
|
[self setNextStream:url withUserInfo:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setNextStream:(NSURL *)url withUserInfo:(id)userInfo
|
|
|
|
{
|
|
|
|
[url retain];
|
|
|
|
[nextStream release];
|
|
|
|
nextStream = url;
|
|
|
|
|
|
|
|
[userInfo retain];
|
|
|
|
[nextStreamUserInfo release];
|
|
|
|
nextStreamUserInfo = userInfo;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)setShouldContinue:(BOOL)s
|
|
|
|
{
|
2007-10-13 08:25:44 +00:00
|
|
|
if (bufferChain)
|
|
|
|
[bufferChain setShouldContinue:s];
|
|
|
|
|
|
|
|
if (output)
|
|
|
|
[output setShouldContinue:s];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (double)amountPlayed
|
|
|
|
{
|
|
|
|
return [output amountPlayed];
|
|
|
|
}
|
|
|
|
|
2007-03-19 00:19:47 +00:00
|
|
|
- (void)launchOutputThread
|
|
|
|
{
|
|
|
|
if (outputLaunched == NO) {
|
2007-10-13 08:50:40 +00:00
|
|
|
[self setPlaybackStatus:kCogStatusPlaying];
|
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
|
|
|
|
|
|
|
- (void)requestNextStream:(id)userInfo
|
|
|
|
{
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:requestNextStream:) withObject:userInfo waitUntilDone:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notifyStreamChanged:(id)userInfo
|
|
|
|
{
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:streamChanged:) withObject:userInfo waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
- (void)addChainToQueue:(BufferChain *)newChain
|
|
|
|
{
|
|
|
|
[newChain setUserInfo: nextStreamUserInfo];
|
|
|
|
|
|
|
|
[newChain setShouldContinue:YES];
|
|
|
|
[newChain launchThreads];
|
|
|
|
|
|
|
|
[chainQueue insertObject:newChain atIndex:[chainQueue count]];
|
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
- (BOOL)endOfInputReached:(BufferChain *)sender //Sender is a BufferChain
|
2007-02-24 20:36:27 +00:00
|
|
|
{
|
|
|
|
BufferChain *newChain = nil;
|
|
|
|
|
|
|
|
nextStreamUserInfo = [sender userInfo];
|
|
|
|
[nextStreamUserInfo retain]; //Retained because when setNextStream is called, it will be released!!!
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
[self requestNextStream: nextStreamUserInfo];
|
|
|
|
newChain = [[BufferChain alloc] initWithController:self];
|
|
|
|
|
|
|
|
BufferChain *lastChain = [chainQueue lastObject];
|
|
|
|
if (lastChain == nil) {
|
|
|
|
lastChain = bufferChain;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([[nextStream scheme] isEqualToString:[[lastChain streamURL] scheme]]
|
|
|
|
&& [[nextStream host] isEqualToString:[[lastChain streamURL] host]]
|
|
|
|
&& [[nextStream path] isEqualToString:[[lastChain streamURL] path]])
|
|
|
|
{
|
|
|
|
if ([lastChain setTrack:nextStream]) {
|
|
|
|
[newChain openWithInput:[lastChain inputNode] withOutputFormat:[output format]];
|
|
|
|
|
|
|
|
[newChain setStreamURL:nextStream];
|
|
|
|
[newChain setUserInfo:nextStreamUserInfo];
|
|
|
|
|
|
|
|
[self addChainToQueue:newChain];
|
|
|
|
NSLog(@"TRACK SET!!! %@", newChain);
|
|
|
|
//Keep on-playin
|
|
|
|
[newChain release];
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (![newChain open:nextStream withOutputFormat:[output format]])
|
|
|
|
{
|
2007-02-24 20:36:27 +00:00
|
|
|
if (nextStream == nil)
|
|
|
|
{
|
2007-10-11 02:08:29 +00:00
|
|
|
return YES;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
[newChain release];
|
|
|
|
[self requestNextStream: nextStreamUserInfo];
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
newChain = [[BufferChain alloc] initWithController:self];
|
2007-10-11 02:08:29 +00:00
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
[self addChainToQueue:newChain];
|
2007-02-24 20:36:27 +00:00
|
|
|
|
|
|
|
[newChain release];
|
2007-10-11 02:08:29 +00:00
|
|
|
|
|
|
|
return YES;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)endOfInputPlayed
|
|
|
|
{
|
|
|
|
if ([chainQueue count] <= 0)
|
|
|
|
{
|
|
|
|
//End of playlist
|
|
|
|
[self stop];
|
|
|
|
|
2007-10-13 08:25:44 +00:00
|
|
|
[bufferChain release];
|
|
|
|
bufferChain = nil;
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-13 08:25:44 +00:00
|
|
|
[bufferChain release];
|
|
|
|
|
2007-10-13 08:14:05 +00:00
|
|
|
@synchronized(chainQueue) {
|
|
|
|
bufferChain = [chainQueue objectAtIndex:0];
|
|
|
|
[bufferChain retain];
|
|
|
|
|
|
|
|
NSLog(@"New!!! %@ %@", bufferChain, [[bufferChain inputNode] decoder]);
|
|
|
|
|
|
|
|
[chainQueue removeObjectAtIndex:0];
|
|
|
|
}
|
2007-10-11 02:08:29 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
[self notifyStreamChanged:[bufferChain userInfo]];
|
|
|
|
[output setEndOfStream:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendDelegateMethod:(SEL)selector withObject:(id)obj waitUntilDone:(BOOL)wait
|
|
|
|
{
|
|
|
|
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:selector]];
|
|
|
|
[invocation setSelector:selector];
|
|
|
|
[invocation setArgument:&self atIndex:2]; //Indexes start at 2, the first being self, the second being command.
|
|
|
|
[invocation setArgument:&obj atIndex:3];
|
|
|
|
|
|
|
|
[self performSelectorOnMainThread:@selector(sendDelegateMethodMainThread:) withObject:invocation waitUntilDone:wait];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)sendDelegateMethodMainThread:(id)invocation
|
|
|
|
{
|
|
|
|
[invocation invokeWithTarget:delegate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPlaybackStatus:(int)status
|
|
|
|
{
|
|
|
|
[self sendDelegateMethod:@selector(audioPlayer:statusChanged:) withObject:[NSNumber numberWithInt:status] waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BufferChain *)bufferChain
|
|
|
|
{
|
|
|
|
return bufferChain;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OutputNode *) output
|
|
|
|
{
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2007-10-09 01:20:46 +00:00
|
|
|
+ (NSArray *)containerTypes
|
|
|
|
{
|
|
|
|
return [[[PluginController sharedPluginController] containers] allKeys];
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
|
|
|
PluginController *pluginController = [PluginController sharedPluginController];
|
|
|
|
|
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];
|
2007-02-24 20:36:27 +00:00
|
|
|
|
|
|
|
NSMutableSet *types = [NSMutableSet set];
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
return [types allObjects];
|
|
|
|
}
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
+ (NSArray *)schemes
|
|
|
|
{
|
|
|
|
PluginController *pluginController = [PluginController sharedPluginController];
|
|
|
|
|
|
|
|
return [[pluginController sources] allKeys];
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@end
|