2006-01-20 15:34:02 +00:00
|
|
|
//
|
2006-04-02 15:44:08 +00:00
|
|
|
// BufferChain.m
|
2006-01-20 15:34:02 +00:00
|
|
|
// CogNew
|
|
|
|
//
|
2006-09-04 18:46:18 +00:00
|
|
|
// Created by Vincent Spader on 1/4/06.
|
|
|
|
// Copyright 2006 Vincent Spader. All rights reserved.
|
2006-01-20 15:34:02 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "BufferChain.h"
|
2007-03-02 01:36:52 +00:00
|
|
|
#import "AudioSource.h"
|
2007-02-24 20:36:27 +00:00
|
|
|
#import "CoreAudioUtils.h"
|
2022-02-07 05:49:27 +00:00
|
|
|
#import "OutputNode.h"
|
2006-01-20 15:34:02 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
@implementation BufferChain
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)initWithController:(id)c {
|
2006-01-20 15:34:02 +00:00
|
|
|
self = [super init];
|
2022-02-07 05:49:27 +00:00
|
|
|
if(self) {
|
2007-02-24 20:36:27 +00:00
|
|
|
controller = c;
|
|
|
|
streamURL = nil;
|
|
|
|
userInfo = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
rgInfo = nil;
|
2007-03-02 01:36:52 +00:00
|
|
|
|
2006-04-13 02:51:22 +00:00
|
|
|
inputNode = nil;
|
2007-10-03 20:23:14 +00:00
|
|
|
converterNode = nil;
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)buildChain {
|
|
|
|
inputNode = nil;
|
|
|
|
converterNode = nil;
|
|
|
|
|
2006-04-13 02:51:22 +00:00
|
|
|
inputNode = [[InputNode alloc] initWithController:self previous:nil];
|
2007-10-03 20:23:14 +00:00
|
|
|
converterNode = [[ConverterNode alloc] initWithController:self previous:inputNode];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
finalNode = converterNode;
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
- (BOOL)open:(NSURL *)url withUserInfo:(id)userInfo withRGInfo:(NSDictionary *)rgi {
|
2007-03-02 01:36:52 +00:00
|
|
|
[self setStreamURL:url];
|
2022-06-13 02:43:41 +00:00
|
|
|
[self setUserInfo:userInfo];
|
2007-03-02 01:36:52 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
[self buildChain];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
id<CogSource> source = [AudioSource audioSourceForURL:url];
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Opening: %@", url);
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!source || ![source open:url]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Couldn't open source...");
|
2022-02-07 05:49:27 +00:00
|
|
|
url = [NSURL URLWithString:@"silence://10"];
|
|
|
|
source = [AudioSource audioSourceForURL:url];
|
|
|
|
if(![source open:url])
|
|
|
|
return NO;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![inputNode openWithSource:source])
|
2006-04-13 02:51:22 +00:00
|
|
|
return NO;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
NSDictionary *properties = [inputNode properties];
|
|
|
|
|
2022-02-08 06:02:17 +00:00
|
|
|
AudioStreamBasicDescription inputFormat = [inputNode nodeFormat];
|
|
|
|
uint32_t inputChannelConfig = 0;
|
2022-02-07 08:56:05 +00:00
|
|
|
if([properties valueForKey:@"channelConfig"])
|
2022-02-08 03:18:45 +00:00
|
|
|
inputChannelConfig = [[properties valueForKey:@"channelConfig"] unsignedIntValue];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
if(![converterNode setupWithInputFormat:inputFormat withInputConfig:inputChannelConfig isLossless:[[properties valueForKey:@"encoding"] isEqualToString:@"lossless"]])
|
2007-11-24 20:16:27 +00:00
|
|
|
return NO;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[self setRGInfo:rgi];
|
2013-10-07 20:03:34 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// return NO;
|
2007-11-24 20:16:27 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
- (BOOL)openWithInput:(InputNode *)i withUserInfo:(id)userInfo withRGInfo:(NSDictionary *)rgi {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"New buffer chain!");
|
2022-06-13 02:43:41 +00:00
|
|
|
[self setUserInfo:userInfo];
|
2007-10-11 02:08:29 +00:00
|
|
|
[self buildChain];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![inputNode openWithDecoder:[i decoder]])
|
2007-10-11 02:08:29 +00:00
|
|
|
return NO;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
NSDictionary *properties = [inputNode properties];
|
|
|
|
|
2022-02-08 06:02:17 +00:00
|
|
|
AudioStreamBasicDescription inputFormat = [inputNode nodeFormat];
|
|
|
|
uint32_t inputChannelConfig = 0;
|
2022-02-07 08:56:05 +00:00
|
|
|
if([properties valueForKey:@"channelConfig"])
|
2022-02-08 03:18:45 +00:00
|
|
|
inputChannelConfig = [[properties valueForKey:@"channelConfig"] unsignedIntValue];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
DLog(@"Input Properties: %@", properties);
|
2022-06-24 06:17:31 +00:00
|
|
|
if(![converterNode setupWithInputFormat:inputFormat withInputConfig:inputChannelConfig isLossless:[[properties objectForKey:@"encoding"] isEqualToString:@"lossless"]])
|
2007-10-11 02:08:29 +00:00
|
|
|
return NO;
|
2013-10-11 12:03:55 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[self setRGInfo:rgi];
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-12-26 10:01:02 +00:00
|
|
|
- (BOOL)openWithDecoder:(id<CogDecoder>)decoder
|
2022-06-13 02:43:41 +00:00
|
|
|
withUserInfo:(id)userInfo
|
2022-02-07 05:49:27 +00:00
|
|
|
withRGInfo:(NSDictionary *)rgi;
|
2021-12-26 10:01:02 +00:00
|
|
|
{
|
2022-02-07 05:49:27 +00:00
|
|
|
DLog(@"New buffer chain!");
|
2022-06-13 02:43:41 +00:00
|
|
|
[self setUserInfo:userInfo];
|
2022-02-07 05:49:27 +00:00
|
|
|
[self buildChain];
|
|
|
|
|
|
|
|
if(![inputNode openWithDecoder:decoder])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
NSDictionary *properties = [inputNode properties];
|
|
|
|
|
|
|
|
DLog(@"Input Properties: %@", properties);
|
|
|
|
|
2022-02-08 06:02:17 +00:00
|
|
|
AudioStreamBasicDescription inputFormat = [inputNode nodeFormat];
|
|
|
|
uint32_t inputChannelConfig = 0;
|
2022-02-07 08:56:05 +00:00
|
|
|
if([properties valueForKey:@"channelConfig"])
|
2022-02-08 03:18:45 +00:00
|
|
|
inputChannelConfig = [[properties valueForKey:@"channelConfig"] unsignedIntValue];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
if(![converterNode setupWithInputFormat:inputFormat withInputConfig:inputChannelConfig isLossless:[[properties objectForKey:@"encoding"] isEqualToString:@"lossless"]])
|
2022-02-07 05:49:27 +00:00
|
|
|
return NO;
|
|
|
|
|
|
|
|
[self setRGInfo:rgi];
|
|
|
|
|
|
|
|
return YES;
|
2021-12-26 10:01:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)launchThreads {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Properties: %@", [inputNode properties]);
|
2007-10-11 02:08:29 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
[inputNode launchThread];
|
2007-11-24 20:16:27 +00:00
|
|
|
[converterNode launchThread];
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setUserInfo:(id)i {
|
2007-02-24 20:36:27 +00:00
|
|
|
userInfo = i;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)userInfo {
|
2007-02-24 20:36:27 +00:00
|
|
|
return userInfo;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setRGInfo:(NSDictionary *)rgi {
|
|
|
|
rgInfo = rgi;
|
|
|
|
[converterNode setRGInfo:rgi];
|
2013-10-02 09:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSDictionary *)rgInfo {
|
|
|
|
return rgInfo;
|
2013-10-02 09:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
[inputNode setShouldContinue:NO];
|
|
|
|
[[inputNode exitAtTheEndOfTheStream] signal];
|
|
|
|
[[inputNode semaphore] signal];
|
|
|
|
[[inputNode exitAtTheEndOfTheStream] wait]; // wait for decoder to be closed (see InputNode's -(void)process )
|
2013-10-12 20:59:34 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Bufferchain dealloc");
|
2006-04-02 15:44:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)seek:(double)time {
|
|
|
|
long frame = (long)round(time * [[[inputNode properties] objectForKey:@"sampleRate"] floatValue]);
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
[inputNode seek:frame];
|
2006-04-02 20:03:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)endOfInputReached {
|
2007-10-11 02:08:29 +00:00
|
|
|
return [controller endOfInputReached:self];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)setTrack:(NSURL *)track {
|
2007-10-11 02:08:29 +00:00
|
|
|
return [inputNode setTrack:track];
|
2006-04-13 02:51:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)initialBufferFilled:(id)sender {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"INITIAL BUFFER FILLED");
|
2007-03-19 00:19:47 +00:00
|
|
|
[controller launchOutputThread];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (InputNode *)inputNode {
|
2007-10-11 02:08:29 +00:00
|
|
|
return inputNode;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)finalNode {
|
2006-01-20 15:34:02 +00:00
|
|
|
return finalNode;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSURL *)streamURL {
|
2007-02-24 20:36:27 +00:00
|
|
|
return streamURL;
|
2006-04-13 02:51:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setStreamURL:(NSURL *)url {
|
2007-03-02 01:36:52 +00:00
|
|
|
streamURL = url;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setShouldContinue:(BOOL)s {
|
2006-01-20 15:34:02 +00:00
|
|
|
[inputNode setShouldContinue:s];
|
2007-10-03 20:23:14 +00:00
|
|
|
[converterNode setShouldContinue:s];
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)isRunning {
|
|
|
|
InputNode *node = [self inputNode];
|
|
|
|
if(nil != node && [node shouldContinue] && ![node endOfStream]) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
2013-10-12 20:52:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)controller {
|
|
|
|
return controller;
|
2013-10-21 05:04:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (ConverterNode *)converter {
|
|
|
|
return converterNode;
|
2021-12-26 07:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (AudioStreamBasicDescription)inputFormat {
|
2022-02-08 06:02:17 +00:00
|
|
|
return [inputNode nodeFormat];
|
2021-12-26 07:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 08:56:05 +00:00
|
|
|
- (uint32_t)inputConfig {
|
2022-02-08 06:02:17 +00:00
|
|
|
return [inputNode nodeChannelConfig];
|
2022-02-07 08:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (double)secondsBuffered {
|
|
|
|
double duration = 0.0;
|
|
|
|
Node *node = [self finalNode];
|
|
|
|
while(node) {
|
|
|
|
duration += [node secondsBuffered];
|
|
|
|
node = [node previousNode];
|
|
|
|
}
|
|
|
|
return duration;
|
2022-01-14 07:05:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)sustainHDCD {
|
|
|
|
OutputNode *outputNode = (OutputNode *)[controller output];
|
|
|
|
[outputNode sustainHDCD];
|
|
|
|
[controller sustainHDCD];
|
2022-01-19 10:08:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 06:44:56 +00:00
|
|
|
- (void)restartPlaybackAtCurrentPosition {
|
|
|
|
[controller restartPlaybackAtCurrentPosition];
|
|
|
|
}
|
|
|
|
|
2022-02-09 03:56:39 +00:00
|
|
|
- (void)pushInfo:(NSDictionary *)info {
|
2022-02-12 15:29:02 +00:00
|
|
|
[controller pushInfo:info toTrack:userInfo];
|
2022-02-09 03:56:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 10:15:48 +00:00
|
|
|
- (void)setError:(BOOL)status {
|
|
|
|
[controller setError:status];
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
@end
|